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
2 changed files with 46 additions and 11 deletions

29
sat.tac
View file

@ -19,8 +19,11 @@ You should have received a copy of the GNU General Public License
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 @@ class SAT(service.Service):
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 @@ class SAT(service.Service):
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")

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")