7 # Validate RSPEC hiearchy, values, types, and names using supplied xsd.
8 # Class to translate xml to dict
10 # Faiyaz Ahmed <faiyaza at cs dot princeton dot edu>
12 # Copyright 2009 Princeton University
13 # http://www.planet-lab.org
19 from xml.dom import minidom
23 logging.basicConfig(level=logging.DEBUG)
27 '''Manipulates xml into python dict'''
28 def __init__(self, file):
29 filedom = minidom.parse(file)
30 if filedom.nodeName == "#document":
31 self.xmlDom = filedom.childNodes[0]
34 self.xmlDict = self.nodeDict()
37 #def _getSchema(self):
38 # '''If the schema doesn't exist at the NameSpace's URL, then use the
40 # conn = httplib.HTTPConnection(self.NSURL)
41 # conn.request("GET", "/" + self.xsd)
42 # r1 = conn.getresponse()
43 # if r1.status != 200:
44 # logging.debug("http://%s/%s: file not found" %(self.NSURL,self.xsd))
45 # if os.path.exists(self.xsd):
46 # logging.debug("using local copy.")
47 # self.schemaDom = minidom.parse(self.xsd)
49 # self.schemaDom = minidom.parseString(r1.read())
50 # # XML begings with a '#document'. Just check to be safe.
51 # if self.schemaDom.nodeName == "#document":
52 # self.schemaDom = self.schemaDom.childNodes[0]
55 def _getText(self, nodelist):
58 if node.nodeType == node.TEXT_NODE:
62 def _getName(self, node):
63 '''Gets name of node. If tag has no name, then return tag's localName'''
64 if not node.nodeName.startswith("#"):
65 if node.attributes.has_key("name"):
66 name = node.attributes.get("name").value
71 def nodeDict(self, nodeDom = None):
72 '''Traverse complex node. Create a dict
73 {name : [{attributename : {name: value,}, sequence]}'''
74 children = [] # array of dicts. 1 for each element/attribute.
76 # if a dom isn't passed in the arguments, use the one defined in the class.
77 if not nodeDom: nodeDom = self.xmlDom
78 if nodeDom.nodeName and not nodeDom.nodeName.startswith("#"):
79 # attributes have tags and values. get {tag: value}, else {type: value}
80 children.append(self._attributeDict(nodeDom))
81 # resolve the child nodes.
82 if nodeDom.hasChildNodes():
83 for child in nodeDom.childNodes:
84 childelems = self.nodeDict(child)
85 if len(childelems): children.append(childelems)
86 node = { self._getName(nodeDom) : children}
90 # Attribute. {name : nameofattribute, {items: values})
91 def _attributeDict(self, attributeDom):
92 '''Traverse single attribute node. Create a dict {attributename : {name: value,}]}'''
93 node = {} # parsed dict
94 for attr in attributeDom.attributes.keys():
95 node[attr] = attributeDom.attributes.get(attr).value
100 pp = pprint.PrettyPrinter(indent=4)
103 print "Testing Whole Doc:"
106 print "Testing Complex Type:"
107 pp.pprint(s.nodeDict(s.xmlDom.childNodes[21]))
109 print "Testing rspec parsing:"
110 rspec = Xmlxlate("sample_rspec.xml")
111 pp.pprint(rspec.xmlDict)
113 if __name__ == '__main__':
114 main(fname="planetlab.xsd")