initial checkin of merge method
[sfa.git] / sfa / util / rspec.py
index 1cd7edc..515dd86 100644 (file)
@@ -8,7 +8,7 @@ import httplib
 from xml.dom import minidom
 from types import StringTypes, ListType
 
-class Rspec:
+class RSpec:
 
     def __init__(self, xml = None, xsd = None, NSURL = None):
         '''
@@ -77,7 +77,7 @@ class Rspec:
             dict[key]=[value]
         return dict
 
-    def toGenDict(self, nodeDom=None, parentdict={}, siblingdict={}, parent=None):
+    def toGenDict(self, nodeDom=None, parentdict=None, siblingdict={}, parent=None):
         """
         convert an XML to a nested dict:
           * Non-terminal nodes (elements with string children and attributes) are simple dictionaries
@@ -89,17 +89,32 @@ class Rspec:
 
         curNodeName = nodeDom.localName
 
-        if (nodeDom.nodeValue):
-            siblingdict = self.appendToDictOrCreate(siblingdict, parent, nodeDom.nodeValue)
-        elif (nodeDom.hasChildNodes()):
-            for child in nodeDom.childNodes:
-                 siblingdict = self.toGenDict(child, None, siblingdict,curNodeName)
-
+        if (nodeDom.hasChildNodes()):
+            childdict={}
+            for attribute in nodeDom.attributes.keys():
+                childdict = self.appendToDictOrCreate(childdict, attribute, nodeDom.getAttribute(attribute))
+            for child in nodeDom.childNodes[:-1]:
+                if (child.nodeValue):
+                    siblingdict = self.appendToDictOrCreate(siblingdict, curNodeName, child.nodeValue)
+                else:
+                    childdict = self.toGenDict(child, None, childdict, curNodeName)
+
+            child = nodeDom.childNodes[-1]
+            if (child.nodeValue):
+                siblingdict = self.appendToDictOrCreate(siblingdict, curNodeName, child.nodeValue)
+                if (childdict):
+                    siblingdict = self.appendToDictOrCreate(siblingdict, curNodeName, childdict)
+            else:
+                siblingdict = self.toGenDict(child, siblingdict, childdict, curNodeName)
+        else:
+            childdict={}
             for attribute in nodeDom.attributes.keys():
-                parentdict = self.appendToDictOrCreate(parentdict, curNodeName, nodeDom.getAttribute(attribute))
+                childdict = self.appendToDictOrCreate(childdict, attribute, nodeDom.getAttribute(attribute))
 
+            self.appendToDictOrCreate(siblingdict, curNodeName, childdict)
+            
         if (parentdict is not None):
-            parentdict = self.appendToDictOrCreate(parentdict, curNodeName, siblingdict)
+            parentdict = self.appendToDictOrCreate(parentdict, parent, siblingdict)
             return parentdict
         else:
             return siblingdict
@@ -168,7 +183,7 @@ class Rspec:
         """
         read an xml string and store it as a dom object.
         """
-        xml = xml.replace('\n', '').replace('\t', '').replace(' ', '').strip()
+        xml = xml.replace('\n', '').replace('\t', '').strip()
         dom = minidom.parseString(xml)
         self.rootNode = dom.childNodes[0]
 
@@ -259,7 +274,7 @@ class Rspec:
         """
         Convert a dictionary into a dom object and store it.
         """
-        self.rootNode = self.dict2dom(rdict, include_doc)
+        self.rootNode = self.dict2dom(rdict, include_doc).childNodes[0]
  
  
     def getDictsByTagName(self, tagname, dom = None):
@@ -317,6 +332,28 @@ class Rspec:
                 self.filter(tagname, attribute, blacklist, whitelist, child) 
 
 
+    def merge(self, rspecs, tagname, dom=None):
+        """
+        Merge this rspec with the requested rspec based on the specified 
+        starting tag name. The start tag (and all of its children) will be merged  
+        """
+        tempdict = {}
+        if not dom:
+            dom = self.rootNode
+
+        whitelist = []
+        blacklist = []
+            
+        if dom.localName in [tagname] and dom.attributes.has_key(attribute):
+            if whitelist and dom.attributes.get(attribute).value not in whitelist:
+                dom.parentNode.removeChild(dom)
+            if blacklist and dom.attributes.get(attribute).value in blacklist:
+                dom.parentNode.removeChild(dom)
+
+        if dom.hasChildNodes():
+            for child in dom.childNodes:
+                self.filter(tagname, attribute, blacklist, whitelist, child) 
+
     def validateDicts(self):
         types = {
             'EInt' : int,
@@ -348,7 +385,7 @@ class Rspec:
     
 
 
-class RecordSpec(Rspec):
+class RecordSpec(RSpec):
 
     root_tag = 'record'
     def parseDict(self, rdict, include_doc = False):
@@ -361,7 +398,7 @@ class RecordSpec(Rspec):
         record_dict = rdict
         if not len(rdict.keys()) == 1:
             record_dict = {self.root_tag : rdict}
-        return Rspec.dict2dom(self, record_dict, include_doc)
+        return RSpec.dict2dom(self, record_dict, include_doc)
 
         
 # vim:ts=4:expandtab