9f43b96f8380b62f5c6beef9b70fbad061bd25bb
[sfa.git] / sfa / util / xml.py
1 #!/usr/bin/env python3
2 from lxml import etree
3 from sfa.util.faults import InvalidXML
4 from sfa.rspecs.elements.element import Element
5
6 from io import StringIO
7
8 # helper functions to help build xpaths
9
10
11 class XpathFilter:
12
13     @staticmethod
14     def filter_value(key, value):
15         xpath = ""
16         if isinstance(value, str):
17             if '*' in value:
18                 value = value.replace('*', '')
19                 xpath = 'contains(%s, "%s")' % (key, value)
20             else:
21                 xpath = '%s="%s"' % (key, value)
22         return xpath
23
24     @staticmethod
25     def xpath(filter=None):
26         if filter is None:
27             filter = {}
28         xpath = ""
29         if filter:
30             filter_list = []
31             for (key, value) in list(filter.items()):
32                 if key == 'text':
33                     key = 'text()'
34                 else:
35                     key = '@' + key
36                 if isinstance(value, str):
37                     filter_list.append(XpathFilter.filter_value(key, value))
38                 elif isinstance(value, list):
39                     stmt = ' or '.join(
40                         [XpathFilter.filter_value(key, str(val)) for val in value])
41                     filter_list.append(stmt)
42             if filter_list:
43                 xpath = ' and '.join(filter_list)
44                 xpath = '[' + xpath + ']'
45         return xpath
46
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
60 # unmutable)
61
62
63 class XmlElement:
64
65     def __init__(self, element, namespaces):
66         self.element = element
67         self.namespaces = namespaces
68
69     # redefine as few methods as possible
70     def xpath(self, xpath, namespaces=None):
71         if not namespaces:
72             namespaces = self.namespaces
73         elems = self.element.xpath(xpath, namespaces=namespaces)
74         return [XmlElement(elem, namespaces) for elem in elems]
75
76     def add_element(self, tagname, **kwds):
77         element = etree.SubElement(self.element, tagname, **kwds)
78         return XmlElement(element, self.namespaces)
79
80     def append(self, elem):
81         if isinstance(elem, XmlElement):
82             self.element.append(elem.element)
83         else:
84             self.element.append(elem)
85
86     def getparent(self):
87         return XmlElement(self.element.getparent(), self.namespaces)
88
89     def get_instance(self, instance_class=None, fields=None):
90         """
91         Returns an instance (dict) of this xml element. The instance
92         holds a reference to this xml element.   
93         """
94         if fields is None:
95             fields = []
96         if not instance_class:
97             instance_class = Element
98         if not fields and hasattr(instance_class, 'fields'):
99             fields = instance_class.fields
100
101         if not fields:
102             instance = instance_class(self.attrib, self)
103         else:
104             instance = instance_class({}, self)
105             for field in fields:
106                 if field in self.attrib:
107                     instance[field] = self.attrib[field]
108         return instance
109
110     def add_instance(self, name, instance, fields=None):
111         """
112         Adds the specifed instance(s) as a child element of this xml 
113         element. 
114         """
115         if fields is None:
116             fields = []
117         if not fields and hasattr(instance, 'keys'):
118             fields = list(instance.keys())
119         elem = self.add_element(name)
120         for field in fields:
121             if field in instance and instance[field]:
122                 elem.set(field, str(instance[field]))
123         return elem
124
125     def remove_elements(self, name):
126         """
127         Removes all occurences of an element from the tree. Start at
128         specified root_node if specified, otherwise start at tree's root.
129         """
130
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)
137
138     def delete(self):
139         parent = self.getparent()
140         parent.remove(self)
141
142     def remove(self, element):
143         if isinstance(element, XmlElement):
144             self.element.remove(element.element)
145         else:
146             self.element.remove(element)
147
148     def set_text(self, text):
149         self.element.text = text
150
151     # Element does not have unset ?!?
152     def unset(self, key):
153         del self.element.attrib[key]
154
155     def toxml(self):
156         return etree.tostring(self.element, encoding='UTF-8', pretty_print=True)
157
158     def __str__(self):
159         return self.toxml()
160
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)
167
168
169 class XML:
170
171     def __init__(self, xml=None, namespaces=None):
172         self.root = None
173         self.namespaces = namespaces
174         self.default_namespace = None
175         self.schema = None
176         if isinstance(xml, str):
177             self.parse_xml(xml)
178         if isinstance(xml, XmlElement):
179             self.root = xml
180             self.namespaces = xml.namespaces
181         elif isinstance(xml, etree._ElementTree) or isinstance(xml, etree._Element):
182             self.parse_xml(etree.tostring(xml))
183
184     def parse_xml(self, xml):
185         """
186         parse rspec into etree
187         """
188         parser = etree.XMLParser(remove_blank_text=True)
189         try:
190             tree = etree.parse(xml, parser)
191         except IOError:
192             # 'rspec' file doesnt exist. 'rspec' is proably an xml string
193             try:
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)
199         # set namespaces map
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)
206
207         else:
208             self.namespaces['default'] = 'default'
209
210         self.root = XmlElement(root, self.namespaces)
211         # set schema
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
216                 schema_parts = [
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]
220                 break
221
222     def parse_dict(self, d, root_tag_name='xml', element=None):
223         if element is None:
224             if self.root is None:
225                 self.parse_xml('<%s/>' % root_tag_name)
226             element = self.root.element
227
228         if 'text' in d:
229             text = d.pop('text')
230             element.text = text
231
232         # handle repeating fields
233         for (key, value) in list(d.items()):
234             if isinstance(value, list):
235                 value = d.pop(key)
236                 for val in value:
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
243
244             elif isinstance(value, int):
245                 d[key] = str(d[key])
246             elif value is None:
247                 d.pop(key)
248
249         # element.attrib.update will explode if DateTimes are in the
250         # dcitionary.
251         d = d.copy()
252         # looks like iteritems won't stand side-effects
253         for k in list(d.keys()):
254             if not isinstance(d[k], str):
255                 del d[k]
256
257         element.attrib.update(d)
258
259     def validate(self, schema):
260         """
261         Validate against rng schema
262         """
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)
269         return True
270
271     def xpath(self, xpath, namespaces=None):
272         if not namespaces:
273             namespaces = self.namespaces
274         return self.root.xpath(xpath, namespaces=namespaces)
275
276     def set(self, key, value):
277         return self.root.set(key, value)
278
279     def remove_attribute(self, name, element=None):
280         if not element:
281             element = self.root
282         element.remove_attribute(name)
283
284     def add_element(self, *args, **kwds):
285         """
286         Wrapper around etree.SubElement(). Adds an element to 
287         specified parent node. Adds element to root node is parent is 
288         not specified. 
289         """
290         return self.root.add_element(*args, **kwds)
291
292     def remove_elements(self, name, element=None):
293         """
294         Removes all occurences of an element from the tree. Start at 
295         specified root_node if specified, otherwise start at tree's root.   
296         """
297         if not element:
298             element = self.root
299
300         element.remove_elements(name)
301
302     def add_instance(self, *args, **kwds):
303         return self.root.add_instance(*args, **kwds)
304
305     def get_instance(self, *args, **kwds):
306         return self.root.get_instnace(*args, **kwds)
307
308     def get_element_attributes(self, elem=None, depth=0):
309         if elem == None:
310             elem = self.root
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
314             return elem
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)
321                 if key not in attrs:
322                     attrs[key] = [self.get_element_attributes(
323                         child_elem, depth - 1)]
324                 else:
325                     attrs[key].append(
326                         self.get_element_attributes(child_elem, depth - 1))
327         else:
328             attrs['child_nodes'] = list(elem)
329         return attrs
330
331     def append(self, elem):
332         return self.root.append(elem)
333
334     def iterchildren(self):
335         return self.root.iterchildren()
336
337     def merge(self, in_xml):
338         pass
339
340     def __str__(self):
341         return self.toxml()
342
343     def toxml(self):
344         return etree.tostring(self.root.element, encoding='UTF-8', pretty_print=True)
345
346     # XXX smbaker, for record.load_from_string
347     def todict(self, elem=None):
348         if elem is None:
349             elem = self.root
350         d = {}
351         d.update(elem.attrib)
352         d['text'] = elem.text
353         for child in elem.iterchildren():
354             if child.tag not in d:
355                 d[child.tag] = []
356             d[child.tag].append(self.todict(child))
357
358         if len(d) == 1 and ("text" in d):
359             d = d["text"]
360
361         return d
362
363     def save(self, filename):
364         f = open(filename, 'w')
365         f.write(self.toxml())
366         f.close()
367
368 # no RSpec in scope
369 # if __name__ == '__main__':
370 #    rspec = RSpec('/tmp/resources.rspec')
371 #    print rspec