diff tools/memory.py @ 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
line wrap: on
line diff
--- 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)