fix error with datetime structs in parse_dict
[sfa.git] / sfa / util / xml.py
old mode 100755 (executable)
new mode 100644 (file)
index e79fd47..89eef9e
@@ -1,10 +1,8 @@
 #!/usr/bin/python 
 from lxml import etree
 from StringIO import StringIO
-from datetime import datetime, timedelta
-from sfa.util.xrn import *
-from sfa.util.plxrn import hostname_to_urn
-from sfa.util.faults import SfaNotImplemented, InvalidXML
+
+from sfa.util.faults import InvalidXML
 
 class XpathFilter:
     @staticmethod
@@ -57,14 +55,13 @@ class XML:
         # set namespaces map
         self.namespaces = dict(self.root.nsmap)
         if 'default' not in self.namespaces and None in self.namespaces: 
+            # If the 'None' exist, then it's pointing to the default namespace. This makes 
+            # it hard for us to write xpath queries for the default naemspace because lxml 
+            # wont understand a None prefix. We will just associate the default namespeace 
+            # with a key named 'default'.     
             self.namespaces['default'] = self.namespaces[None]
-        # If the 'None' exist, then it's pointing to the default namespace. This makes 
-        # it hard for us to write xpath queries for the default naemspace because lxml 
-        # wont understand a None prefix. We will just associate the default namespeace 
-        # with a key named 'default'.     
-        if None in self.namespaces:
-            default_namespace = self.namespaces.pop(None)
-            self.namespaces['default'] = default_namespace
+        else:
+            self.namespaces['default'] = 'default' 
 
         # set schema 
         for key in self.root.attrib.keys():
@@ -77,7 +74,8 @@ class XML:
 
     def parse_dict(self, d, root_tag_name='xml', element = None):
         if element is None: 
-            self.parse_xml('<%s/>' % root_tag_name)
+            if self.root is None:
+                self.parse_xml('<%s/>' % root_tag_name)
             element = self.root
 
         if 'text' in d:
@@ -91,8 +89,22 @@ class XML:
                 for val in value:
                     if isinstance(val, dict):
                         child_element = etree.SubElement(element, key)
-                        self.parse_dict(val, key, child_element) 
-        
+                        self.parse_dict(val, key, child_element)
+                    elif isinstance(val, basestring):
+                        child_element = etree.SubElement(element, key).text = val
+                        
+            elif isinstance(value, int):
+                d[key] = unicode(d[key])  
+            elif value is None:
+                d.pop(key)
+
+        # element.attrib.update will explode if DateTimes are in the
+        # dcitionary.
+        d=d.copy()
+        for k in d.keys():
+            if (type(d[k]) != str) and (type(d[k]) != unicode):
+                del d[k]
+
         element.attrib.update(d)
 
     def validate(self, schema):
@@ -220,8 +232,9 @@ class XML:
         f = open(filename, 'w')
         f.write(self.toxml())
         f.close()
-if __name__ == '__main__':
-    rspec = RSpec('/tmp/resources.rspec')
-    print rspec
+
+# no RSpec in scope 
+#if __name__ == '__main__':
+#    rspec = RSpec('/tmp/resources.rspec')
+#    print rspec