changeset 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
files plugins/plugin_xep_0065.py plugins/plugin_xep_0096.py tools/memory.py
diffstat 3 files changed, 52 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/plugin_xep_0065.py	Fri Nov 06 23:31:00 2009 +0100
+++ b/plugins/plugin_xep_0065.py	Sat Nov 07 21:23:28 2009 +0100
@@ -451,6 +451,15 @@
 class XEP_0065(XMPPHandler):
     implements(iwokkel.IDisco)
     
+    params = """
+    <params>
+    <category name="File Transfert">
+        <param name="IP" value="192.168.0.10" type="string" />
+        <param name="Port" value="28915" type="string" />
+    </category>
+    </params>
+    """
+    
     def __init__(self, host):
         info("Plugin XEP_0065 initialization")
         self.host = host
@@ -458,6 +467,7 @@
         self.server_factory = Socks5ServerFactory()
         self.server_factory.protocol.host = self.host #needed for progress CB
         self.client_factory = Socks5ClientFactory()
+        host.memory.import_params(self, XEP_0065.params)
         port = int(self.host.memory.getParamV("Port", "File Transfert"))
         info("Launching Socks5 Stream server on port %d", port)
         reactor.listenTCP(port, self.server_factory)
--- a/plugins/plugin_xep_0096.py	Fri Nov 06 23:31:00 2009 +0100
+++ b/plugins/plugin_xep_0096.py	Sat Nov 07 21:23:28 2009 +0100
@@ -51,7 +51,7 @@
 
 class XEP_0096(XMPPHandler):
     implements(iwokkel.IDisco)
-    
+
     def __init__(self, host):
         info("Plugin XEP_0096 initialization")
         self.host = host
--- 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)