changeset 17:74a39f40eb6d

refactoring: using xml params (not finished yet)
author Goffi <goffi@goffi.org>
date Fri, 06 Nov 2009 19:48:57 +0100
parents 0a024d5e0cd0
children 6928e3cb73a8
files sat.tac sat_bridge/DBus.py tools/memory.py
diffstat 3 files changed, 113 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/sat.tac	Mon Nov 02 00:45:03 2009 +0100
+++ b/sat.tac	Fri Nov 06 19:48:57 2009 +0100
@@ -258,7 +258,7 @@
         self.bridge.register("getPresenceStatus", self.memory.getPresenceStatus)
         self.bridge.register("sendMessage", self.sendMessage)
         self.bridge.register("setParam", self.setParam)
-        self.bridge.register("getParam", self.memory.getParam)
+        #self.bridge.register("getParam", self.memory.getParam)
         self.bridge.register("getParams", self.memory.getParams)
         self.bridge.register("getParamsCategories", self.memory.getParamsCategories)
         self.bridge.register("getHistory", self.memory.getHistory)
--- a/sat_bridge/DBus.py	Mon Nov 02 00:45:03 2009 +0100
+++ b/sat_bridge/DBus.py	Fri Nov 06 19:48:57 2009 +0100
@@ -123,18 +123,23 @@
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
                          in_signature='sss', out_signature='')
-    def setParam(self, name, value, namespace="default"):
-        self.cb["setParam"](name, str(value), namespace)
+    def setParam(self, name, value, category):
+        self.cb["setParam"](name, str(value), category)
         
-    @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
+    """@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
                          in_signature='ss', out_signature='(ss)')
     def getParam(self, name, namespace="default"):
-        return self.cb["getParam"](name, namespace)
+        return self.cb["getParam"](name, namespace)"""
+
+    """@dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
+                         in_signature='s', out_signature='a(sss)')
+    def getParams(self, namespace):
+        return self.cb["getParams"](namespace)"""
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='s', out_signature='a(sss)')
-    def getParams(self, namespace):
-        return self.cb["getParams"](namespace)
+                         in_signature='', out_signature='s')
+    def getParams(self):
+        return self.cb["getParams"]()
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
                          in_signature='', out_signature='as')
--- a/tools/memory.py	Mon Nov 02 00:45:03 2009 +0100
+++ b/tools/memory.py	Fri Nov 06 19:48:57 2009 +0100
@@ -24,11 +24,98 @@
 import os.path
 import time
 import pickle
+from xml.dom import minidom
 from logging import debug, info, error
+import pdb
 
 const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save")
 const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save")
 
+class Param():
+    """This class manage parameter with xml"""
+    
+    #TODO: mettre Watched dans un plugin
+    default_xml = """
+    <params>
+    <category name="Connection">
+        <param name="JabberID" value="goffi@jabber.goffi.int/TestScript" type="string" />
+        <param name="Password" value="toto" type="password" />
+        <param name="Server" value="jabber.goffi.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>
+    </params>
+    """
+
+    def load_default_params(self):
+        self.dom = minidom.parseString(Param.default_xml)
+    
+    def __init__(self):
+        debug("Parameters init")
+        self.load_default_params()
+        
+
+    def getParamV(self, name, category):
+        """Helper method to get the value in the good type
+           getParamV stands for GetParamValue"""
+        node = self.__getParamNode(name, category)
+        if not node:
+            error("Requested param [%s] in category [%s] doesn't exist !" , name, category)
+            return none
+        return node.getAttribute("value")
+
+    def getParams(self):
+        """Return the wold params XML"""
+        return self.dom.toxml()
+
+
+    """def getParam(self, name, category):
+        if self.params.has_key(namespace) and self.params[namespace].has_key(name):
+            return self.params[namespace][name]
+        return ["",""]"""
+
+    def __getParamNode(self, name, category):
+        for node in self.dom.documentElement.childNodes:
+            if node.nodeName == "category" and node.getAttribute("name") == category:
+                params = node.getElementsByTagName("param")
+                for param in params:
+                    if param.getAttribute("name") == name:
+                        return param
+        return None
+        
+
+    """def getParams(self, namespace):
+        if self.params.has_key(namespace):
+            ret=[]
+            for name in self.params[namespace]:
+                ret.append([name] + self.params[namespace][name])
+            return ret
+        return [[]]"""
+
+    def getParamsCategories(self):
+        """return the categories availables"""
+        categories=[]
+        for cat in self.dom.getElementsByTagName("category"):
+            categories.append(cat.getAttribute("name"))
+        return categories
+
+    def setParam(self, name, value, category):
+        node = self.__getParamNode(name, category)
+        if not node:
+            return #TODO: throw an error
+        node.setAttribute(name, value)
+
+    """def createParam (self, name, value, type, namespace):
+        ### TODO: add desciption in params
+        if not self.params.has_key(namespace):
+            self.params[namespace]={}
+        self.params[namespace][name]=[value,type];"""
+
 class Memory:
     """This class manage all persistent informations"""
 
