Mercurial > libervia-backend
changeset 20:fc8c202cda87
refactoring: using xml params part IV (default values)
- File Transfert/IP is now set by default to the ip externally visible
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 08 Nov 2009 00:11:00 +0100 |
parents | f2a745ca0fbc |
children | 633c5ed65701 |
files | plugins/plugin_xep_0065.py tools/memory.py |
diffstat | 2 files changed, 40 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/plugins/plugin_xep_0065.py Sat Nov 07 21:23:28 2009 +0100 +++ b/plugins/plugin_xep_0065.py Sun Nov 08 00:11:00 2009 +0100 @@ -59,8 +59,9 @@ from twisted.internet import protocol, reactor from twisted.protocols.basic import FileSender from twisted.words.xish import domish +from twisted.web.client import getPage import struct -from binascii import hexlify +import urllib import hashlib, pdb from zope.interface import implements @@ -454,12 +455,12 @@ params = """ <params> <category name="File Transfert"> - <param name="IP" value="192.168.0.10" type="string" /> + <param name="IP" value='0.0.0.0' default_cb='yes' type="string" /> <param name="Port" value="28915" type="string" /> </category> </params> """ - + def __init__(self, host): info("Plugin XEP_0065 initialization") self.host = host @@ -467,11 +468,16 @@ 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) + host.memory.importParams(self, XEP_0065.params) + host.memory.setDefault("IP", "File Transfert", self.getExternalIP) port = int(self.host.memory.getParamV("Port", "File Transfert")) info("Launching Socks5 Stream server on port %d", port) reactor.listenTCP(port, self.server_factory) + def getExternalIP(self): + """Return IP visible from outside, by asking to a website""" + return getPage("http://www.goffi.org/sat_tools/get_ip.php") + def connectionInitialized(self): self.xmlstream.addObserver(BS_REQUEST, self.getFile)
--- a/tools/memory.py Sat Nov 07 21:23:28 2009 +0100 +++ b/tools/memory.py Sun Nov 08 00:11:00 2009 +0100 @@ -27,6 +27,7 @@ from xml.dom import minidom from logging import debug, info, error import pdb +from twisted.internet import defer const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save") const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save") @@ -70,11 +71,10 @@ #the node is new return None - def import_params(self, parent, xml): + def importParams(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): @@ -90,6 +90,29 @@ import_node(self.dom.documentElement, src_dom.documentElement) + def __default_ok(self, value, name, category): + self.setParam(name, value, category) + + def __default_ko(self, failure, name, category): + error ("Can't determine default value for [%s/%s]: %s" % (category, name, str(failure.value))) + + def setDefault(self, name, category, callback, errback=None): + """Set default value of parameter + 'default_cb' attibute of parameter must be set to 'yes' + @param name: name of the parameter + @param category: category of the parameter + @param callback: must return a string with the value (use deferred if needed) + @param errback: must manage the error with args failure, name, category + """ + node = self.__getParamNode(name, category) + if not node: + error("Requested param [%s] in category [%s] doesn't exist !" , name, category) + return + if node.getAttribute('default_cb') == 'yes': + del node.attributes['default_cb'] + d = defer.maybeDeferred(callback) + d.addCallback(self.__default_ok, name, category) + d.addErrback(errback or self.__default_ko, name, category) def getParamV(self, name, category): """Helper method to get the value in the good type @@ -251,5 +274,8 @@ 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) + def importParams(self, parent, xml): + return self.params.importParams(parent, xml) + + def setDefault(self, name, category, callback, errback=None): + return self.params.setDefault(name, category, callback, errback)