fix for {"text":} dictionaries in record fields
[sfa.git] / sfa / util / xml.py
index 06d61c9..d880ed3 100755 (executable)
@@ -55,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():
@@ -97,8 +96,15 @@ class XML:
             elif isinstance(value, int):
                 d[key] = unicode(d[key])  
             elif value is None:
-                d.pop(key)          
-             
+                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,7 +226,24 @@ class XML:
             if child.tag not in d:
                 d[child.tag] = []
             d[child.tag].append(self.todict(child))
-        return d            
+        return d
+
+    # XXX smbaker, for record.load_from_string
+    def todict2(self, elem=None):
+        if elem is None:
+            elem = self.root
+        d = {}
+        d.update(elem.attrib)
+        d['text'] = elem.text
+        for child in elem.iterchildren():
+            if child.tag not in d:
+                d[child.tag] = []
+            d[child.tag].append(self.todict2(child))
+
+        if len(d)==1 and ("text" in d):
+            d = d["text"]
+
+        return d
         
     def save(self, filename):
         f = open(filename, 'w')