3 from StringIO import StringIO
4 from datetime import datetime, timedelta
5 from sfa.util.xrn import *
6 from sfa.util.plxrn import hostname_to_urn
7 from sfa.util.faults import SfaNotImplemented, InvalidXML
15 for (key, value) in filter.items():
20 if isinstance(value, str):
21 filter_list.append('%s="%s"' % (key, value))
22 elif isinstance(value, list):
23 filter_list.append('contains("%s", %s)' % (' '.join(map(str, value)), key))
25 xpath = ' and '.join(filter_list)
26 xpath = '[' + xpath + ']'
31 def __init__(self, xml=None):
33 self.namespaces = None
34 self.default_namespace = None
36 if isinstance(xml, basestring):
38 elif isinstance(xml, etree._ElementTree):
39 self.root = xml.getroot()
40 elif isinstance(xml, etree._Element):
43 def parse_xml(self, xml):
45 parse rspec into etree
47 parser = etree.XMLParser(remove_blank_text=True)
49 tree = etree.parse(xml, parser)
51 # 'rspec' file doesnt exist. 'rspec' is proably an xml string
53 tree = etree.parse(StringIO(xml), parser)
55 raise InvalidXML(str(e))
56 self.root = tree.getroot()
58 self.namespaces = dict(self.root.nsmap)
59 if 'default' not in self.namespaces and None in self.namespaces:
60 self.namespaces['default'] = self.namespaces[None]
61 # If the 'None' exist, then it's pointing to the default namespace. This makes
62 # it hard for us to write xpath queries for the default naemspace because lxml
63 # wont understand a None prefix. We will just associate the default namespeace
64 # with a key named 'default'.
65 if None in self.namespaces:
66 default_namespace = self.namespaces.pop(None)
67 self.namespaces['default'] = default_namespace
70 for key in self.root.attrib.keys():
71 if key.endswith('schemaLocation'):
72 # schema location should be at the end of the list
73 schema_parts = self.root.attrib[key].split(' ')
74 self.schema = schema_parts[1]
75 namespace, schema = schema_parts[0], schema_parts[1]
78 def parse_dict(self, d, root_tag_name='xml', element = None):
80 self.parse_xml('<%s/>' % root_tag_name)
87 # handle repeating fields
88 for (key, value) in d.items():
89 if isinstance(value, list):
92 if isinstance(val, dict):
93 child_element = etree.SubElement(element, key)
94 self.parse_dict(val, key, child_element)
96 element.attrib.update(d)
98 def validate(self, schema):
100 Validate against rng schema
102 relaxng_doc = etree.parse(schema)
103 relaxng = etree.RelaxNG(relaxng_doc)
104 if not relaxng(self.root):
105 error = relaxng.error_log.last_error
106 message = "%s (line %s)" % (error.message, error.line)
107 raise InvalidXML(message)
110 def xpath(self, xpath, namespaces=None):
112 namespaces = self.namespaces
113 return self.root.xpath(xpath, namespaces=namespaces)
115 def set(self, key, value):
116 return self.root.set(key, value)
118 def add_attribute(self, elem, name, value):
120 Add attribute to specified etree element
122 opt = etree.SubElement(elem, name)
125 def add_element(self, name, attrs={}, parent=None, text=""):
127 Generic wrapper around etree.SubElement(). Adds an element to
128 specified parent node. Adds element to root node is parent is
133 element = etree.SubElement(parent, name)
136 if isinstance(attrs, dict):
138 element.set(attr, attrs[attr])
141 def remove_attribute(self, elem, name, value):
143 Removes an attribute from an element
146 opts = elem.iterfind(name)
149 if opt.text == value:
152 def remove_element(self, element_name, root_node = None):
154 Removes all occurences of an element from the tree. Start at
155 specified root_node if specified, otherwise start at tree's root.
158 root_node = self.root
160 if not element_name.startswith('//'):
161 element_name = '//' + element_name
163 elements = root_node.xpath('%s ' % element_name, namespaces=self.namespaces)
164 for element in elements:
165 parent = element.getparent()
166 parent.remove(element)
168 def attributes_list(self, elem):
169 # convert a list of attribute tags into list of tuples
170 # (tagnme, text_value)
174 opts.append((e.tag, str(e.text).strip()))
177 def get_element_attributes(self, elem=None, depth=0):
179 elem = self.root_node
180 if not hasattr(elem, 'attrib'):
181 # this is probably not an element node with attribute. could be just and an
182 # attribute, return it
184 attrs = dict(elem.attrib)
185 attrs['text'] = str(elem.text).strip()
186 attrs['parent'] = elem.getparent()
187 if isinstance(depth, int) and depth > 0:
188 for child_elem in list(elem):
189 key = str(child_elem.tag)
191 attrs[key] = [self.get_element_attributes(child_elem, depth-1)]
193 attrs[key].append(self.get_element_attributes(child_elem, depth-1))
195 attrs['child_nodes'] = list(elem)
198 def merge(self, in_xml):
205 return etree.tostring(self.root, encoding='UTF-8', pretty_print=True)
207 def todict(self, elem=None):
211 d.update(elem.attrib)
212 d['text'] = elem.text
213 for child in elem.iterchildren():
214 if child.tag not in d:
216 d[child.tag].append(self.todict(child))
219 def save(self, filename):
220 f = open(filename, 'w')
221 f.write(self.toxml())
224 if __name__ == '__main__':
225 rspec = RSpec('/tmp/resources.rspec')