comparison tools/memory.py @ 38:3e24753b9e0b

Fixed parameters loading/saving - removed unused "parent" argument in importParams - changed "ext" for "tld" in wix jid example
author Goffi <goffi@goffi.org>
date Thu, 17 Dec 2009 13:13:13 +1100
parents a544b376b6f0
children d24629c631fc
comparison
equal deleted inserted replaced
37:a61beb21d16d 38:3e24753b9e0b
51 </params> 51 </params>
52 """ 52 """
53 53
54 def load_default_params(self): 54 def load_default_params(self):
55 self.dom = minidom.parseString(Param.default_xml.encode('utf-8')) 55 self.dom = minidom.parseString(Param.default_xml.encode('utf-8'))
56
57 def load(self, file):
58 """Load parameters from file"""
59 self.dom = minidom.parse(file)
60
61 def save(self, file):
62 """Save parameters to xml file"""
63 with open(file, 'wb') as xml_file:
64 self.dom.writexml(xml_file)
56 65
57 def __init__(self, host): 66 def __init__(self, host):
58 debug("Parameters init") 67 debug("Parameters init")
59 self.host = host 68 self.host = host
60 self.load_default_params()
61 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB) 69 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
62 70
63 def __get_unique_node(self, parent, tag, name): 71 def __get_unique_node(self, parent, tag, name):
64 """return node with given tag, create a new one if the node doesn't exist 72 """return node with given tag, create a new one if the node doesn't exist
65 @param parent: parent of nodes to check (e.g. documentElement) 73 @param parent: parent of nodes to check (e.g. documentElement)
72 #the node already exists 80 #the node already exists
73 return node 81 return node
74 #the node is new 82 #the node is new
75 return None 83 return None
76 84
77 def importParams(self, parent, xml): 85 def importParams(self, xml):
78 """import xml in parameters, do nothing if the param already exist 86 """import xml in parameters, do nothing if the param already exist
79 @param parent: parent class (usefull for callbacks)
80 @param xml: parameters in xml form""" 87 @param xml: parameters in xml form"""
81 src_dom = minidom.parseString(xml.encode('utf-8')) 88 src_dom = minidom.parseString(xml.encode('utf-8'))
82 89
83 def import_node(tgt_parent, src_parent): 90 def import_node(tgt_parent, src_parent):
84 for child in src_parent.childNodes: 91 for child in src_parent.childNodes:
182 self.load() 189 self.load()
183 190
184 def load(self): 191 def load(self):
185 """Load parameters and all memory things from file/db""" 192 """Load parameters and all memory things from file/db"""
186 193
187 #first parameters 194 #parameters
188 if os.path.exists(const_SAVEFILE_PARAM): 195 if os.path.exists(const_SAVEFILE_PARAM):
189 try: 196 try:
190 #gof: FIXME FIXME FIXME: sauver l'xml et non plus le pickle !!!!!!! 197 self.params.load(const_SAVEFILE_PARAM)
191 with open(const_SAVEFILE_PARAM, 'r') as params_pickle:
192 self.params=pickle.load(params_pickle)
193 debug("params loaded") 198 debug("params loaded")
194 except: 199 except:
195 error ("Can't load params !") 200 error ("Can't load params !")
196 self.params.load_default_params() 201 self.params.load_default_params()
197 else: 202 else:
208 error ("Can't load history !") 213 error ("Can't load history !")
209 214
210 215
211 def save(self): 216 def save(self):
212 """Save parameters and all memory things to file/db""" 217 """Save parameters and all memory things to file/db"""
213 with open(const_SAVEFILE_PARAM, 'w') as params_pickle: 218 #TODO: need to encrypt files (at least passwords !) and set permissions
214 pickle.dump(self.params, params_pickle) 219 self.params.save(const_SAVEFILE_PARAM)
220 debug("params saved")
215 with open(const_SAVEFILE_HISTORY, 'w') as history_pickle: 221 with open(const_SAVEFILE_HISTORY, 'w') as history_pickle:
216 pickle.dump(self.history, history_pickle) 222 pickle.dump(self.history, history_pickle)
223 debug("history saved")
217 224
218 def addToHistory(self, me_jid, from_jid, to_jid, type, message): 225 def addToHistory(self, me_jid, from_jid, to_jid, type, message):
219 me_short=me_jid.userhost() 226 me_short=me_jid.userhost()
220 from_short=from_jid.userhost() 227 from_short=from_jid.userhost()
221 to_short=to_jid.userhost() 228 to_short=to_jid.userhost()
285 return self.params.getParamsCategories() 292 return self.params.getParamsCategories()
286 293
287 def setParam(self, name, value, category): 294 def setParam(self, name, value, category):
288 return self.params.setParam(name, value, category) 295 return self.params.setParam(name, value, category)
289 296
290 def importParams(self, parent, xml): 297 def importParams(self, xml):
291 return self.params.importParams(parent, xml) 298 return self.params.importParams(xml)
292 299
293 def setDefault(self, name, category, callback, errback=None): 300 def setDefault(self, name, category, callback, errback=None):
294 return self.params.setDefault(name, category, callback, errback) 301 return self.params.setDefault(name, category, callback, errback)