From: Faiyaz Ahmed Date: Wed, 21 Jan 2009 22:51:48 +0000 (+0000) Subject: Made generic for all xml X-Git-Tag: sfa-0.9-0@14641~730 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=2f492e2a0c60b375883f699d45ffdfa3a66d0946;p=sfa.git Made generic for all xml --- diff --git a/rspec/rspecvalidate.py b/rspec/rspecvalidate.py index 4c7e76d6..322c7016 100644 --- a/rspec/rspecvalidate.py +++ b/rspec/rspecvalidate.py @@ -5,6 +5,7 @@ # # Validate RSPEC hiearchy, values, types, and names using supplied xsd. +# Class to translate xml to dict # # Faiyaz Ahmed # @@ -22,30 +23,33 @@ import httplib logging.basicConfig(level=logging.DEBUG) -class Schema(): - '''Manipulates Rspec xsd into python dict''' - def __init__(self): - self.NSURL = "www.planet-lab.org" - self.xsd = "planetlab.xsd" - self.schemaDom = None # parsed schema file's DOM - self.schemaDict = self.nodeDict() - - def _getSchema(self): - '''If the schema doesn't exist at the NameSpace's URL, then use the - local file.''' - conn = httplib.HTTPConnection(self.NSURL) - conn.request("GET", "/" + self.xsd) - r1 = conn.getresponse() - if r1.status != 200: - logging.debug("http://%s/%s: file not found" %(self.NSURL,self.xsd)) - if os.path.exists(self.xsd): - logging.debug("using local copy.") - self.schemaDom = minidom.parse(self.xsd) +class Xmlxlate(file): + '''Manipulates xml into python dict''' + def __init__(self, file): + filedom = minidom.parse(file) + if filedom.nodeName == "#document": + self.xmlDom = filedom.childNodes[0] else: - self.schemaDom = minidom.parseString(r1.read()) - # XML begings with a '#document'. Just check to be safe. - if self.schemaDom.nodeName == "#document": - self.schemaDom = self.schemaDom.childNodes[0] + self.xmlDom = filedom + self.xmlDict = self.nodeDict() + + + #def _getSchema(self): + # '''If the schema doesn't exist at the NameSpace's URL, then use the + # local file.''' + # conn = httplib.HTTPConnection(self.NSURL) + # conn.request("GET", "/" + self.xsd) + # r1 = conn.getresponse() + # if r1.status != 200: + # logging.debug("http://%s/%s: file not found" %(self.NSURL,self.xsd)) + # if os.path.exists(self.xsd): + # logging.debug("using local copy.") + # self.schemaDom = minidom.parse(self.xsd) + # else: + # self.schemaDom = minidom.parseString(r1.read()) + # # XML begings with a '#document'. Just check to be safe. + # if self.schemaDom.nodeName == "#document": + # self.schemaDom = self.schemaDom.childNodes[0] def _getText(self, nodelist): @@ -55,12 +59,6 @@ class Schema(): rc = rc + node.data return rc - # The rspec is comprised of 2 parts, and 1 reference: - # attributes/elements describe individual resources - # complexTypes are used to describe a set of attributes/elements - # complexTypes can include a reference to other complexTypes. - - def _getName(self, node): '''Gets name of node. If tag has no name, then return tag's localName''' if not node.nodeName.startswith("#"): @@ -75,9 +73,8 @@ class Schema(): {name : [{attributename : {name: value,}, sequence]}''' children = [] # array of dicts. 1 for each element/attribute. node = {} - if not nodeDom: - self._getSchema() - nodeDom = self.schemaDom + # if a dom isn't passed in the arguments, use the one defined in the class. + if not nodeDom: nodeDom = self.xmlDom if nodeDom.nodeName and not nodeDom.nodeName.startswith("#"): # attributes have tags and values. get {tag: value}, else {type: value} children.append(self._attributeDict(nodeDom)) @@ -101,16 +98,17 @@ class Schema(): def main(fname): pp = pprint.PrettyPrinter(indent=4) - dom = minidom.parse(fname) - s = Schema() - print "Testing Complex Type:" - pp.pprint(s.nodeDict(dom.childNodes[0].childNodes[21])) + s = Xmlxlate(fname) print "Testing Whole Doc:" - pp.pprint(s.nodeDict(dom.childNodes[0])) + pp.pprint(s.xmlDict) + + print "Testing Complex Type:" + pp.pprint(s.nodeDict(s.xmlDom.childNodes[21])) - print "Testing URL ofWhole doc:" - pp.pprint(s.schemaDict) + print "Testing rspec parsing:" + rspec = Xmlxlate("sample_rspec.xml") + pp.pprint(rspec.xmlDict) if __name__ == '__main__': main(fname="planetlab.xsd")