Mercurial > libervia-backend
diff 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 |
line wrap: on
line diff
--- a/tools/memory.py Thu Jan 28 02:32:27 2010 +1100 +++ b/tools/memory.py Thu Jan 28 12:38:12 2010 +1100 @@ -30,7 +30,8 @@ from twisted.internet import defer from twisted.words.protocols.jabber import jid -SAVEFILE_PARAM="/param" +SAVEFILE_PARAM_XML="/param" #xml parameters template +SAVEFILE_PARAM_DATA="/param" #individual & general parameters; _ind and _gen suffixes will be added SAVEFILE_HISTORY="/history" SAVEFILE_PRIVATE="/private" #file used to store misc values (mainly for plugins) @@ -60,22 +61,57 @@ def load_default_params(self): self.dom = minidom.parseString(Param.default_xml.encode('utf-8')) - def load(self, file): - """Load parameters from file""" + def load_xml(self, file): + """Load parameters template from file""" self.dom = minidom.parse(file) - def save(self, file): - """Save parameters to xml file""" + def load_data(self, file): + """Load parameters data from file""" + file_ind = file + '_ind' + file_gen = file + '_gen' + + if os.path.exists(file_gen): + try: + with open(file_gen, 'r') as file_gen_pickle: + self.params_gen=pickle.load(file_gen_pickle) + debug("general params data loaded") + except: + error ("Can't load general params data !") + + if os.path.exists(file_ind): + try: + with open(file_ind, 'r') as file_ind_pickle: + self.params=pickle.load(file_ind_pickle) + debug("individual params data loaded") + except: + error ("Can't load individual params data !") + + def save_xml(self, file): + """Save parameters template to xml file""" with open(file, 'wb') as xml_file: self.dom.writexml(xml_file) + def save_data(self, file): + """Save parameters data to file""" + #TODO: save properly in a separate file/database, + # use different behaviour depending of the data type (e.g. password encrypted) + + #general params + with open(file+'_gen', 'w') as param_gen_pickle: + pickle.dump(self.params_gen, param_gen_pickle) + + #then individual params + with open(file+'_ind', 'w') as param_ind_pickle: + pickle.dump(self.params, param_ind_pickle) + def __init__(self, host): debug("Parameters init") self.host = host self.default_profile = None self.params = {'goffi':{}} #gof: self.params_gen = {} - host.set_const('savefile_param', SAVEFILE_PARAM) + host.set_const('savefile_param_xml', SAVEFILE_PARAM_XML) + host.set_const('savefile_param_data', SAVEFILE_PARAM_DATA) host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB) def getProfilesList(self): @@ -139,7 +175,7 @@ import_node(self.dom.documentElement, src_dom.documentElement) def __default_ok(self, value, name, category): - self.setParam(name, value, category) + self.setParam(name, value, category) #FIXME: better to set param xml value ??? def __default_ko(self, failure, name, category): error ("Can't determine default value for [%s/%s]: %s" % (category, name, str(failure.value))) @@ -299,6 +335,7 @@ if node[0] == 'general': self.params_gen[(category, name)] = value + self.host.bridge.paramUpdate(name, value, category) #TODO: add profile in signal return assert (node[0] == 'individual') @@ -308,7 +345,7 @@ error('Trying to set parameter for an unknown profile') return #TODO: throw an error - type = node.getAttribute("type") + type = node[1].getAttribute("type") if type=="button": print "clique",node.toxml() else: @@ -335,25 +372,33 @@ 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')) + param_file_xml = os.path.expanduser(self.host.get_const('local_dir')+ + self.host.get_const('savefile_param_xml')) + param_file_data = os.path.expanduser(self.host.get_const('local_dir')+ + self.host.get_const('savefile_param_data')) history_file = os.path.expanduser(self.host.get_const('local_dir')+ self.host.get_const('savefile_history')) private_file = os.path.expanduser(self.host.get_const('local_dir')+ self.host.get_const('savefile_private')) #parameters - if os.path.exists(param_file): + if os.path.exists(param_file_xml): try: - self.params.load(param_file) - debug("params loaded") + self.params.load_xml(param_file_xml) + debug("params template loaded") except: - error ("Can't load params !") + error ("Can't load params template !") self.params.load_default_params() else: - error ("No params, using default parameters") + info ("No params template, using default template") self.params.load_default_params() + try: + self.params.load_data(param_file_data) + debug("params loaded") + except: + error ("Can't load params !") + #history if os.path.exists(history_file): try: @@ -375,14 +420,17 @@ def save(self): """Save parameters and all memory things to file/db""" #TODO: need to encrypt files (at least passwords !) and set permissions - param_file = os.path.expanduser(self.host.get_const('local_dir')+ - self.host.get_const('savefile_param')) + param_file_xml = os.path.expanduser(self.host.get_const('local_dir')+ + self.host.get_const('savefile_param_xml')) + param_file_data = os.path.expanduser(self.host.get_const('local_dir')+ + self.host.get_const('savefile_param_data')) history_file = os.path.expanduser(self.host.get_const('local_dir')+ self.host.get_const('savefile_history')) private_file = os.path.expanduser(self.host.get_const('local_dir')+ self.host.get_const('savefile_private')) - self.params.save(param_file) + self.params.save_xml(param_file_xml) + self.params.save_data(param_file_data) debug("params saved") with open(history_file, 'w') as history_pickle: pickle.dump(self.history, history_pickle)