17f208f0f9ab2a671f5f7dbecec36056dd243466
[sfa.git] / rspec / rspecvalidate.py
1 #!/usr/bin/python
2
3 # $HeadURL$
4 # $Id$
5
6 #
7 # Validate RSPEC hiearchy, values, types, and names using supplied xsd.
8 #
9 # Faiyaz Ahmed <faiyaza at cs dot princeton dot edu>
10 #
11 # Copyright 2009 Princeton University
12 # http://www.planet-lab.org
13 #
14
15 import sys
16 import pprint
17 from xml.dom import minidom
18
19
20 def getText(nodelist):
21     rc = ""
22     for node in nodelist:
23         if node.nodeType == node.TEXT_NODE:
24             rc = rc + node.data
25     return rc
26
27 # complexType: a supernode comprised of element nodes below it.
28 def traverseComplexType(cmpTypeNode):
29     _elements = {}
30     if cmpTypeNode.hasChildNodes():
31         for n in cmpTypeNode.getElementsByTagName("xsd:attribute"):
32             _elements[n.getAttribute("name")] = {'type': n.getAttribute("type")}
33
34
35 # Element.  {name, value, default}
36 def Element(elementDom):
37     node = {} #parsed dict
38     for attr in elementDom.attributes.keys():
39         node[attr] = elementDom.attributes.get(attr).value
40     # set the name to the name of the element.  otherwise, node name.
41     if elementDom.attributes.has_key("name"): 
42         element = {(elementDom.localName, elementDom.attributes.get("name").value) : node}
43     else:
44         element = {elementDom.localName: node}
45     # print repr(element)
46     # print
47     return element
48
49 # Sequence is a list of dicts.  Each dict is an element type with Type fields
50 def Sequence(sequenceNode):
51     pass
52
53 def buildDict(document, docdict = {}):
54     if document.hasChildNodes():
55         for i in document.childNodes: 
56             if i.attributes: docdict.update({ i.localName: buildDict(i, docdict)})
57     if document.attributes: docdict.update(Element(document))
58     return docdict
59
60 def main(fname):
61     pp = pprint.PrettyPrinter(indent=4)
62     pp.pprint(buildDict(minidom.parse(fname)))
63
64 if __name__ == '__main__':  
65     main(fname="planetlab.xsd")