2 from types import StringTypes
4 from StringIO import StringIO
6 # helper functions to help build xpaths
10 def filter_value(key, value):
12 if isinstance(value, str):
14 value = value.replace('*', '')
15 xpath = 'contains(%s, "%s")' % (key, value)
17 xpath = '%s="%s"' % (key, value)
25 for (key, value) in filter.items():
30 if isinstance(value, str):
31 filter_list.append(XpathFilter.filter_value(key, value))
32 elif isinstance(value, list):
33 stmt = ' or '.join([XpathFilter.filter_value(key, str(val)) for val in value])
34 filter_list.append(stmt)
36 xpath = ' and '.join(filter_list)
37 xpath = '[' + xpath + ']'
40 # a wrapper class around lxml.etree._Element
41 # the reason why we need this one is because of the limitations
42 # we've found in xpath to address documents with multiple namespaces defined
43 # in a nutshell, we deal with xml documents that have
44 # a default namespace defined (xmlns="http://default.com/") and specific prefixes defined
45 # (xmlns:foo="http://foo.com")
46 # according to the documentation instead of writing
47 # element.xpath ( "//node/foo:subnode" )
48 # we'd then need to write xpaths like
49 # element.xpath ( "//{http://default.com/}node/{http://foo.com}subnode" )
50 # which is a real pain..
51 # So just so we can keep some reasonable programming style we need to manage the
52 # namespace map that goes with the _Element (its internal .nsmap being unmutable)
54 def __init__(self, element, namespaces):
55 self.element = element
56 self.namespaces = namespaces
58 # redefine as few methods as possible
59 def xpath(self, xpath, namespaces=None):
61 namespaces = self.namespaces
62 elems = self.element.xpath(xpath, namespaces=namespaces)
63 return [XmlElement(elem, namespaces) for elem in elems]
65 def add_element(self, tagname, **kwds):
66 element = etree.SubElement(self.element, tagname, **kwds)
67 return XmlElement(element, self.namespaces)
69 def append(self, elem):
70 if isinstance(elem, XmlElement):
71 self.element.append(elem.element)
73 self.element.append(elem)
76 return XmlElement(self.element.getparent(), self.namespaces)
78 def get_instance(self, instance_class=None, fields=[]):
80 Returns an instance (dict) of this xml element. The instance
81 holds a reference to this xml element.
83 if not instance_class:
84 instance_class = Object
85 if not fields and hasattr(instance_class, 'fields'):
86 fields = instance_class.fields
89 instance = instance_class(self.attrib, self)
91 instance = instance_class({}, self)
93 if field in self.attrib:
94 instance[field] = self.attrib[field]
97 def add_instance(self, name, instance, fields=[]):
99 Adds the specifed instance(s) as a child element of this xml
102 if not fields and hasattr(instance, 'keys'):
103 fields = instance.keys()
104 elem = self.add_element(name)
106 if field in instance and instance[field]:
107 elem.set(field, unicode(instance[field]))
110 def remove_elements(self, name):
112 Removes all occurences of an element from the tree. Start at
113 specified root_node if specified, otherwise start at tree's root.
116 if not element_name.startswith('//'):
117 element_name = '//' + element_name
118 elements = self.element.xpath('%s ' % name, namespaces=self.namespaces)
119 for element in elements:
120 parent = element.getparent()
121 parent.remove(element)
124 parent = self.getparent()
127 def remove(self, element):
128 if isinstance(element, XmlElement):
129 self.element.remove(element.element)
131 self.element.remove(element)
133 def set_text(self, text):
134 self.element.text = text
136 # Element does not have unset ?!?
137 def unset(self, key):
138 del self.element.attrib[key]
141 return etree.tostring(self.element, encoding='UTF-8', pretty_print=True)
146 # are redirected on self.element
147 def __getattr__ (self, name):
148 if not hasattr(self.element, name):
149 raise AttributeError, name
150 return getattr(self.element, name)
154 def __init__(self, xml=None, namespaces=None):
156 self.namespaces = namespaces
157 self.default_namespace = None
159 if isinstance(xml, basestring):
161 if isinstance(xml, XmlElement):
163 self.namespaces = xml.namespaces
164 elif isinstance(xml, etree._ElementTree) or isinstance(xml, etree._Element):
165 self.parse_xml(etree.tostring(xml))
167 def parse_xml(self, xml):
169 parse rspec into etree
171 parser = etree.XMLParser(remove_blank_text=True)
173 tree = etree.parse(xml, parser)
175 # 'rspec' file doesnt exist. 'rspec' is proably an xml string
177 tree = etree.parse(StringIO(xml), parser)
179 raise Exception, str(e)
180 root = tree.getroot()
181 self.namespaces = dict(root.nsmap)
183 if 'default' not in self.namespaces and None in self.namespaces:
184 # If the 'None' exist, then it's pointing to the default namespace. This makes
185 # it hard for us to write xpath queries for the default naemspace because lxml
186 # wont understand a None prefix. We will just associate the default namespeace
187 # with a key named 'default'.
188 self.namespaces['default'] = self.namespaces.pop(None)
191 self.namespaces['default'] = 'default'
193 self.root = XmlElement(root, self.namespaces)
195 for key in self.root.attrib.keys():
196 if key.endswith('schemaLocation'):
197 # schemaLocation should be at the end of the list.
198 # Use list comprehension to filter out empty strings
199 schema_parts = [x for x in self.root.attrib[key].split(' ') if x]
200 self.schema = schema_parts[1]
201 namespace, schema = schema_parts[0], schema_parts[1]
204 def parse_dict(self, d, root_tag_name='xml', element = None):
206 if self.root is None:
207 self.parse_xml('<%s/>' % root_tag_name)
208 element = self.root.element
214 # handle repeating fields
215 for (key, value) in d.items():
216 if isinstance(value, list):
219 if isinstance(val, dict):
220 child_element = etree.SubElement(element, key)
221 self.parse_dict(val, key, child_element)
222 elif isinstance(val, basestring):
223 child_element = etree.SubElement(element, key).text = val
225 elif isinstance(value, int):
226 d[key] = unicode(d[key])
230 # element.attrib.update will explode if DateTimes are in the
233 # looks like iteritems won't stand side-effects
235 if not isinstance(d[k],StringTypes):
238 element.attrib.update(d)
240 def validate(self, schema):
242 Validate against rng schema
244 relaxng_doc = etree.parse(schema)
245 relaxng = etree.RelaxNG(relaxng_doc)
246 if not relaxng(self.root):
247 error = relaxng.error_log.last_error
248 message = "%s (line %s)" % (error.message, error.line)
249 raise Exception, message
252 def xpath(self, xpath, namespaces=None):
254 namespaces = self.namespaces
255 return self.root.xpath(xpath, namespaces=namespaces)
257 def set(self, key, value):
258 return self.root.set(key, value)
260 def remove_attribute(self, name, element=None):
263 element.remove_attribute(name)
265 def add_element(self, *args, **kwds):
267 Wrapper around etree.SubElement(). Adds an element to
268 specified parent node. Adds element to root node is parent is
271 return self.root.add_element(*args, **kwds)
273 def remove_elements(self, name, element = None):
275 Removes all occurences of an element from the tree. Start at
276 specified root_node if specified, otherwise start at tree's root.
281 element.remove_elements(name)
283 def add_instance(self, *args, **kwds):
284 return self.root.add_instance(*args, **kwds)
286 def get_instance(self, *args, **kwds):
287 return self.root.get_instnace(*args, **kwds)
289 def get_element_attributes(self, elem=None, depth=0):
292 if not hasattr(elem, 'attrib'):
293 # this is probably not an element node with attribute. could be just and an
294 # attribute, return it
296 attrs = dict(elem.attrib)
297 attrs['text'] = str(elem.text).strip()
298 attrs['parent'] = elem.getparent()
299 if isinstance(depth, int) and depth > 0:
300 for child_elem in list(elem):
301 key = str(child_elem.tag)
303 attrs[key] = [self.get_element_attributes(child_elem, depth-1)]
305 attrs[key].append(self.get_element_attributes(child_elem, depth-1))
307 attrs['child_nodes'] = list(elem)
310 def append(self, elem):
311 return self.root.append(elem)
313 def iterchildren(self):
314 return self.root.iterchildren()
316 def merge(self, in_xml):
323 return etree.tostring(self.root.element, encoding='UTF-8', pretty_print=True)
325 # XXX smbaker, for record.load_from_string
326 def todict(self, elem=None):
330 d.update(elem.attrib)
331 d['text'] = elem.text
332 for child in elem.iterchildren():
333 if child.tag not in d:
335 d[child.tag].append(self.todict(child))
337 if len(d)==1 and ("text" in d):
342 def save(self, filename):
343 f = open(filename, 'w')
344 f.write(self.toxml())