initial checkin of xml_interface
[sfa.git] / sfa / rspecs / xml_interface.py
1 #!/usr/bin/python 
2 from lxml import etree
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, InvalidRSpec, InvalidRSpecElement
8
9 class XpathFilter:
10     @staticmethod
11     def xpath(filter={}):
12         xpath = ""
13         if filter:
14             filter_list = []
15             for (key, value) in filter.items():
16                 if key == 'text':
17                     key = 'text()'
18                 else:
19                     key = '@'+key
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))
24             if filter_list:
25                 xpath = ' and '.join(filter_list)
26                 xpath = '[' + xpath + ']'
27         return xpath
28
29 class XMLInterface:
30  
31     def __init__(self, xml="", namespaces={}):
32         self.header = '<?xml version="1.0"?>\n'
33         self.template = """<RSpec></RSpec>"""
34         self.xml = None
35         self.namespaces = namespaces
36         if xml:
37             self.parse_xml(xml)
38         else:
39             self.create()
40
41     def create(self):
42         """
43         Create root element
44         """
45         self.parse_rspec(self.template, self.namespaces)
46     
47     def parse_xml(self, xml):
48         """
49         parse rspec into etree
50         """
51         parser = etree.XMLParser(remove_blank_text=True)
52         try:
53             tree = etree.parse(xml, parser)
54         except IOError:
55             # 'rspec' file doesnt exist. 'rspec' is proably an xml string
56             try:
57                 tree = etree.parse(StringIO(xml), parser)
58             except Exception, e:
59                 raise InvalidRSpec(str(e))
60         self.xml = tree.getroot()  
61
62     def validate(self, schema):
63         """
64         Validate against rng schema
65         """
66
67         relaxng_doc = etree.parse(schema)
68         relaxng = etree.RelaxNG(relaxng_doc)
69         if not relaxng(self.xml):
70             error = relaxng.error_log.last_error
71             message = "%s (line %s)" % (error.message, error.line)
72             raise InvalidRSpec(message)
73         return True
74
75     def xpath(self, xpath):
76         return self.xml.xpath(xpath, namespaces=self.namespaces)
77
78     def add_attribute(self, elem, name, value):
79         """
80         Add attribute to specified etree element    
81         """
82         opt = etree.SubElement(elem, name)
83         opt.text = value
84
85     def add_element(self, name, attrs={}, parent=None, text=""):
86         """
87         Generic wrapper around etree.SubElement(). Adds an element to 
88         specified parent node. Adds element to root node is parent is 
89         not specified. 
90         """
91         if parent == None:
92             parent = self.xml
93         element = etree.SubElement(parent, name)
94         if text:
95             element.text = text
96         if isinstance(attrs, dict):
97             for attr in attrs:
98                 element.set(attr, attrs[attr])  
99         return element
100
101     def remove_attribute(self, elem, name, value):
102         """
103         Removes an attribute from an element
104         """
105         if elem is not None:
106             opts = elem.iterfind(name)
107             if opts is not None:
108                 for opt in opts:
109                     if opt.text == value:
110                         elem.remove(opt)
111
112     def remove_element(self, element_name, root_node = None):
113         """
114         Removes all occurences of an element from the tree. Start at 
115         specified root_node if specified, otherwise start at tree's root.   
116         """
117         if not root_node:
118             root_node = self.xml
119
120         if not element_name.startswith('//'):
121             element_name = '//' + element_name
122
123         elements = root_node.xpath('%s ' % element_name, namespaces=self.namespaces)
124         for element in elements:
125             parent = element.getparent()
126             parent.remove(element)
127
128     def attributes_list(self, elem):
129         # convert a list of attribute tags into list of tuples
130         # (tagnme, text_value)
131         opts = []
132         if elem is not None:
133             for e in elem:
134                 opts.append((e.tag, str(e.text).strip()))
135         return opts
136
137     def get_element_attributes(self, elem=None, depth=0):
138         if elem == None:
139             elem = self.root_node
140         if not hasattr(elem, 'attrib'):
141             # this is probably not an element node with attribute. could be just and an
142             # attribute, return it
143             return elem
144         attrs = dict(elem.attrib)
145         attrs['text'] = str(elem.text).strip()
146         attrs['parent'] = elem.getparent()
147         if isinstance(depth, int) and depth > 0:
148             for child_elem in list(elem):
149                 key = str(child_elem.tag)
150                 if key not in attrs:
151                     attrs[key] = [self.get_element_attributes(child_elem, depth-1)]
152                 else:
153                     attrs[key].append(self.get_element_attributes(child_elem, depth-1))
154         else:
155             attrs['child_nodes'] = list(elem)
156         return attrs
157
158     def merge(self, in_xml):
159         pass
160
161     def cleanup(self):
162         """
163         Optional method which inheriting classes can choose to implent. 
164         """
165         pass 
166
167     def __str__(self):
168         return self.toxml()
169
170     def toxml(self, cleanup=False):
171         if cleanup:
172             self.cleanup()
173         return self.header + etree.tostring(self.xml, pretty_print=True)  
174         
175     def save(self, filename):
176         f = open(filename, 'w')
177         f.write(self.toxml())
178         f.close()
179  
180 if __name__ == '__main__':
181     rspec = RSpec('/tmp/resources.rspec')
182     print rspec
183