2 from types import StringTypes
4 from StringIO import StringIO
5 from sfa.util.faults import InvalidXML
6 from sfa.rspecs.elements.element import Element
8 # helper functions to help build xpaths
12 def filter_value(key, value):
14 if isinstance(value, str):
16 value = value.replace('*', '')
17 xpath = 'contains(%s, "%s")' % (key, value)
19 xpath = '%s="%s"' % (key, value)
23 def xpath(filter=None):
24 if filter is None: filter={}
28 for (key, value) in filter.items():
33 if isinstance(value, str):
34 filter_list.append(XpathFilter.filter_value(key, value))
35 elif isinstance(value, list):
36 stmt = ' or '.join([XpathFilter.filter_value(key, str(val)) for val in value])
37 filter_list.append(stmt)
39 xpath = ' and '.join(filter_list)
40 xpath = '[' + xpath + ']'
43 # a wrapper class around lxml.etree._Element
44 # the reason why we need this one is because of the limitations
45 # we've found in xpath to address documents with multiple namespaces defined
46 # in a nutshell, we deal with xml documents that have
47 # a default namespace defined (xmlns="http://default.com/") and specific prefixes defined
48 # (xmlns:foo="http://foo.com")
49 # according to the documentation instead of writing
50 # element.xpath ( "//node/foo:subnode" )
51 # we'd then need to write xpaths like
52 # element.xpath ( "//{http://default.com/}node/{http://foo.com}subnode" )
53 # which is a real pain..
54 # So just so we can keep some reasonable programming style we need to manage the
55 # namespace map that goes with the _Element (its internal .nsmap being unmutable)
58 def __init__(self, element, namespaces):
59 self.element = element
60 self.namespaces = namespaces
62 # redefine as few methods as possible
63 def xpath(self, xpath, namespaces=None):
65 namespaces = self.namespaces
66 elems = self.element.xpath(xpath, namespaces=namespaces)
67 return [XmlElement(elem, namespaces) for elem in elems]
69 def add_element(self, tagname, **kwds):
70 element = etree.SubElement(self.element, tagname, **kwds)
71 return XmlElement(element, self.namespaces)
73 def append(self, elem):
74 if isinstance(elem, XmlElement):
75 self.element.append(elem.element)
77 self.element.append(elem)
80 return XmlElement(self.element.getparent(), self.namespaces)
82 def get_instance(self, instance_class=None, fields=None):
84 Returns an instance (dict) of this xml element. The instance
85 holds a reference to this xml element.
87 if fields is None: fields=[]
88 if not instance_class:
89 instance_class = Element
90 if not fields and hasattr(instance_class, 'fields'):
91 fields = instance_class.fields
94 instance = instance_class(self.attrib, self)
96 instance = instance_class({}, self)
98 if field in self.attrib:
99 instance[field] = self.attrib[field]
102 def add_instance(self, name, instance, fields=None):
104 Adds the specifed instance(s) as a child element of this xml
107 if fields is None: fields=[]
108 if not fields and hasattr(instance, 'keys'):
109 fields = instance.keys()
110 elem = self.add_element(name)
112 if field in instance and instance[field]:
113 elem.set(field, unicode(instance[field]))
116 def remove_elements(self, name):
118 Removes all occurences of an element from the tree. Start at
119 specified root_node if specified, otherwise start at tree's root.
122 if not element_name.startswith('//'):
123 element_name = '//' + element_name
124 elements = self.element.xpath('%s ' % name, namespaces=self.namespaces)
125 for element in elements:
126 parent = element.getparent()
127 parent.remove(element)
130 parent = self.getparent()
133 def remove(self, element):
134 if isinstance(element, XmlElement):
135 self.element.remove(element.element)
137 self.element.remove(element)
139 def set_text(self, text):
140 self.element.text = text
142 # Element does not have unset ?!?
143 def unset(self, key):
144 del self.element.attrib[key]
147 return etree.tostring(self.element, encoding='UTF-8', pretty_print=True)
152 ### other method calls or attribute access like .text or .tag or .get
153 # are redirected on self.element
154 def __getattr__ (self, name):
155 if not hasattr(self.element, name):
156 raise AttributeError, name
157 return getattr(self.element, name)
161 def __init__(self, xml=None, namespaces=None):
163 self.namespaces = namespaces
164 self.default_namespace = None
166 if isinstance(xml, basestring):
168 if isinstance(xml, XmlElement):
170 self.namespaces = xml.namespaces
171 elif isinstance(xml, etree._ElementTree) or isinstance(xml, etree._Element):
172 self.parse_xml(etree.tostring(xml))
174 def parse_xml(self, xml):
176 parse rspec into etree
178 parser = etree.XMLParser(remove_blank_text=True)
180 tree = etree.parse(xml, parser)
182 # 'rspec' file doesnt exist. 'rspec' is proably an xml string
184 tree = etree.parse(StringIO(xml), parser)
186 raise InvalidXML(str(e))
187 root = tree.getroot()
188 self.namespaces = dict(root.nsmap)
190 if 'default' not in self.namespaces and None in self.namespaces:
191 # If the 'None' exist, then it's pointing to the default namespace. This makes
192 # it hard for us to write xpath queries for the default naemspace because lxml
193 # wont understand a None prefix. We will just associate the default namespeace
194 # with a key named 'default'.
195 self.namespaces['default'] = self.namespaces.pop(None)
198 self.namespaces['default'] = 'default'
200 self.root = XmlElement(root, self.namespaces)
202 for key in self.root.attrib.keys():
203 if key.endswith('schemaLocation'):
204 # schemaLocation should be at the end of the list.
205 # Use list comprehension to filter out empty strings
206 schema_parts = [x for x in self.root.attrib[key].split(' ') if x]
207 self.schema = schema_parts[1]
208 namespace, schema = schema_parts[0], schema_parts[1]
211 def parse_dict(self, d, root_tag_name='xml', element = None):
213 if self.root is None:
214 self.parse_xml('<%s/>' % root_tag_name)
215 element = self.root.element
221 # handle repeating fields
222 for (key, value) in d.items():
223 if isinstance(value, list):
226 if isinstance(val, dict):
227 child_element = etree.SubElement(element, key)
228 self.parse_dict(val, key, child_element)
229 elif isinstance(val, basestring):
230 child_element = etree.SubElement(element, key).text = val
232 elif isinstance(value, int):
233 d[key] = unicode(d[key])
237 # element.attrib.update will explode if DateTimes are in the
240 # looks like iteritems won't stand side-effects
242 if not isinstance(d[k],StringTypes):
245 element.attrib.update(d)
247 def validate(self, schema):
249 Validate against rng schema
251 relaxng_doc = etree.parse(schema)
252 relaxng = etree.RelaxNG(relaxng_doc)
253 if not relaxng(self.root):
254 error = relaxng.error_log.last_error
255 message = "%s (line %s)" % (error.message, error.line)
256 raise InvalidXML(message)
259 def xpath(self, xpath, namespaces=None):
261 namespaces = self.namespaces
262 return self.root.xpath(xpath, namespaces=namespaces)
264 def set(self, key, value):
265 return self.root.set(key, value)
267 def remove_attribute(self, name, element=None):
270 element.remove_attribute(name)
272 def add_element(self, *args, **kwds):
274 Wrapper around etree.SubElement(). Adds an element to
275 specified parent node. Adds element to root node is parent is
278 return self.root.add_element(*args, **kwds)
280 def remove_elements(self, name, element = None):
282 Removes all occurences of an element from the tree. Start at
283 specified root_node if specified, otherwise start at tree's root.
288 element.remove_elements(name)
290 def add_instance(self, *args, **kwds):
291 return self.root.add_instance(*args, **kwds)
293 def get_instance(self, *args, **kwds):
294 return self.root.get_instnace(*args, **kwds)
296 def get_element_attributes(self, elem=None, depth=0):
299 if not hasattr(elem, 'attrib'):
300 # this is probably not an element node with attribute. could be just and an
301 # attribute, return it
303 attrs = dict(elem.attrib)
304 attrs['text'] = str(elem.text).strip()
305 attrs['parent'] = elem.getparent()
306 if isinstance(depth, int) and depth > 0:
307 for child_elem in list(elem):
308 key = str(child_elem.tag)
310 attrs[key] = [self.get_element_attributes(child_elem, depth-1)]
312 attrs[key].append(self.get_element_attributes(child_elem, depth-1))
314 attrs['child_nodes'] = list(elem)
317 def append(self, elem):
318 return self.root.append(elem)
320 def iterchildren(self):
321 return self.root.iterchildren()
323 def merge(self, in_xml):
330 return etree.tostring(self.root.element, encoding='UTF-8', pretty_print=True)
332 # XXX smbaker, for record.load_from_string
333 def todict(self, elem=None):
337 d.update(elem.attrib)
338 d['text'] = elem.text
339 for child in elem.iterchildren():
340 if child.tag not in d:
342 d[child.tag].append(self.todict(child))
344 if len(d)==1 and ("text" in d):
349 def save(self, filename):
350 f = open(filename, 'w')
351 f.write(self.toxml())
355 #if __name__ == '__main__':
356 # rspec = RSpec('/tmp/resources.rspec')