@@ -36,22 +123,12 @@
         info ("Memory manager init")
         self.contact={}
         self.presenceStatus={}
-        self.params={}
+        self.params=Param()
         self.history={}
         self.disco={}  #XXX: maybe best in a separate class
         self.features={}
         self.load()
 
-    def load_default_params(self):
-        """Load default value, in case of no save file or error."""
-
-        self.createParam("JabberID", "goffi@jabber.goffi.int/TestScript", "string", "Connection")
-        self.createParam("Password", "toto", "password", "Connection")
-        self.createParam("Server", "jabber.goffi.int", "string", "Connection")
-        self.createParam("IP", "192.168.0.2", "string", "File Transfert")
-        self.createParam("Port", "28915", "string", "File Transfert")
-        self.createParam("Watched", "test@jabber.goffi.int", "string", "Misc") #TODO: a mettre dans un plugin
-
     def load(self):
         """Load parameters and all memory things from file/db"""
 
@@ -63,9 +140,9 @@
                 debug("params loaded")
             except:
                 error ("Can't load params !")
-                self.load_default_params()
+                self.params.load_default_params()
         else:
-            self.load_default_params()
+            self.params.load_default_params()
 
         #history
         if os.path.exists(const_SAVEFILE_HISTORY):
@@ -141,39 +218,14 @@
         debug ("Memory getPresenceStatus (%s)", status)
         return status
 
-    def getParamV(self, name, namespace):
-        """Helper method to get the value in the good type
-           getParamV stands for GetParamValue"""
-        if not self.params.has_key(namespace) or not self.params[namespace].has_key(name):
-            error("Requested param %s in namespace %s doesn't exist !" , name, namespace)
-            return None
-        return self.params[namespace][name][0]
-
-
-    def getParam(self, name, namespace):
-        if self.params.has_key(namespace) and self.params[namespace].has_key(name):
-            return self.params[namespace][name]
-        return ["",""]
-
-    def getParams(self, namespace):
-        if self.params.has_key(namespace):
-            ret=[]
-            for name in self.params[namespace]:
-                ret.append([name] + self.params[namespace][name])
-            return ret
-        return [[]]
-
+    def getParamV(self, name, category):
+        return self.params.getParamV(name, category)
+    
+    def getParams(self):
+        return self.params.getParams() 
+    
     def getParamsCategories(self):
-        """return the namespaces availables"""
-        return self.params.keys()
-
-    def setParam(self, name, value, namespace):
-        if not self.params.has_key(namespace) or not self.params[namespace].has_key(name):
-           return #TODO: throw an error
-        self.params[namespace][name][0]=value;
-
-    def createParam (self, name, value, type, namespace):
-        ### TODO: add desciption in params
-        if not self.params.has_key(namespace):
-            self.params[namespace]={}
-        self.params[namespace][name]=[value,type];
+        return self.params.getParamsCategories()
+    
+    def setParam(self, name, value, category):
+        return self.params.setParam(name, value, category)