comparison 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
comparison
equal deleted inserted replaced
40:6f0699ba0329 41:d24629c631fc
27 from xml.dom import minidom 27 from xml.dom import minidom
28 from logging import debug, info, error 28 from logging import debug, info, error
29 import pdb 29 import pdb
30 from twisted.internet import defer 30 from twisted.internet import defer
31 31
32 const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save") 32 SAVEFILE_PARAM="/param"
33 const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save") 33 SAVEFILE_HISTORY="/history"
34 34
35 class Param(): 35 class Param():
36 """This class manage parameter with xml""" 36 """This class manage parameter with xml"""
37 ### TODO: add desciption in params 37 ### TODO: add desciption in params
38 38
64 self.dom.writexml(xml_file) 64 self.dom.writexml(xml_file)
65 65
66 def __init__(self, host): 66 def __init__(self, host):
67 debug("Parameters init") 67 debug("Parameters init")
68 self.host = host 68 self.host = host
69 host.set_const('savefile_param', SAVEFILE_PARAM)
70 host.set_const('savefile_history', SAVEFILE_HISTORY)
69 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB) 71 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
70 72
71 def __get_unique_node(self, parent, tag, name): 73 def __get_unique_node(self, parent, tag, name):
72 """return node with given tag, create a new one if the node doesn't exist 74 """return node with given tag, create a new one if the node doesn't exist
73 @param parent: parent of nodes to check (e.g. documentElement) 75 @param parent: parent of nodes to check (e.g. documentElement)
178 class Memory: 180 class Memory:
179 """This class manage all persistent informations""" 181 """This class manage all persistent informations"""
180 182
181 def __init__(self, host): 183 def __init__(self, host):
182 info ("Memory manager init") 184 info ("Memory manager init")
185 self.host = host
183 self.contact={} 186 self.contact={}
184 self.presenceStatus={} 187 self.presenceStatus={}
185 self.params=Param(host) 188 self.params=Param(host)
186 self.history={} 189 self.history={}
187 self.disco={} #XXX: maybe best in a separate class 190 self.disco={} #XXX: maybe best in a separate class
188 self.features={} 191 self.features={}
189 self.load() 192 self.load()
190 193
191 def load(self): 194 def load(self):
192 """Load parameters and all memory things from file/db""" 195 """Load parameters and all memory things from file/db"""
196 param_file = os.path.expanduser(self.host.get_const('local_dir')+
197 self.host.get_const('savefile_param'))
198 history_file = os.path.expanduser(self.host.get_const('local_dir')+
199 self.host.get_const('savefile_history'))
193 200
194 #parameters 201 #parameters
195 if os.path.exists(const_SAVEFILE_PARAM): 202 if os.path.exists(param_file):
196 try: 203 try:
197 self.params.load(const_SAVEFILE_PARAM) 204 self.params.load(param_file)
198 debug("params loaded") 205 debug("params loaded")
199 except: 206 except:
200 error ("Can't load params !") 207 error ("Can't load params !")
201 self.params.load_default_params() 208 self.params.load_default_params()
202 else: 209 else:
203 error ("No params, using default parameters") 210 error ("No params, using default parameters")
204 self.params.load_default_params() 211 self.params.load_default_params()
205 212
206 #history 213 #history
207 if os.path.exists(const_SAVEFILE_HISTORY): 214 if os.path.exists(history_file):
208 try: 215 try:
209 with open(const_SAVEFILE_HISTORY, 'r') as history_pickle: 216 with open(history_file, 'r') as history_pickle:
210 self.history=pickle.load(history_pickle) 217 self.history=pickle.load(history_pickle)
211 debug("history loaded") 218 debug("history loaded")
212 except: 219 except:
213 error ("Can't load history !") 220 error ("Can't load history !")
214 221
215 222
216 def save(self): 223 def save(self):
217 """Save parameters and all memory things to file/db""" 224 """Save parameters and all memory things to file/db"""
218 #TODO: need to encrypt files (at least passwords !) and set permissions 225 #TODO: need to encrypt files (at least passwords !) and set permissions
219 self.params.save(const_SAVEFILE_PARAM) 226 param_file = os.path.expanduser(self.host.get_const('local_dir')+
227 self.host.get_const('savefile_param'))
228 history_file = os.path.expanduser(self.host.get_const('local_dir')+
229 self.host.get_const('savefile_history'))
230
231 self.params.save(param_file)
220 debug("params saved") 232 debug("params saved")
221 with open(const_SAVEFILE_HISTORY, 'w') as history_pickle: 233 with open(history_file, 'w') as history_pickle:
222 pickle.dump(self.history, history_pickle) 234 pickle.dump(self.history, history_pickle)
223 debug("history saved") 235 debug("history saved")
224 236
225 def addToHistory(self, me_jid, from_jid, to_jid, type, message): 237 def addToHistory(self, me_jid, from_jid, to_jid, type, message):
226 me_short=me_jid.userhost() 238 me_short=me_jid.userhost()