comparison src/memory/memory.py @ 777:5642939d254e

core, bridge: new method paramsRegisterApp to register frontend's specific parameters
author souliane <souliane@mailoo.org>
date Fri, 27 Dec 2013 13:28:26 +0100
parents bfabeedbf32e
children bfafed251b40
comparison
equal deleted inserted replaced
776:f89173f44850 777:5642939d254e
293 #the node already exists 293 #the node already exists
294 return node 294 return node
295 #the node is new 295 #the node is new
296 return None 296 return None
297 297
298 def updateParams(self, xml): 298 def updateParams(self, xml, security_limit=NO_SECURITY_LIMIT, app=''):
299 """import xml in parameters, do nothing if the param already exist 299 """import xml in parameters, do nothing if the param already exist
300 @param xml: parameters in xml form""" 300 If security_limit is specified and greater than -1, the parameters
301 src_dom = minidom.parseString(xml.encode('utf-8')) 301 that have a security level lower than security_limit are skipped.
302 @param xml: parameters in xml form
303 @param security_limit: -1 means no security then the higher the most secure
304 @param app: name of the frontend registering the parameters or empty value
305 """
306 src_parent = minidom.parseString(xml.encode('utf-8')).documentElement
307
308 def pre_process_app_node(src_parent, security_limit, app):
309 """Parameters that are registered from a frontend must be checked"""
310 to_remove = []
311 for type_node in src_parent.childNodes:
312 if type_node.nodeName != INDIVIDUAL:
313 to_remove.append(type_node) # accept individual parameters only
314 continue
315 for cat_node in type_node.childNodes:
316 if cat_node.nodeName != 'category':
317 to_remove.append(cat_node)
318 continue
319 to_remove_count = 0 # count the params to be removed from current category
320 for node in cat_node.childNodes:
321 if node.nodeName != "param" or not self.checkSecurityLimit(node, security_limit):
322 to_remove.append(node)
323 to_remove_count += 1
324 continue
325 node.setAttribute('app', app)
326 if len(cat_node.childNodes) == to_remove_count: # remove empty category
327 for dummy in xrange(0, to_remove_count):
328 to_remove.pop()
329 to_remove.append(cat_node)
330 for node in to_remove:
331 node.parentNode.removeChild(node)
302 332
303 def import_node(tgt_parent, src_parent): 333 def import_node(tgt_parent, src_parent):
304 for child in src_parent.childNodes: 334 for child in src_parent.childNodes:
305 if child.nodeName == '#text': 335 if child.nodeName == '#text':
306 continue 336 continue
313 tgt_parent.replaceChild(child, node) 343 tgt_parent.replaceChild(child, node)
314 else: 344 else:
315 # the node already exists, we recurse 1 more level 345 # the node already exists, we recurse 1 more level
316 import_node(node, child) 346 import_node(node, child)
317 347
318 import_node(self.dom.documentElement, src_dom.documentElement) 348 if app:
349 pre_process_app_node(src_parent, security_limit, app)
350 import_node(self.dom.documentElement, src_parent)
351
352 def paramsRegisterApp(self, xml, security_limit, app):
353 """Register frontend's specific parameters
354 If security_limit is specified and greater than -1, the parameters
355 that have a security level lower than security_limit are skipped.
356 @param xml: XML definition of the parameters to be added
357 @param security_limit: -1 means no security then the higher the most secure
358 @param app: name of the frontend registering the parameters
359 """
360 if not app:
361 warning(_("Trying to register frontends parameters with no specified app: aborted"))
362 return
363 if not hasattr(self, "frontends_cache"):
364 self.frontends_cache = {}
365 if app in self.frontends_cache:
366 warning(_("Trying to register twice frontends parameters for %(app)s: aborted" % {"app": app}))
367 return
368 self.updateParams(xml, security_limit, app)
369 debug("Frontends parameters registered for %(app)s" % {'app': app})
319 370
320 def __default_ok(self, value, name, category): 371 def __default_ok(self, value, name, category):
321 #FIXME: will not work with individual parameters 372 #FIXME: will not work with individual parameters
322 self.setParam(name, value, category) 373 self.setParam(name, value, category)
323 374
469 raise exceptions.ProfileNotInCacheError 520 raise exceptions.ProfileNotInCacheError
470 if (category, name) not in cache: 521 if (category, name) not in cache:
471 return None 522 return None
472 return cache[(category, name)] 523 return cache[(category, name)]
473 524
474 def __constructProfileXml(self, security_limit, profile): 525 def __constructProfileXml(self, security_limit, app, profile):
475 """Construct xml for asked profile, filling values when needed 526 """Construct xml for asked profile, filling values when needed
476 /!\ as noticed in doc, don't forget to unlink the minidom.Document 527 /!\ as noticed in doc, don't forget to unlink the minidom.Document
477 @security_limit: NO_SECURITY_LIMIT (-1) to return all the params. 528 @param security_limit: NO_SECURITY_LIMIT (-1) to return all the params.
478 Otherwise sole the params which have a security level defined *and* 529 Otherwise sole the params which have a security level defined *and*
479 lower or equal to the specified value are returned. 530 lower or equal to the specified value are returned.
531 @param app: name of the frontend requesting the parameters, or '' to get all parameters
480 @param profile: profile name (not key !) 532 @param profile: profile name (not key !)
481 @return: a deferred that fire a minidom.Document of the profile xml (cf warning above) 533 @return: a deferred that fire a minidom.Document of the profile xml (cf warning above)
482 """ 534 """
483 535
484 def constructProfile(ignore, profile_cache): 536 def constructProfile(ignore, profile_cache):
516 # we have to merge new params (we are parsing individual parameters, we have to add them 568 # we have to merge new params (we are parsing individual parameters, we have to add them
517 # to the previously parsed general ones) 569 # to the previously parsed general ones)
518 name = param_node.getAttribute('name') 570 name = param_node.getAttribute('name')
519 if not self.checkSecurityLimit(param_node, security_limit): 571 if not self.checkSecurityLimit(param_node, security_limit):
520 continue 572 continue
573 if not self.checkApp(param_node, app):
574 continue
521 if name not in dest_params: 575 if name not in dest_params:
522 # this is reached when a previous category exists 576 # this is reached when a previous category exists
523 dest_params[name] = param_node.cloneNode(True) 577 dest_params[name] = param_node.cloneNode(True)
524 dest_cat.appendChild(dest_params[name]) 578 dest_cat.appendChild(dest_params[name])
525 579
549 profile_cache = {} 603 profile_cache = {}
550 d = self.loadIndParams(profile, profile_cache) 604 d = self.loadIndParams(profile, profile_cache)
551 605
552 return d.addCallback(constructProfile, profile_cache) 606 return d.addCallback(constructProfile, profile_cache)
553 607
554 def getParamsUI(self, security_limit, profile_key): 608 def getParamsUI(self, security_limit, app, profile_key):
555 """Return a SàT XMLUI for parameters, with given profile""" 609 """
610 @param security_limit: NO_SECURITY_LIMIT (-1) to return all the params.
611 Otherwise sole the params which have a security level defined *and*
612 lower or equal to the specified value are returned.
613 @param app: name of the frontend requesting the parameters, or '' to get all parameters
614 @param profile_key: Profile key which can be either a magic (eg: @DEFAULT@) or the name of an existing profile.
615 @return: a SàT XMLUI for parameters
616 """
556 profile = self.getProfileName(profile_key) 617 profile = self.getProfileName(profile_key)
557 if not profile: 618 if not profile:
558 error(_("Asking params for inexistant profile")) 619 error(_("Asking params for inexistant profile"))
559 return "" 620 return ""
560 d = self.getParams(security_limit, profile) 621 d = self.getParams(security_limit, app, profile)
561 return d.addCallback(lambda param_xml: paramsXml2xmlUI(param_xml)) 622 return d.addCallback(lambda param_xml: paramsXml2xmlUI(param_xml))
562 623
563 def getParams(self, security_limit, profile_key): 624 def getParams(self, security_limit, app, profile_key):
564 """Construct xml for asked profile 625 """Construct xml for asked profile, take params xml as skeleton
565 Take params xml as skeleton""" 626 @param security_limit: NO_SECURITY_LIMIT (-1) to return all the params.
627 Otherwise sole the params which have a security level defined *and*
628 lower or equal to the specified value are returned.
629 @param app: name of the frontend requesting the parameters, or '' to get all parameters
630 @param profile_key: Profile key which can be either a magic (eg: @DEFAULT@) or the name of an existing profile.
631 @return: XML of parameters
632 """
566 profile = self.getProfileName(profile_key) 633 profile = self.getProfileName(profile_key)
567 if not profile: 634 if not profile:
568 error(_("Asking params for inexistant profile")) 635 error(_("Asking params for inexistant profile"))
569 return "" 636 return ""
570 637
571 def returnXML(prof_xml): 638 def returnXML(prof_xml):
572 return_xml = prof_xml.toxml() 639 return_xml = prof_xml.toxml()
573 prof_xml.unlink() 640 prof_xml.unlink()
574 return '\n'.join((line for line in return_xml.split('\n') if line)) 641 return '\n'.join((line for line in return_xml.split('\n') if line))
575 642
576 return self.__constructProfileXml(security_limit, profile).addCallback(returnXML) 643 return self.__constructProfileXml(security_limit, app, profile).addCallback(returnXML)
577 644
578 def getParamsForCategory(self, category, security_limit, profile_key): 645 def getParamsForCategory(self, category, security_limit, app, profile_key):
579 """Return node's xml for selected category""" 646 """
647 @param category: the desired category
648 @param security_limit: NO_SECURITY_LIMIT (-1) to return all the params.
649 Otherwise sole the params which have a security level defined *and*
650 lower or equal to the specified value are returned.
651 @param app: name of the frontend requesting the parameters, or '' to get all parameters
652 @param profile_key: Profile key which can be either a magic (eg: @DEFAULT@) or the name of an existing profile.
653 @return: node's xml for selected category
654 """
580 #TODO: manage category of general type (without existant profile) 655 #TODO: manage category of general type (without existant profile)
581 profile = self.getProfileName(profile_key) 656 profile = self.getProfileName(profile_key)
582 if not profile: 657 if not profile:
583 error(_("Asking params for inexistant profile")) 658 error(_("Asking params for inexistant profile"))
584 return "" 659 return ""
591 return result 666 return result
592 667
593 prof_xml.unlink() 668 prof_xml.unlink()
594 return "<category />" 669 return "<category />"
595 670
596 d = self.__constructProfileXml(security_limit, profile) 671 d = self.__constructProfileXml(security_limit, app, profile)
597 return d.addCallback(returnCategoryXml) 672 return d.addCallback(returnCategoryXml)
598 673
599 def _getParamNode(self, name, category, type_="@ALL@"): # FIXME: is type_ useful ? 674 def _getParamNode(self, name, category, type_="@ALL@"): # FIXME: is type_ useful ?
600 """Return a node from the param_xml 675 """Return a node from the param_xml
601 @param name: name of the node 676 @param name: name of the node
677 return True 752 return True
678 if node.hasAttribute("security"): 753 if node.hasAttribute("security"):
679 if int(node.getAttribute("security")) <= security_limit: 754 if int(node.getAttribute("security")) <= security_limit:
680 return True 755 return True
681 return False 756 return False
757
758 def checkApp(self, node, app):
759 """Check the given node against the given app.
760 @param node: parameter node
761 @param app: name of the frontend requesting the parameters, or '' to get all parameters
762 @return: True if this node concerns the given app.
763 """
764 if not app or not node.hasAttribute("app"):
765 return True
766 return node.getAttribute("app") == app
682 767
683 768
684 class Memory(object): 769 class Memory(object):
685 """This class manage all persistent informations""" 770 """This class manage all persistent informations"""
686 771
1061 return self.params.asyncGetParamA(name, category, attr, security_limit, profile_key) 1146 return self.params.asyncGetParamA(name, category, attr, security_limit, profile_key)
1062 1147
1063 def asyncGetStringParamA(self, name, category, attr="value", security_limit=NO_SECURITY_LIMIT, profile_key='@NONE@'): 1148 def asyncGetStringParamA(self, name, category, attr="value", security_limit=NO_SECURITY_LIMIT, profile_key='@NONE@'):
1064 return self.params.asyncGetStringParamA(name, category, attr, security_limit, profile_key) 1149 return self.params.asyncGetStringParamA(name, category, attr, security_limit, profile_key)
1065 1150
1066 def getParamsUI(self, security_limit=NO_SECURITY_LIMIT, profile_key='@NONE@'): 1151 def getParamsUI(self, security_limit=NO_SECURITY_LIMIT, app='', profile_key='@NONE@'):
1067 return self.params.getParamsUI(security_limit, profile_key) 1152 return self.params.getParamsUI(security_limit, app, profile_key)
1068 1153
1069 def getParams(self, security_limit=NO_SECURITY_LIMIT, profile_key='@NONE@'): 1154 def getParams(self, security_limit=NO_SECURITY_LIMIT, app='', profile_key='@NONE@'):
1070 return self.params.getParams(security_limit, profile_key) 1155 return self.params.getParams(security_limit, app, profile_key)
1071 1156
1072 def getParamsForCategory(self, category, security_limit=NO_SECURITY_LIMIT, profile_key='@NONE@'): 1157 def getParamsForCategory(self, category, security_limit=NO_SECURITY_LIMIT, app='', profile_key='@NONE@'):
1073 return self.params.getParamsForCategory(category, security_limit, profile_key) 1158 return self.params.getParamsForCategory(category, security_limit, app, profile_key)
1074 1159
1075 def getParamsCategories(self): 1160 def getParamsCategories(self):
1076 return self.params.getParamsCategories() 1161 return self.params.getParamsCategories()
1077 1162
1078 def setParam(self, name, value, category, security_limit=NO_SECURITY_LIMIT, profile_key='@NONE@'): 1163 def setParam(self, name, value, category, security_limit=NO_SECURITY_LIMIT, profile_key='@NONE@'):
1079 return self.params.setParam(name, value, category, security_limit, profile_key) 1164 return self.params.setParam(name, value, category, security_limit, profile_key)
1080 1165
1081 def updateParams(self, xml): 1166 def updateParams(self, xml):
1082 return self.params.updateParams(xml) 1167 return self.params.updateParams(xml)
1083 1168
1169 def paramsRegisterApp(self, xml, security_limit=NO_SECURITY_LIMIT, app=''):
1170 return self.params.paramsRegisterApp(xml, security_limit, app)
1171
1084 def setDefault(self, name, category, callback, errback=None): 1172 def setDefault(self, name, category, callback, errback=None):
1085 return self.params.setDefault(name, category, callback, errback) 1173 return self.params.setDefault(name, category, callback, errback)