diff tools/memory.py @ 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 3e24753b9e0b
children 9aa2d9dd4045
line wrap: on
line diff
--- 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")