comparison tools/memory.py @ 61:58d49fc19639

parameters are saved again - use 2 new files to save individual and general parameters data - setParam from dbus bridge now use str instead of dbus.String - fixed param updated signal for general params - fixed param setting
author Goffi <goffi@goffi.org>
date Thu, 28 Jan 2010 12:38:12 +1100
parents 9764e027ecc0
children 93cb45a7420f
comparison
equal deleted inserted replaced
60:9764e027ecc0 61:58d49fc19639
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 from twisted.words.protocols.jabber import jid 31 from twisted.words.protocols.jabber import jid
32 32
33 SAVEFILE_PARAM="/param" 33 SAVEFILE_PARAM_XML="/param" #xml parameters template
34 SAVEFILE_PARAM_DATA="/param" #individual & general parameters; _ind and _gen suffixes will be added
34 SAVEFILE_HISTORY="/history" 35 SAVEFILE_HISTORY="/history"
35 SAVEFILE_PRIVATE="/private" #file used to store misc values (mainly for plugins) 36 SAVEFILE_PRIVATE="/private" #file used to store misc values (mainly for plugins)
36 37
37 class Param(): 38 class Param():
38 """This class manage parameter with xml""" 39 """This class manage parameter with xml"""
58 """ 59 """
59 60
60 def load_default_params(self): 61 def load_default_params(self):
61 self.dom = minidom.parseString(Param.default_xml.encode('utf-8')) 62 self.dom = minidom.parseString(Param.default_xml.encode('utf-8'))
62 63
63 def load(self, file): 64 def load_xml(self, file):
64 """Load parameters from file""" 65 """Load parameters template from file"""
65 self.dom = minidom.parse(file) 66 self.dom = minidom.parse(file)
66 67
67 def save(self, file): 68 def load_data(self, file):
68 """Save parameters to xml file""" 69 """Load parameters data from file"""
70 file_ind = file + '_ind'
71 file_gen = file + '_gen'
72
73 if os.path.exists(file_gen):
74 try:
75 with open(file_gen, 'r') as file_gen_pickle:
76 self.params_gen=pickle.load(file_gen_pickle)
77 debug("general params data loaded")
78 except:
79 error ("Can't load general params data !")
80
81 if os.path.exists(file_ind):
82 try:
83 with open(file_ind, 'r') as file_ind_pickle:
84 self.params=pickle.load(file_ind_pickle)
85 debug("individual params data loaded")
86 except:
87 error ("Can't load individual params data !")
88
89 def save_xml(self, file):
90 """Save parameters template to xml file"""
69 with open(file, 'wb') as xml_file: 91 with open(file, 'wb') as xml_file:
70 self.dom.writexml(xml_file) 92 self.dom.writexml(xml_file)
93
94 def save_data(self, file):
95 """Save parameters data to file"""
96 #TODO: save properly in a separate file/database,
97 # use different behaviour depending of the data type (e.g. password encrypted)
98
99 #general params
100 with open(file+'_gen', 'w') as param_gen_pickle:
101 pickle.dump(self.params_gen, param_gen_pickle)
102
103 #then individual params
104 with open(file+'_ind', 'w') as param_ind_pickle:
105 pickle.dump(self.params, param_ind_pickle)
71 106
72 def __init__(self, host): 107 def __init__(self, host):
73 debug("Parameters init") 108 debug("Parameters init")
74 self.host = host 109 self.host = host
75 self.default_profile = None 110 self.default_profile = None
76 self.params = {'goffi':{}} #gof: 111 self.params = {'goffi':{}} #gof:
77 self.params_gen = {} 112 self.params_gen = {}
78 host.set_const('savefile_param', SAVEFILE_PARAM) 113 host.set_const('savefile_param_xml', SAVEFILE_PARAM_XML)
114 host.set_const('savefile_param_data', SAVEFILE_PARAM_DATA)
79 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB) 115 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
80 116
81 def getProfilesList(self): 117 def getProfilesList(self):
82 return self.params.keys() 118 return self.params.keys()
83 119
137 import_node(node, child) 173 import_node(node, child)
138 174
139 import_node(self.dom.documentElement, src_dom.documentElement) 175 import_node(self.dom.documentElement, src_dom.documentElement)
140 176
141 def __default_ok(self, value, name, category): 177 def __default_ok(self, value, name, category):
142 self.setParam(name, value, category) 178 self.setParam(name, value, category) #FIXME: better to set param xml value ???
143 179
144 def __default_ko(self, failure, name, category): 180 def __default_ko(self, failure, name, category):
145 error ("Can't determine default value for [%s/%s]: %s" % (category, name, str(failure.value))) 181 error ("Can't determine default value for [%s/%s]: %s" % (category, name, str(failure.value)))
146 182
147 def setDefault(self, name, category, callback, errback=None): 183 def setDefault(self, name, category, callback, errback=None):
297 error('Requesting an unknown parameter (%s/%s)' % (category, name)) 333 error('Requesting an unknown parameter (%s/%s)' % (category, name))
298 return 334 return
299 335
300 if node[0] == 'general': 336 if node[0] == 'general':
301 self.params_gen[(category, name)] = value 337 self.params_gen[(category, name)] = value
338 self.host.bridge.paramUpdate(name, value, category) #TODO: add profile in signal
302 return 339 return
303 340
304 assert (node[0] == 'individual') 341 assert (node[0] == 'individual')
305 342
306 profile = self.getProfileName(profile_key) 343 profile = self.getProfileName(profile_key)
307 if not profile: 344 if not profile:
308 error('Trying to set parameter for an unknown profile') 345 error('Trying to set parameter for an unknown profile')
309 return #TODO: throw an error 346 return #TODO: throw an error
310 347
311 type = node.getAttribute("type") 348 type = node[1].getAttribute("type")
312 if type=="button": 349 if type=="button":
313 print "clique",node.toxml() 350 print "clique",node.toxml()
314 else: 351 else:
315 self.params[profile][(category, name)] = value 352 self.params[profile][(category, name)] = value
316 self.host.bridge.paramUpdate(name, value, category) #TODO: add profile in signal 353 self.host.bridge.paramUpdate(name, value, category) #TODO: add profile in signal
333 host.set_const('savefile_private', SAVEFILE_PRIVATE) 370 host.set_const('savefile_private', SAVEFILE_PRIVATE)
334 self.load() 371 self.load()
335 372
336 def load(self): 373 def load(self):
337 """Load parameters and all memory things from file/db""" 374 """Load parameters and all memory things from file/db"""
338 param_file = os.path.expanduser(self.host.get_const('local_dir')+ 375 param_file_xml = os.path.expanduser(self.host.get_const('local_dir')+
339 self.host.get_const('savefile_param')) 376 self.host.get_const('savefile_param_xml'))
377 param_file_data = os.path.expanduser(self.host.get_const('local_dir')+
378 self.host.get_const('savefile_param_data'))
340 history_file = os.path.expanduser(self.host.get_const('local_dir')+ 379 history_file = os.path.expanduser(self.host.get_const('local_dir')+
341 self.host.get_const('savefile_history')) 380 self.host.get_const('savefile_history'))
342 private_file = os.path.expanduser(self.host.get_const('local_dir')+ 381 private_file = os.path.expanduser(self.host.get_const('local_dir')+
343 self.host.get_const('savefile_private')) 382 self.host.get_const('savefile_private'))
344 383
345 #parameters 384 #parameters
346 if os.path.exists(param_file): 385 if os.path.exists(param_file_xml):
347 try: 386 try:
348 self.params.load(param_file) 387 self.params.load_xml(param_file_xml)
349 debug("params loaded") 388 debug("params template loaded")
350 except: 389 except:
351 error ("Can't load params !") 390 error ("Can't load params template !")
352 self.params.load_default_params() 391 self.params.load_default_params()
353 else: 392 else:
354 error ("No params, using default parameters") 393 info ("No params template, using default template")
355 self.params.load_default_params() 394 self.params.load_default_params()
395
396 try:
397 self.params.load_data(param_file_data)
398 debug("params loaded")
399 except:
400 error ("Can't load params !")
356 401
357 #history 402 #history
358 if os.path.exists(history_file): 403 if os.path.exists(history_file):
359 try: 404 try:
360 with open(history_file, 'r') as history_pickle: 405 with open(history_file, 'r') as history_pickle:
373 error ("Can't load private values !") 418 error ("Can't load private values !")
374 419
375 def save(self): 420 def save(self):
376 """Save parameters and all memory things to file/db""" 421 """Save parameters and all memory things to file/db"""
377 #TODO: need to encrypt files (at least passwords !) and set permissions 422 #TODO: need to encrypt files (at least passwords !) and set permissions
378 param_file = os.path.expanduser(self.host.get_const('local_dir')+ 423 param_file_xml = os.path.expanduser(self.host.get_const('local_dir')+
379 self.host.get_const('savefile_param')) 424 self.host.get_const('savefile_param_xml'))
425 param_file_data = os.path.expanduser(self.host.get_const('local_dir')+
426 self.host.get_const('savefile_param_data'))
380 history_file = os.path.expanduser(self.host.get_const('local_dir')+ 427 history_file = os.path.expanduser(self.host.get_const('local_dir')+
381 self.host.get_const('savefile_history')) 428 self.host.get_const('savefile_history'))
382 private_file = os.path.expanduser(self.host.get_const('local_dir')+ 429 private_file = os.path.expanduser(self.host.get_const('local_dir')+
383 self.host.get_const('savefile_private')) 430 self.host.get_const('savefile_private'))
384 431
385 self.params.save(param_file) 432 self.params.save_xml(param_file_xml)
433 self.params.save_data(param_file_data)
386 debug("params saved") 434 debug("params saved")
387 with open(history_file, 'w') as history_pickle: 435 with open(history_file, 'w') as history_pickle:
388 pickle.dump(self.history, history_pickle) 436 pickle.dump(self.history, history_pickle)
389 debug("history saved") 437 debug("history saved")
390 with open(private_file, 'w') as private_pickle: 438 with open(private_file, 'w') as private_pickle: