From ea056ce96e4620639d8015d8290b17121c1e7f46 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sat, 7 Nov 2009 21:23:28 +0100 Subject: [PATCH] refactoring: using xml params part III (parameters import) --- plugins/plugin_xep_0065.py | 10 +++++++ plugins/plugin_xep_0096.py | 2 +- tools/memory.py | 53 +++++++++++++++++++++++++++++--------- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/plugins/plugin_xep_0065.py b/plugins/plugin_xep_0065.py index 51330255..1e01cb1a 100755 --- a/plugins/plugin_xep_0065.py +++ b/plugins/plugin_xep_0065.py @@ -451,6 +451,15 @@ class Socks5ClientFactory(protocol.ClientFactory): class XEP_0065(XMPPHandler): implements(iwokkel.IDisco) + params = """ + + + + + + + """ + def __init__(self, host): info("Plugin XEP_0065 initialization") self.host = host @@ -458,6 +467,7 @@ class XEP_0065(XMPPHandler): 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) diff --git a/plugins/plugin_xep_0096.py b/plugins/plugin_xep_0096.py index 976a9c6e..ee393801 100755 --- a/plugins/plugin_xep_0096.py +++ b/plugins/plugin_xep_0096.py @@ -51,7 +51,7 @@ PLUGIN_INFO = { class XEP_0096(XMPPHandler): implements(iwokkel.IDisco) - + def __init__(self, host): info("Plugin XEP_0096 initialization") self.host = host diff --git a/tools/memory.py b/tools/memory.py index f262cfcb..f991ac4a 100644 --- a/tools/memory.py +++ b/tools/memory.py @@ -33,6 +33,7 @@ const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save") 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 @@ class Param(): - - - - @@ -58,7 +55,41 @@ class Param(): 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 @@ class Param(): 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 @@ class Param(): 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 @@ class Memory: 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 @@ class Memory: 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)