refactoring: using xml params part III (parameters import)

This commit is contained in:
Goffi 2009-11-07 21:23:28 +01:00
commit ea056ce96e
3 changed files with 52 additions and 13 deletions

View file

@ -451,6 +451,15 @@ class Socks5ClientFactory(protocol.ClientFactory):
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 @@ 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)

View file

@ -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

View file

@ -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():
<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 @@ 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)