changeset 41:d24629c631fc

SàT: new constant management, a local dir (~/.sat) is now used
author Goffi <goffi@goffi.org>
date Sat, 19 Dec 2009 20:32:58 +1100
parents 6f0699ba0329
children 874de3020e1c
files sat.tac tools/memory.py
diffstat 2 files changed, 46 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/sat.tac	Sat Dec 19 18:13:04 2009 +1100
+++ b/sat.tac	Sat Dec 19 20:32:58 2009 +1100
@@ -19,8 +19,11 @@
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 """
 
-client_name = u'SàT (Salut à toi)'
-client_version = '0.0.1D'   #Please add 'D' at the end for dev versions
+CONST = {
+    'client_name' : u'SàT (Salut à toi)',
+    'client_version' : u'0.0.1D',   #Please add 'D' at the end for dev versions
+    'local_dir' : '~/.sat'
+}
 
 from twisted.application import internet, service
 from twisted.internet import glib2reactor, protocol, task
@@ -250,8 +253,27 @@
     def get_next_id(self):
         return sat_next_id()
 
+    def get_const(self, name):
+        """Return a constant"""
+        if not CONST.has_key(name):
+            error('Trying to access an undefined constant')
+            raise Exception
+        return CONST[name]
+
+    def set_const(self, name, value):
+        """Save a constant"""
+        if CONST.has_key(name):
+            error('Trying to redefine a constant')
+            raise Exception  
+        CONST[name] = value
+    
     def __init__(self):
         #TODO: standardize callback system
+        
+        local_dir = os.path.expanduser(self.get_const('local_dir'))
+        if not os.path.exists(local_dir):
+            os.makedirs(local_dir)
+
         self.__waiting_conf = {}  #callback called when a confirmation is received
         self.__progress_cb_map = {}  #callback called when a progress is requested (key = progress id)
         self.__general_cb_map = {}  #callback called for general reasons (key = name) 
@@ -323,7 +345,8 @@
         self.fallBack = SatFallbackHandler(self)
         self.fallBack.setHandlerParent(self.xmppclient)
 
-        self.versionHandler = generic.VersionHandler(unicode(client_name), client_version)
+        self.versionHandler = generic.VersionHandler(self.get_const('client_name'),
+                                                     self.get_const('client_version'))
         self.versionHandler.setHandlerParent(self.xmppclient)
 
         debug ("setting plugins parents")
--- a/tools/memory.py	Sat Dec 19 18:13:04 2009 +1100
+++ b/tools/memory.py	Sat Dec 19 20:32:58 2009 +1100
@@ -29,8 +29,8 @@
 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")
+SAVEFILE_PARAM="/param"
+SAVEFILE_HISTORY="/history"
 
 class Param():
     """This class manage parameter with xml"""
@@ -66,6 +66,8 @@
     def __init__(self, host):
         debug("Parameters init")
         self.host = host
+        host.set_const('savefile_param', SAVEFILE_PARAM)
+        host.set_const('savefile_history', SAVEFILE_HISTORY)
         host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
 
     def __get_unique_node(self, parent, tag, name):
@@ -180,6 +182,7 @@
 
     def __init__(self, host):
         info ("Memory manager init")
+        self.host = host
         self.contact={}
         self.presenceStatus={}
         self.params=Param(host)
@@ -190,11 +193,15 @@
 
     def load(self):
         """Load parameters and all memory things from file/db"""
+        param_file = os.path.expanduser(self.host.get_const('local_dir')+
+                                        self.host.get_const('savefile_param'))
+        history_file = os.path.expanduser(self.host.get_const('local_dir')+
+                                        self.host.get_const('savefile_history'))
 
         #parameters
-        if os.path.exists(const_SAVEFILE_PARAM):
+        if os.path.exists(param_file):
             try:
-                self.params.load(const_SAVEFILE_PARAM)
+                self.params.load(param_file)
                 debug("params loaded")
             except:
                 error ("Can't load params !")
@@ -204,9 +211,9 @@
             self.params.load_default_params()
 
         #history
-        if os.path.exists(const_SAVEFILE_HISTORY):
+        if os.path.exists(history_file):
             try:
-                with open(const_SAVEFILE_HISTORY, 'r') as history_pickle:
+                with open(history_file, 'r') as history_pickle:
                     self.history=pickle.load(history_pickle)
                 debug("history loaded")
             except:
@@ -216,9 +223,14 @@
     def save(self):
         """Save parameters and all memory things to file/db"""
         #TODO: need to encrypt files (at least passwords !) and set permissions
-        self.params.save(const_SAVEFILE_PARAM)
+        param_file = os.path.expanduser(self.host.get_const('local_dir')+
+                                        self.host.get_const('savefile_param'))
+        history_file = os.path.expanduser(self.host.get_const('local_dir')+
+                                        self.host.get_const('savefile_history'))
+        
+        self.params.save(param_file)
         debug("params saved")
-        with open(const_SAVEFILE_HISTORY, 'w') as history_pickle:
+        with open(history_file, 'w') as history_pickle:
             pickle.dump(self.history, history_pickle)
         debug("history saved")