3 from sfa.util.faults import InvalidXML
4 from sfa.rspecs.elements.element import Element
6 from io import StringIO
8 # helper functions to help build xpaths
14 def filter_value(key, value):
16 if isinstance(value, str):
18 value = value.replace('*', '')
19 xpath = 'contains(%s, "%s")' % (key, value)
21 xpath = '%s="%s"' % (key, value)
25 def xpath(filter=None):
31 for (key, value) in list(filter.items()):
36 if isinstance(value, str):
37 filter_list.append(XpathFilter.filter_value(key, value))
38 elif isinstance(value, list):
40 [XpathFilter.filter_value(key, str(val)) for val in value])
41 filter_list.append(stmt)
43 xpath = ' and '.join(filter_list)
44 xpath = '[' + xpath + ']'
47 # a wrapper class around lxml.etree._Element
48 # the reason why we need this one is because of the limitations
49 # we've found in xpath to address documents with multiple namespaces defined
50 # in a nutshell, we deal with xml documents that have
51 # a default namespace defined (xmlns="http://default.com/") and specific prefixes defined
52 # (xmlns:foo="http://foo.com")
53 # according to the documentation instead of writing
54 # element.xpath ( "//node/foo:subnode" )
55 # we'd then need to write xpaths like
56 # element.xpath ( "//{http://default.com/}node/{http://foo.com}subnode" )
57 # which is a real pain..
58 # So just so we can keep some reasonable programming style we need to manage the
59 # namespace map that goes with the _Element (its internal .nsmap being
65 def __init__(self, element, namespaces):
66 self.element = element
67 self.namespaces = namespaces
69 # redefine as few methods as possible
70 def xpath(self, xpath, namespaces=None):
72 namespaces = self.namespaces
73 elems = self.element.xpath(xpath, namespaces=namespaces)
74 return [XmlElement(elem, namespaces) for elem in elems]
76 def add_element(self, tagname, **kwds):
77 element = etree.SubElement(self.element, tagname, **kwds)
78 return XmlElement(element, self.namespaces)
80 def append(self, elem):
81 if isinstance(elem, XmlElement):
82 self.element.append(elem.element)
84 self.element.append(elem)
87 return XmlElement(self.element.getparent(), self.namespaces)
89 def get_instance(self, instance_class=None, fields=None):
91 Returns an instance (dict) of this xml element. The instance
92 holds a reference to this xml element.
96 if not instance_class:
97 instance_class = Element
98 if not fields and hasattr(instance_class, 'fields'):
99 fields = instance_class.fields
102 instance = instance_class(self.attrib, self)
104 instance = instance_class({}, self)
106 if field in self.attrib:
107 instance[field] = self.attrib[field]
110 def add_instance(self, name, instance, fields=None):
112 Adds the specifed instance(s) as a child element of this xml
117 if not fields and hasattr(instance, 'keys'):
118 fields = list(instance.keys())
119 elem = self.add_element(name)
121 if field in instance and instance[field]:
122 elem.set(field, str(instance[field]))
125 def remove_elements(self, name):
127 Removes all occurences of an element from the tree. Start at
128 specified root_node if specified, otherwise start at tree's root.
131 if not element_name.startswith('//'):
132 element_name = '//' + element_name
133 elements = self.element.xpath('%s ' % name, namespaces=self.namespaces)
134 for element in elements:
135 parent = element.getparent()
136 parent.remove(element)
139 parent = self.getparent()
142 def remove(self, element):
143 if isinstance(element, XmlElement):
144 self.element.remove(element.element)
146 self.element.remove(element)
148 def set_text(self, text):
149 self.element.text = text
151 # Element does not have unset ?!?
152 def unset(self, key):
153 del self.element.attrib[key]
156 return etree.tostring(self.element, encoding='UTF-8', pretty_print=True)
161 # other method calls or attribute access like .text or .tag or .get
162 # are redirected on self.element
163 def __getattr__(self, name):
164 if not hasattr(self.element, name):
165 raise AttributeError(name)
166 return getattr(self.element, name)
171 def __init__(self, xml=None, namespaces=None):
173 self.namespaces = namespaces
174 self.default_namespace = None
176 if isinstance(xml, str):
178 if isinstance(xml, XmlElement):
180 self.namespaces = xml.namespaces
181 elif isinstance(xml, etree._ElementTree) or isinstance(xml, etree._Element):
182 self.parse_xml(etree.tostring(xml))
184 def parse_xml(self, xml):
186 parse rspec into etree
188 parser = etree.XMLParser(remove_blank_text=True)
190 tree = etree.parse(xml, parser)
192 # 'rspec' file doesnt exist. 'rspec' is proably an xml string
194 tree = etree.parse(StringIO(xml), parser)
195 except Exception as e:
196 raise InvalidXML(str(e))
197 root = tree.getroot()
198 self.namespaces = dict(root.nsmap)
200 if 'default' not in self.namespaces and None in self.namespaces:
201 # If the 'None' exist, then it's pointing to the default namespace. This makes
202 # it hard for us to write xpath queries for the default naemspace because lxml
203 # wont understand a None prefix. We will just associate the default namespeace
204 # with a key named 'default'.
205 self.namespaces['default'] = self.namespaces.pop(None)
208 self.namespaces['default'] = 'default'
210 self.root = XmlElement(root, self.namespaces)
212 for key in list(self.root.attrib.keys()):
213 if key.endswith('schemaLocation'):
214 # schemaLocation should be at the end of the list.
215 # Use list comprehension to filter out empty strings
217 x for x in self.root.attrib[key].split(' ') if x]
218 self.schema = schema_parts[1]
219 namespace, schema = schema_parts[0], schema_parts[1]
222 def parse_dict(self, d, root_tag_name='xml', element=None):
224 if self.root is None:
225 self.parse_xml('<%s/>' % root_tag_name)
226 element = self.root.element
232 # handle repeating fields
233 for (key, value) in list(d.items()):
234 if isinstance(value, list):
237 if isinstance(val, dict):
238 child_element = etree.SubElement(element, key)
239 self.parse_dict(val, key, child_element)
240 elif isinstance(val, str):
241 child_element = etree.SubElement(
242 element, key).text = val
244 elif isinstance(value, int):
249 # element.attrib.update will explode if DateTimes are in the
252 # looks like iteritems won't stand side-effects
253 for k in list(d.keys()):
254 if not isinstance(d[k], str):
257 element.attrib.update(d)
259 def validate(self, schema):
261 Validate against rng schema
263 relaxng_doc = etree.parse(schema)
264 relaxng = etree.RelaxNG(relaxng_doc)
265 if not relaxng(self.root):
266 error = relaxng.error_log.last_error
267 message = "%s (line %s)" % (error.message, error.line)
268 raise InvalidXML(message)
271 def xpath(self, xpath, namespaces=None):
273 namespaces = self.namespaces
274 return self.root.xpath(xpath, namespaces=namespaces)
276 def set(self, key, value):
277 return self.root.set(key, value)
279 def remove_attribute(self, name, element=None):
282 element.remove_attribute(name)
284 def add_element(self, *args, **kwds):
286 Wrapper around etree.SubElement(). Adds an element to
287 specified parent node. Adds element to root node is parent is
290 return self.root.add_element(*args, **kwds)
292 def remove_elements(self, name, element=None):
294 Removes all occurences of an element from the tree. Start at
295 specified root_node if specified, otherwise start at tree's root.
300 element.remove_elements(name)
302 def add_instance(self, *args, **kwds):
303 return self.root.add_instance(*args, **kwds)
305 def get_instance(self, *args, **kwds):
306 return self.root.get_instnace(*args, **kwds)
308 def get_element_attributes(self, elem=None, depth=0):
311 if not hasattr(elem, 'attrib'):
312 # this is probably not an element node with attribute. could be just and an
313 # attribute, return it
315 attrs = dict(elem.attrib)
316 attrs['text'] = str(elem.text).strip()
317 attrs['parent'] = elem.getparent()
318 if isinstance(depth, int) and depth > 0:
319 for child_elem in list(elem):
320 key = str(child_elem.tag)
322 attrs[key] = [self.get_element_attributes(
323 child_elem, depth - 1)]
326 self.get_element_attributes(child_elem, depth - 1))
328 attrs['child_nodes'] = list(elem)
331 def append(self, elem):
332 return self.root.append(elem)
334 def iterchildren(self):
335 return self.root.iterchildren()
337 def merge(self, in_xml):
344 return etree.tostring(self.root.element, encoding='UTF-8', pretty_print=True)
346 # XXX smbaker, for record.load_from_string
347 def todict(self, elem=None):
351 d.update(elem.attrib)
352 d['text'] = elem.text
353 for child in elem.iterchildren():
354 if child.tag not in d:
356 d[child.tag].append(self.todict(child))
358 if len(d) == 1 and ("text" in d):
363 def save(self, filename):
364 f = open(filename, 'w')
365 f.write(self.toxml())
369 # if __name__ == '__main__':
370 # rspec = RSpec('/tmp/resources.rspec')