5 from xml.dom import minidom
6 from types import StringTypes, ListType
10 def __init__(self, xml = None, xsd = None, NSURL = None):
12 Class to manipulate RSpecs. Reads and parses rspec xml into python dicts
13 and reads python dicts and writes rspec xml
15 self.xsd = # Schema. Can be local or remote file.
16 self.NSURL = # If schema is remote, Name Space URL to query (full path minus filename)
17 self.rootNode = # root of the DOM
18 self.dict = # dict of the RSpec.
19 self.schemaDict = {} # dict of the Schema
32 self.dict = self.toDict()
34 self._parseXSD(self.NSURL + self.xsd)
37 def _getText(self, nodelist):
40 if node.nodeType == node.TEXT_NODE:
44 # The rspec is comprised of 2 parts, and 1 reference:
45 # attributes/elements describe individual resources
46 # complexTypes are used to describe a set of attributes/elements
47 # complexTypes can include a reference to other complexTypes.
50 def _getName(self, node):
51 '''Gets name of node. If tag has no name, then return tag's localName'''
53 if not node.nodeName.startswith("#"):
56 elif node.attributes.has_key("name"):
57 name = node.attributes.get("name").value
61 # Attribute. {name : nameofattribute, {items: values})
62 def _attributeDict(self, attributeDom):
63 '''Traverse single attribute node. Create a dict {attributename : {name: value,}]}'''
64 node = {} # parsed dict
65 for attr in attributeDom.attributes.keys():
66 node[attr] = attributeDom.attributes.get(attr).value
70 def toDict(self, nodeDom = None):
72 convert this rspec to a dict and return it.
76 nodeDom = self.rootNode
78 elementName = nodeDom.nodeName
79 if elementName and not elementName.startswith("#"):
80 # attributes have tags and values. get {tag: value}, else {type: value}
81 node[elementName] = self._attributeDict(nodeDom)
82 # resolve the child nodes.
83 if nodeDom.hasChildNodes():
84 for child in nodeDom.childNodes:
85 childName = self._getName(child)
89 # initialize the possible array of children
90 if not node[elementName].has_key(childName):
91 node[elementName][childName] = []
92 # if child node has text child nodes
93 # append the children to the array as strings
94 if child.hasChildNodes() and isinstance(child.childNodes[0], minidom.Text):
95 for nextchild in child.childNodes:
96 node[elementName][childName].append(nextchild.data)
97 # convert element child node to dict
99 childdict = self.toDict(child)
100 for value in childdict.values():
101 node[elementName][childName].append(value)
102 #node[childName].append(self.toDict(child))
108 convert this rspec to an xml string and return it.
110 return self.rootNode.toxml()
113 def toprettyxml(self):
115 print this rspec in xml in a pretty format.
117 return self.rootNode.toprettyxml()
120 def parseFile(self, filename):
122 read a local xml file and store it as a dom object.
124 dom = minidom.parse(filename)
125 self.rootNode = dom.childNodes[0]
128 def parseString(self, xml):
130 read an xml string and store it as a dom object.
132 xml = xml.replace('\n', '').replace('\t', '').strip()
133 dom = minidom.parseString(xml)
134 self.rootNode = dom.childNodes[0]
137 def _httpGetXSD(self, xsdURI):
138 # split the URI into relevant parts
139 host = xsdURI.split("/")[2]
140 if xsdURI.startswith("https"):
141 conn = httplib.HTTPSConnection(host,
142 httplib.HTTPSConnection.default_port)
143 elif xsdURI.startswith("http"):
144 conn = httplib.HTTPConnection(host,
145 httplib.HTTPConnection.default_port)
146 conn.request("GET", xsdURI)
147 # If we can't download the schema, raise an exception
148 r1 = conn.getresponse()
151 return r1.read().replace('\n', '').replace('\t', '').strip()
154 def _parseXSD(self, xsdURI):
156 Download XSD from URL, or if file, read local xsd file and set schemaDict
158 # Since the schema definiton is a global namespace shared by and agreed upon by
159 # others, this should probably be a URL. Check for URL, download xsd, parse, or
160 # if local file, use local file.
162 if xsdURI.startswith("http"):
164 schemaDom = minidom.parseString(self._httpGetXSD(xsdURI))
166 # logging.debug("%s: web file not found" % xsdURI)
167 # logging.debug("Using local file %s" % self.xsd")
169 print "Can't find %s on the web. Continuing." % xsdURI
171 if os.path.exists(xsdURI):
172 # logging.debug("using local copy.")
173 print "Using local %s" % xsdURI
174 schemaDom = minidom.parse(xsdURI)
176 raise Exception("Can't find xsd locally")
177 self.schemaDict = self.toDict(schemaDom.childNodes[0])
180 def dict2dom(self, rdict, include_doc = False):
182 convert a dict object into a dom object.
185 def elementNode(tagname, rd):
186 element = minidom.Element(tagname)
187 for key in rd.keys():
188 if isinstance(rd[key], StringTypes) or isinstance(rd[key], int):
189 element.setAttribute(key, str(rd[key]))
190 elif isinstance(rd[key], dict):
191 child = elementNode(key, rd[key])
192 element.appendChild(child)
193 elif isinstance(rd[key], list):
195 if isinstance(item, dict):
196 child = elementNode(key, item)
197 element.appendChild(child)
198 elif isinstance(item, StringTypes) or isinstance(item, int):
199 child = minidom.Element(key)
200 text = minidom.Text()
202 child.appendChild(text)
203 element.appendChild(child)
206 # Minidom does not allow documents to have more then one
207 # child, but elements may have many children. Because of
208 # this, the document's root node will be the first key/value
209 # pair in the dictionary.
210 node = elementNode(rdict.keys()[0], rdict.values()[0])
212 rootNode = minidom.Document()
213 rootNode.appendChild(node)
219 def parseDict(self, rdict, include_doc = True):
221 Convert a dictionary into a dom object and store it.
223 self.rootNode = self.dict2dom(rdict, include_doc)
226 def getDictsByTagName(self, tagname, dom = None):
228 Search the dom for all elements with the specified tagname
229 and return them as a list of dicts
234 doms = dom.getElementsByTagName(tagname)
235 dictlist = [self.toDict(d) for d in doms]
236 for item in dictlist:
237 for value in item.values():
241 def getDictByTagNameValue(self, tagname, value, dom = None):
243 Search the dom for the first element with the specified tagname
244 and value and return it as a dict.
249 dicts = self.getDictsByTagName(tagname, dom)
252 if rdict.has_key('name') and rdict['name'] in [value]:
258 def filter(self, tagname, attribute, blacklist = [], whitelist = [], dom = None):
260 Removes all elements where:
261 1. tagname matches the element tag
262 2. attribute matches the element attribte
263 3. attribute value is in valuelist
270 if dom.localName in [tagname] and dom.attributes.has_key(attribute):
271 if whitelist and dom.attributes.get(attribute).value not in whitelist:
272 dom.parentNode.removeChild(dom)
273 if blacklist and dom.attributes.get(attribute).value in blacklist:
274 dom.parentNode.removeChild(dom)
276 if dom.hasChildNodes():
277 for child in dom.childNodes:
278 self.filter(tagname, attribute, blacklist, whitelist, child)
281 def validateDicts(self):
292 class RecordSpec(Rspec):
295 def parseDict(self, rdict, include_doc = False):
297 Convert a dictionary into a dom object and store it.
299 self.rootNode = self.dict2dom(rdict, include_doc)
301 def dict2dom(self, rdict, include_doc = False):
303 if not len(rdict.keys()) == 1:
304 record_dict = {self.root_tag : rdict}
305 return Rspec.dict2dom(self, record_dict, include_doc)