SàT: new constant management, a local dir (~/.sat) is now used

This commit is contained in:
Goffi 2009-12-19 20:32:58 +11:00
commit 98e10696f5

View file

@ -29,8 +29,8 @@ 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")
SAVEFILE_PARAM="/param"
SAVEFILE_HISTORY="/history"
class Param():
"""This class manage parameter with xml"""
@ -66,6 +66,8 @@ class Param():
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 @@ class Memory:
def __init__(self, host):
info ("Memory manager init")
self.host = host
self.contact={}
self.presenceStatus={}
self.params=Param(host)
@ -190,11 +193,15 @@ class Memory:
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 @@ class Memory:
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 @@ class Memory:
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")