7 # Validate RSPEC hiearchy, values, types, and names using supplied xsd.
9 # Faiyaz Ahmed <faiyaza at cs dot princeton dot edu>
11 # Copyright 2009 Princeton University
12 # http://www.planet-lab.org
18 from xml.dom import minidom
22 logging.basicConfig(level=logging.DEBUG)
26 '''Manipulates Rspec xsd into python dict'''
28 self.NSURL = "www.planet-lab.org"
29 self.xsd = "planetlab.xsd"
30 self.schemaDom = None # parsed schema file's DOM
31 self.schemaDict = self.nodeDict()
34 '''If the schema doesn't exist at the NameSpace's URL, then use the
36 conn = httplib.HTTPConnection(self.NSURL)
37 conn.request("GET", "/" + self.xsd)
38 r1 = conn.getresponse()
40 logging.debug("http://%s/%s: file not found" %(self.NSURL,self.xsd))
41 if os.path.exists(self.xsd):
42 logging.debug("using local copy.")
43 self.schemaDom = minidom.parse(self.xsd)
45 self.schemaDom = minidom.parseString(r1.read())
46 # XML begings with a '#document'. Just check to be safe.
47 if self.schemaDom.nodeName == "#document":
48 self.schemaDom = self.schemaDom.childNodes[0]
51 def _getText(self, nodelist):
54 if node.nodeType == node.TEXT_NODE:
58 # The rspec is comprised of 2 parts, and 1 reference:
59 # attributes/elements describe individual resources
60 # complexTypes are used to describe a set of attributes/elements
61 # complexTypes can include a reference to other complexTypes.
64 def _getName(self, node):
65 '''Gets name of node. If tag has no name, then return tag's localName'''
66 if not node.nodeName.startswith("#"):
67 if node.attributes.has_key("name"):
68 name = node.attributes.get("name").value
73 def nodeDict(self, nodeDom = None):
74 '''Traverse complex node. Create a dict
75 {name : [{attributename : {name: value,}, sequence]}'''
76 children = [] # array of dicts. 1 for each element/attribute.
80 nodeDom = self.schemaDom
81 if nodeDom.nodeName and not nodeDom.nodeName.startswith("#"):
82 # attributes have tags and values. get {tag: value}, else {type: value}
83 children.append(self._attributeDict(nodeDom))
84 # resolve the child nodes.
85 if nodeDom.hasChildNodes():
86 for child in nodeDom.childNodes:
87 childelems = self.nodeDict(child)
88 if len(childelems): children.append(childelems)
89 node = { self._getName(nodeDom) : children}
93 # Attribute. {name : nameofattribute, {items: values})
94 def _attributeDict(self, attributeDom):
95 '''Traverse single attribute node. Create a dict {attributename : {name: value,}]}'''
96 node = {} # parsed dict
97 for attr in attributeDom.attributes.keys():
98 node[attr] = attributeDom.attributes.get(attr).value
103 pp = pprint.PrettyPrinter(indent=4)
104 dom = minidom.parse(fname)
106 print "Testing Complex Type:"
107 pp.pprint(s.nodeDict(dom.childNodes[0].childNodes[21]))
109 print "Testing Whole Doc:"
110 pp.pprint(s.nodeDict(dom.childNodes[0]))
112 print "Testing URL ofWhole doc:"
113 pp.pprint(s.schemaDict)
115 if __name__ == '__main__':
116 main(fname="planetlab.xsd")