in RecordSpec.dict2dom, no need to always hardcode add the root_tag, it may already...
[sfa.git] / geni / util / rspec.py
index 6c44074..393d8df 100644 (file)
@@ -79,19 +79,26 @@ class Rspec():
         if elementName and not elementName.startswith("#"):
             # attributes have tags and values.  get {tag: value}, else {type: value}
             node[elementName] = self._attributeDict(nodeDom)
-            #node.update(self._attributeDict(nodeDom))
             # resolve the child nodes.
             if nodeDom.hasChildNodes():
                 for child in nodeDom.childNodes:
                     childName = self._getName(child)
+                    # skip null children 
                     if not childName:
                         continue
+                    # initialize the possible array of children        
                     if not node[elementName].has_key(childName):
-                        node[elementName][childName] = []       
-                        #node[childName] = []
-                    childdict = self.toDict(child)
-                    for value in childdict.values():
-                        node[elementName][childName].append(value)
+                        node[elementName][childName] = []
+                    # if child node has text child nodes
+                    # append the children to the array as strings
+                    if child.hasChildNodes() and isinstance(child.childNodes[0], minidom.Text):
+                        for nextchild in child.childNodes:
+                            node[elementName][childName].append(nextchild.data)
+                    # convert element child node to dict
+                    else:       
+                        childdict = self.toDict(child)
+                        for value in childdict.values():
+                            node[elementName][childName].append(value)
                     #node[childName].append(self.toDict(child))
         return node
 
@@ -178,15 +185,22 @@ class Rspec():
         def elementNode(tagname, rd):
             element = minidom.Element(tagname)
             for key in rd.keys():
-                if isinstance(rd[key], StringTypes):
-                    element.setAttribute(key, rd[key])
+                if isinstance(rd[key], StringTypes) or isinstance(rd[key], int):
+                    element.setAttribute(key, str(rd[key]))
                 elif isinstance(rd[key], dict):
                     child = elementNode(key, rd[key])
                     element.appendChild(child)
                 elif isinstance(rd[key], list):
                     for item in rd[key]:
-                        child = elementNode(key, item)
-                        element.appendChild(child)
+                        if isinstance(item, dict):
+                            child = elementNode(key, item)
+                            element.appendChild(child)
+                        elif isinstance(item, StringTypes) or isinstance(item, int):
+                            child = minidom.Element(key)
+                            text = minidom.Text()
+                            text.data = item
+                            child.appendChild(text)
+                            element.appendChild(child) 
             return element
         
         # Minidom does not allow documents to have more then one
@@ -264,12 +278,31 @@ class Rspec():
                 self.filter(tagname, attribute, blacklist, whitelist, child) 
 
 
-       def validateDicts(self):
-               types = {
-                       'EInt' : int,
-                       'EString' : str,
-                       'EByteArray' : list,
-                       'EBoolean' : bool,
-                       'EFloat' : float,
-                       'EDate' : date}
+    def validateDicts(self):
+        types = {
+            'EInt' : int,
+            'EString' : str,
+            'EByteArray' : list,
+            'EBoolean' : bool,
+            'EFloat' : float,
+            'EDate' : date}
+
+
+
+class RecordSpec(Rspec):
+
+    root_tag = 'record'
+    def parseDict(self, rdict, include_doc = False):
+        """
+        Convert a dictionary into a dom object and store it.
+        """
+        self.rootNode = self.dict2dom(rdict, include_doc)
+
+    def dict2dom(self, rdict, include_doc = False):
+        record_dict = rdict
+        if not len(rdict.keys()) == 1:
+            record_dict = {self.root_tag : rdict}
+        return Rspec.dict2dom(self, record_dict, include_doc)
+        
 # vim:ts=4:expandtab
+