3 from sfa.util.faults import InvalidXML
4 from sfa.rspecs.elements.element import Element
6 from sfa.util.py23 import StringType
7 from sfa.util.py23 import StringIO
9 # helper functions to help build xpaths
13 def filter_value(key, value):
15 if isinstance(value, str):
17 value = value.replace('*', '')
18 xpath = 'contains(%s, "%s")' % (key, value)
20 xpath = '%s="%s"' % (key, value)
24 def xpath(filter=None):
25 if filter is None: filter={}
29 for (key, value) in filter.items():
34 if isinstance(value, str):
35 filter_list.append(XpathFilter.filter_value(key, value))
36 elif isinstance(value, list):
37 stmt = ' or '.join([XpathFilter.filter_value(key, str(val)) for val in value])
38 filter_list.append(stmt)
40 xpath = ' and '.join(filter_list)
41 xpath = '[' + xpath + ']'
44 # a wrapper class around lxml.etree._Element
45 # the reason why we need this one is because of the limitations
46 # we've found in xpath to address documents with multiple namespaces defined
47 # in a nutshell, we deal with xml documents that have
48 # a default namespace defined (xmlns="http://default.com/") and specific prefixes defined
49 # (xmlns:foo="http://foo.com")
50 # according to the documentation instead of writing
51 # element.xpath ( "//node/foo:subnode" )
52 # we'd then need to write xpaths like
53 # element.xpath ( "//{http://default.com/}node/{http://foo.com}subnode" )
54 # which is a real pain..
55 # So just so we can keep some reasonable programming style we need to manage the
56 # namespace map that goes with the _Element (its internal .nsmap being unmutable)
59 def __init__(self, element, namespaces):
60 self.element = element
61 self.namespaces = namespaces
63 # redefine as few methods as possible
64 def xpath(self, xpath, namespaces=None):
66 namespaces = self.namespaces
67 elems = self.element.xpath(xpath, namespaces=namespaces)
68 return [XmlElement(elem, namespaces) for elem in elems]
70 def add_element(self, tagname, **kwds):
71 element = etree.SubElement(self.element, tagname, **kwds)
72 return XmlElement(element, self.namespaces)
74 def append(self, elem):
75 if isinstance(elem, XmlElement):
76 self.element.append(elem.element)
78 self.element.append(elem)
81 return XmlElement(self.element.getparent(), self.namespaces)
83 def get_instance(self, instance_class=None, fields=None):
85 Returns an instance (dict) of this xml element. The instance
86 holds a reference to this xml element.
88 if fields is None: fields=[]
89 if not instance_class:
90 instance_class = Element
91 if not fields and hasattr(instance_class, 'fields'):
92 fields = instance_class.fields
95 instance = instance_class(self.attrib, self)
97 instance = instance_class({}, self)
99 if field in self.attrib:
100 instance[field] = self.attrib[field]
103 def add_instance(self, name, instance, fields=None):
105 Adds the specifed instance(s) as a child element of this xml
108 if fields is None: fields=[]
109 if not fields and hasattr(instance, 'keys'):
110 fields = instance.keys()
111 elem = self.add_element(name)
113 if field in instance and instance[field]:
114 elem.set(field, unicode(instance[field]))
117 def remove_elements(self, name):
119 Removes all occurences of an element from the tree. Start at
120 specified root_node if specified, otherwise start at tree's root.
123 if not element_name.startswith('//'):
124 element_name = '//' + element_name
125 elements = self.element.xpath('%s ' % name, namespaces=self.namespaces)
126 for element in elements:
127 parent = element.getparent()
128 parent.remove(element)
131 parent = self.getparent()
134 def remove(self, element):
135 if isinstance(element, XmlElement):
136 self.element.remove(element.element)
138 self.element.remove(element)
140 def set_text(self, text):
141 self.element.text = text
143 # Element does not have unset ?!?
144 def unset(self, key):
145 del self.element.attrib[key]
148 return etree.tostring(self.element, encoding='UTF-8', pretty_print=True)
153 ### other method calls or attribute access like .text or .tag or .get
154 # are redirected on self.element
155 def __getattr__ (self, name):
156 if not hasattr(self.element, name):
157 raise AttributeError(name)
158 return getattr(self.element, name)
162 def __init__(self, xml=None, namespaces=None):
164 self.namespaces = namespaces
165 self.default_namespace = None
167 if isinstance(xml, StringType):
169 if isinstance(xml, XmlElement):
171 self.namespaces = xml.namespaces
172 elif isinstance(xml, etree._ElementTree) or isinstance(xml, etree._Element):
173 self.parse_xml(etree.tostring(xml))
175 def parse_xml(self, xml):
177 parse rspec into etree
179 parser = etree.XMLParser(remove_blank_text=True)
181 tree = etree.parse(xml, parser)
183 # 'rspec' file doesnt exist. 'rspec' is proably an xml string
185 tree = etree.parse(StringIO(xml), parser)
186 except Exception as e:
187 raise InvalidXML(str(e))
188 root = tree.getroot()
189 self.namespaces = dict(root.nsmap)
191 if 'default' not in self.namespaces and None in self.namespaces:
192 # If the 'None' exist, then it's pointing to the default namespace. This makes
193 # it hard for us to write xpath queries for the default naemspace because lxml
194 # wont understand a None prefix. We will just associate the default namespeace
195 # with a key named 'default'.
196 self.namespaces['default'] = self.namespaces.pop(None)
199 self.namespaces['default'] = 'default'
201 self.root = XmlElement(root, self.namespaces)
203 for key in self.root.attrib.keys():
204 if key.endswith('schemaLocation'):
205 # schemaLocation should be at the end of the list.
206 # Use list comprehension to filter out empty strings
207 schema_parts = [x for x in self.root.attrib[key].split(' ') if x]
208 self.schema = schema_parts[1]
209 namespace, schema = schema_parts[0], schema_parts[1]
212 def parse_dict(self, d, root_tag_name='xml', element = None):
214 if self.root is None:
215 self.parse_xml('<%s/>' % root_tag_name)
216 element = self.root.element
222 # handle repeating fields
223 for (key, value) in d.items():
224 if isinstance(value, list):
227 if isinstance(val, dict):
228 child_element = etree.SubElement(element, key)
229 self.parse_dict(val, key, child_element)
230 elif isinstance(val, StringType):
231 child_element = etree.SubElement(element, key).text = val
233 elif isinstance(value, int):
234 d[key] = unicode(d[key])
238 # element.attrib.update will explode if DateTimes are in the
241 # looks like iteritems won't stand side-effects
243 if not isinstance(d[k], StringType):
246 element.attrib.update(d)
248 def validate(self, schema):
250 Validate against rng schema
252 relaxng_doc = etree.parse(schema)
253 relaxng = etree.RelaxNG(relaxng_doc)
254 if not relaxng(self.root):
255 error = relaxng.error_log.last_error
256 message = "%s (line %s)" % (error.message, error.line)
257 raise InvalidXML(message)
260 def xpath(self, xpath, namespaces=None):
262 namespaces = self.namespaces
263 return self.root.xpath(xpath, namespaces=namespaces)
265 def set(self, key, value):
266 return self.root.set(key, value)
268 def remove_attribute(self, name, element=None):
271 element.remove_attribute(name)
273 def add_element(self, *args, **kwds):
275 Wrapper around etree.SubElement(). Adds an element to
276 specified parent node. Adds element to root node is parent is
279 return self.root.add_element(*args, **kwds)
281 def remove_elements(self, name, element = None):
283 Removes all occurences of an element from the tree. Start at
284 specified root_node if specified, otherwise start at tree's root.
289 element.remove_elements(name)
291 def add_instance(self, *args, **kwds):
292 return self.root.add_instance(*args, **kwds)
294 def get_instance(self, *args, **kwds):
295 return self.root.get_instnace(*args, **kwds)
297 def get_element_attributes(self, elem=None, depth=0):
300 if not hasattr(elem, 'attrib'):
301 # this is probably not an element node with attribute. could be just and an
302 # attribute, return it
304 attrs = dict(elem.attrib)
305 attrs['text'] = str(elem.text).strip()
306 attrs['parent'] = elem.getparent()
307 if isinstance(depth, int) and depth > 0:
308 for child_elem in list(elem):
309 key = str(child_elem.tag)
311 attrs[key] = [self.get_element_attributes(child_elem, depth-1)]
313 attrs[key].append(self.get_element_attributes(child_elem, depth-1))
315 attrs['child_nodes'] = list(elem)
318 def append(self, elem):
319 return self.root.append(elem)
321 def iterchildren(self):
322 return self.root.iterchildren()
324 def merge(self, in_xml):
331 return etree.tostring(self.root.element, encoding='UTF-8', pretty_print=True)
333 # XXX smbaker, for record.load_from_string
334 def todict(self, elem=None):
338 d.update(elem.attrib)
339 d['text'] = elem.text
340 for child in elem.iterchildren():
341 if child.tag not in d:
343 d[child.tag].append(self.todict(child))
345 if len(d)==1 and ("text" in d):
350 def save(self, filename):
351 f = open(filename, 'w')
352 f.write(self.toxml())
356 #if __name__ == '__main__':
357 # rspec = RSpec('/tmp/resources.rspec')