diff tools/memory.py @ 19:f2a745ca0fbc

refactoring: using xml params part III (parameters import)
author Goffi <goffi@goffi.org>
date Sat, 07 Nov 2009 21:23:28 +0100
parents 6928e3cb73a8
children fc8c202cda87
line wrap: on
line diff
--- a/tools/memory.py	Fri Nov 06 23:31:00 2009 +0100
+++ b/tools/memory.py	Sat Nov 07 21:23:28 2009 +0100
@@ -33,6 +33,7 @@
 
 class Param():
     """This class manage parameter with xml"""
+    ### TODO: add desciption in params
     
     #TODO: mettre Watched dans un plugin
     default_xml = """
@@ -42,10 +43,6 @@
         <param name="Password" value="toto" type="password" />
         <param name="Server" value="necton2.int" type="string" />
     </category>
-    <category name="File Transfert">
-        <param name="IP" value="192.168.0.10" type="string" />
-        <param name="Port" value="28915" type="string" />
-    </category>
     <category name="Misc">
         <param name="Watched" value="test@Jabber.goffi.int" type="string" />
     </category>
@@ -58,7 +55,41 @@
     def __init__(self):
         debug("Parameters init")
         self.load_default_params()
-        
+
+    def __get_unique_node(self, parent, tag, name):
+        """return node with given tag, create a new one if the node doesn't exist
+        @param parent: parent of nodes to check (e.g. documentElement)
+        @param tag: tag to check (e.g. "category")
+        @param name: name to check (e.g. "JID")
+        @return: node if it exist or None
+        """
+        for node in parent.childNodes:
+            if node.nodeName == tag and node.getAttribute("name") == name:
+                #the node already exists
+                return node
+        #the node is new
+        return None
+
+    def import_params(self, parent, xml):
+        """import xml in parameters, do nothing if the param already exist
+        @param parent: parent class (usefull for callbacks)
+        @param xml: parameters in xml form"""
+
+        src_dom = minidom.parseString(xml)
+
+        def import_node(tgt_parent, src_parent):
+            print "import_node [%s, %s]" % (tgt_parent.nodeName, src_parent.nodeName)
+            for child in src_parent.childNodes:
+                if child.nodeName == '#text':
+                    continue
+                node = self.__get_unique_node(tgt_parent, child.nodeName, child.getAttribute("name"))
+                if not node: #The node is new
+                    tgt_parent.appendChild(child)
+                else:
+                    import_node(node, child)
+
+        import_node(self.dom.documentElement, src_dom.documentElement)
+
 
     def getParamV(self, name, category):
         """Helper method to get the value in the good type
@@ -66,7 +97,7 @@
         node = self.__getParamNode(name, category)
         if not node:
             error("Requested param [%s] in category [%s] doesn't exist !" , name, category)
-            return none
+            return None
         return node.getAttribute("value")
 
     def getParams(self):
@@ -102,12 +133,6 @@
             return #TODO: throw an error
         node.setAttribute("value", value)
 
-    """def createParam (self, name, value, type, category):
-        ### TODO: add desciption in params
-        if not self.params.has_key(category):
-            self.params[category]={}
-        self.params[category][name]=[value,type];"""
-
 class Memory:
     """This class manage all persistent informations"""
 
@@ -134,6 +159,7 @@
                 error ("Can't load params !")
                 self.params.load_default_params()
         else:
+            error ("No params, using default parameters")
             self.params.load_default_params()
 
         #history
@@ -224,3 +250,6 @@
     
     def setParam(self, name, value, category):
         return self.params.setParam(name, value, category)
+
+    def import_params(self, parent, xml):
+        return self.params.import_params(parent, xml)