comparison src/tools/memory.py @ 416:32dc8b18c2ae

core: param loading/purging on profile connection/disconnection - fixed default value in .*getParam.*
author Goffi <goffi@goffi.org>
date Tue, 01 Nov 2011 22:59:15 +0100
parents dd4caab17008
children f1bf8b6143b7
comparison
equal deleted inserted replaced
415:3348331e0f09 416:32dc8b18c2ae
77 77
78 def load_xml(self, file): 78 def load_xml(self, file):
79 """Load parameters template from file""" 79 """Load parameters template from file"""
80 self.dom = minidom.parse(file) 80 self.dom = minidom.parse(file)
81 81
82 def loadGenData(self): 82 def loadGenParams(self):
83 """Load general parameters data from storage 83 """Load general parameters data from storage
84 @return: deferred triggered once params are loaded""" 84 @return: deferred triggered once params are loaded"""
85 return self.storage.loadGenParams(self.params_gen) 85 return self.storage.loadGenParams(self.params_gen)
86 86
87 def loadIndData(self, profile): 87 def loadIndParams(self, profile):
88 """Load individual parameters 88 """Load individual parameters
89 set self.params cache 89 set self.params cache
90 @param profile: profile to load (*must exist*) 90 @param profile: profile to load (*must exist*)
91 @return: deferred triggered once params are loaded""" 91 @return: deferred triggered once params are loaded"""
92 self.params[profile] = {} 92 self.params[profile] = {}
93 return self.storage.loadIndParams(self.params, profile) 93 return self.storage.loadIndParams(self.params, profile)
94 94
95 def purgeProfile(self, profile):
96 """Remove cache data of a profile
97 @param profile: %(doc_profile)s"""
98 try:
99 del self.params[profile]
100 except KeyError:
101 error(_("Trying to purge cache of a profile not in memory: [%s]") % profile)
102
95 def save_xml(self, file): 103 def save_xml(self, file):
96 """Save parameters template to xml file""" 104 """Save parameters template to xml file"""
97 with open(file, 'wb') as xml_file: 105 with open(file, 'wb') as xml_file:
98 xml_file.write(self.dom.toxml('utf-8')) 106 xml_file.write(self.dom.toxml('utf-8'))
99 107
231 error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category}) 239 error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category})
232 return "" 240 return ""
233 241
234 if node[0] == 'general': 242 if node[0] == 'general':
235 value = self.__getParam(None, category, name, 'general') 243 value = self.__getParam(None, category, name, 'general')
236 return value or node[1].getAttribute(attr) 244 return value if value!=None else node[1].getAttribute(attr)
237 245
238 assert(node[0] == 'individual') 246 assert(node[0] == 'individual')
239 247
240 profile = self.getProfileName(profile_key) 248 profile = self.getProfileName(profile_key)
241 if not profile: 249 if not profile:
245 if profile not in self.params: 253 if profile not in self.params:
246 error(_('Requesting synchronous param for not connected profile')) 254 error(_('Requesting synchronous param for not connected profile'))
247 return "" 255 return ""
248 256
249 if attr == "value": 257 if attr == "value":
250 return self.__getParam(profile, category, name) or node[1].getAttribute(attr) 258 value = self.__getParam(profile, category, name)
259 return value if value!=None else node[1].getAttribute(attr)
251 else: 260 else:
252 return node[1].getAttribute(attr) 261 return node[1].getAttribute(attr)
253 262
254 def asyncGetParamA(self, name, category, attr="value", profile_key="@DEFAULT@", callback=None, errback=None): 263 def asyncGetParamA(self, name, category, attr="value", profile_key="@DEFAULT@", callback=None, errback=None):
255 """Helper method to get a specific attribute 264 """Helper method to get a specific attribute
264 error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category}) 273 error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category})
265 return None 274 return None
266 275
267 if node[0] == 'general': 276 if node[0] == 'general':
268 value = self.__getParam(None, category, name, 'general') 277 value = self.__getParam(None, category, name, 'general')
269 callback(value or node[1].getAttribute(attr)) 278 callback(value if value!=None else node[1].getAttribute(attr))
270 return 279 return
271 280
272 assert(node[0] == 'individual') 281 assert(node[0] == 'individual')
273 282
274 profile = self.getProfileName(profile_key) 283 profile = self.getProfileName(profile_key)
280 if attr != "value": 289 if attr != "value":
281 callback(node[1].getAttribute(attr)) 290 callback(node[1].getAttribute(attr))
282 return 291 return
283 default = node[1].getAttribute(attr) 292 default = node[1].getAttribute(attr)
284 try: 293 try:
285 callback(self.__getParam(profile, category, name) or default) 294 value = self.__getParam(profile, category, name)
295 callback(value if value!=None else default)
286 except ProfileNotInCacheError: 296 except ProfileNotInCacheError:
287 #We have to ask data to the storage manager 297 #We have to ask data to the storage manager
288 d = self.storage.getIndParam(category, name, profile) 298 d = self.storage.getIndParam(category, name, profile)
289 d.addCallback(callback) 299 d.addCallback(lambda value: callback(value if value!=None else default))
290 d.addErrback(lambda x:errback()) 300 d.addErrback(lambda x:errback())
291 301
292 def __getParam(self, profile, category, name, type='individual'): 302 def __getParam(self, profile, category, name, type='individual'):
293 """Return the param, or None if it doesn't exist 303 """Return the param, or None if it doesn't exist
294 @param profile: the profile name (not profile key, i.e. name and not something like @DEFAULT@) 304 @param profile: the profile name (not profile key, i.e. name and not something like @DEFAULT@)
310 """Construct xml for asked profile, filling values when needed 320 """Construct xml for asked profile, filling values when needed
311 /!\ as noticed in doc, don't forget to unlink the minidom.Document 321 /!\ as noticed in doc, don't forget to unlink the minidom.Document
312 @param profile: profile name (not key !) 322 @param profile: profile name (not key !)
313 @return: minidom.Document of the profile xml (cf warning above) 323 @return: minidom.Document of the profile xml (cf warning above)
314 """ 324 """
325 #TODO: asynchronous equivalent
315 prof_xml = minidom.parseString('<params/>') 326 prof_xml = minidom.parseString('<params/>')
316 cache = {} 327 cache = {}
317 328
318 for type_node in self.dom.documentElement.childNodes: 329 for type_node in self.dom.documentElement.childNodes:
319 if type_node.nodeName == 'general' or type_node.nodeName == 'individual': #we use all params, general and individual 330 if type_node.nodeName == 'general' or type_node.nodeName == 'individual': #we use all params, general and individual
339 if name not in dest_params: 350 if name not in dest_params:
340 dest_params[name] = param_node.cloneNode(True) 351 dest_params[name] = param_node.cloneNode(True)
341 dest_cat.appendChild(dest_params[name]) 352 dest_cat.appendChild(dest_params[name])
342 353
343 profile_value = self.__getParam(profile, category, name, type_node.nodeName) 354 profile_value = self.__getParam(profile, category, name, type_node.nodeName)
344 if profile_value: #there is a value for this profile, we must change the default 355 if profile_value!=None: #there is a value for this profile, we must change the default
345 dest_params[name].setAttribute('value', profile_value) 356 dest_params[name].setAttribute('value', profile_value)
346 if new_node: 357 if new_node:
347 prof_xml.documentElement.appendChild(dest_cat) 358 prof_xml.documentElement.appendChild(dest_cat)
348 return prof_xml 359 return prof_xml
349 360
350 361
351 def getParamsUI(self, profile_key): 362 def getParamsUI(self, profile_key):
352 """Return a SàT XMLUI for parameters, with given profile""" 363 """Return a SàT XMLUI for parameters, with given profile"""
364 #TODO: asynchronous equivalent
353 profile = self.getProfileName(profile_key) 365 profile = self.getProfileName(profile_key)
354 if not profile: 366 if not profile:
355 error(_("Asking params for inexistant profile")) 367 error(_("Asking params for inexistant profile"))
356 return "" 368 return ""
357 param_xml = self.getParams(profile) 369 param_xml = self.getParams(profile)
358 return paramsXml2xmlUI(param_xml) 370 return paramsXml2xmlUI(param_xml)
359 371
360 def getParams(self, profile_key): 372 def getParams(self, profile_key):
361 """Construct xml for asked profile 373 """Construct xml for asked profile
362 Take params xml as skeleton""" 374 Take params xml as skeleton"""
375 #TODO: asynchronous equivalent
363 profile = self.getProfileName(profile_key) 376 profile = self.getProfileName(profile_key)
364 if not profile: 377 if not profile:
365 error(_("Asking params for inexistant profile")) 378 error(_("Asking params for inexistant profile"))
366 return "" 379 return ""
367 prof_xml = self.__constructProfileXml(profile) 380 prof_xml = self.__constructProfileXml(profile)
371 return return_xml 384 return return_xml
372 385
373 def getParamsForCategory(self, category, profile_key): 386 def getParamsForCategory(self, category, profile_key):
374 """Return node's xml for selected category""" 387 """Return node's xml for selected category"""
375 #TODO: manage category of general type (without existant profile) 388 #TODO: manage category of general type (without existant profile)
389 #TODO: asynchronous equivalent
376 profile = self.getProfileName(profile_key) 390 profile = self.getProfileName(profile_key)
377 if not profile: 391 if not profile:
378 error(_("Asking params for inexistant profile")) 392 error(_("Asking params for inexistant profile"))
379 return "" 393 return ""
380 prof_xml = self.__constructProfileXml(profile) 394 prof_xml = self.__constructProfileXml(profile)
549 563
550 def load(self, init_defers): 564 def load(self, init_defers):
551 """Load parameters and all memory things from db 565 """Load parameters and all memory things from db
552 @param init_defers: list of deferred to wait before parameters are loaded""" 566 @param init_defers: list of deferred to wait before parameters are loaded"""
553 #parameters data 567 #parameters data
554 init_defers.append(self.params.loadGenData()) 568 init_defers.append(self.params.loadGenParams())
555 569
556 def loadIndividualParams(self, profile_key): 570 def loadIndividualParams(self, profile):
557 """Load individual parameters for a profile 571 """Load individual parameters for a profile
558 @param profile_key: %(doc_profile_key)s""" 572 @param profile: %(doc_profile)s"""
559 profile = self.getProfileName(profile_key)
560 if not profile:
561 error (_('Trying to load parameters for a non-existant profile'))
562 raise Exception("Profile doesn't exist")
563 return self.params.loadIndParams(profile) 573 return self.params.loadIndParams(profile)
564 574
575 def purgeProfile(self, profile):
576 """Delete cache of data of profile
577 @param profile: %(doc_profile)s"""
578 self.params.purgeProfile(profile)
565 579
566 def save(self): 580 def save(self):
567 """Save parameters and all memory things to file/db""" 581 """Save parameters and all memory things to file/db"""
568 #TODO: need to encrypt files (at least passwords !) and set permissions 582 #TODO: need to encrypt files (at least passwords !) and set permissions
569 param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+ 583 param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+