comparison src/memory/memory.py @ 634:ca2cae6b2c6d

core: security attribute added to the parameters - getParams, getParamsUI and getParamsForCategory have a security_limit parameter to filter - parameters with security = 0 can be retrieved/modified from Libervia
author souliane <souliane@mailoo.org>
date Thu, 05 Sep 2013 20:28:44 +0200
parents 5646ecd3e35e
children eff8772fd472
comparison
equal deleted inserted replaced
633:6a29a4d574bd 634:ca2cae6b2c6d
370 raise exceptions.ProfileNotInCacheError 370 raise exceptions.ProfileNotInCacheError
371 if (category, name) not in cache: 371 if (category, name) not in cache:
372 return None 372 return None
373 return cache[(category, name)] 373 return cache[(category, name)]
374 374
375 def __constructProfileXml(self, profile): 375 def __constructProfileXml(self, security_limit, profile):
376 """Construct xml for asked profile, filling values when needed 376 """Construct xml for asked profile, filling values when needed
377 /!\ as noticed in doc, don't forget to unlink the minidom.Document 377 /!\ as noticed in doc, don't forget to unlink the minidom.Document
378 @security_limit: -1 to return all the params. Otherwise sole the
379 params which have a security level defined *and* lower or equal to
380 the specified value are returned.
378 @param profile: profile name (not key !) 381 @param profile: profile name (not key !)
379 @return: a deferred that fire a minidom.Document of the profile xml (cf warning above) 382 @return: a deferred that fire a minidom.Document of the profile xml (cf warning above)
380 """ 383 """
381 384
382 def constructProfile(ignore, profile_cache): 385 def constructProfile(ignore, profile_cache):
386
387 def filterParam(node):
388 """Filter with security level.
389 @return: True is this param must be filtered"""
390 if security_limit < 0:
391 return False
392 if not node.hasAttribute('security'):
393 debug("filtered param: %s (no security set)"
394 % node.getAttribute("name"))
395 return True
396 if int(node.getAttribute('security')) > security_limit:
397 debug("filtered param: %s (security level > %i)"
398 % (node.getAttribute("name"), security_limit))
399 return True
400 return False
401
402 # init the result document
383 prof_xml = minidom.parseString('<params/>') 403 prof_xml = minidom.parseString('<params/>')
384 cache = {} 404 cache = {}
385 405
386 for type_node in self.dom.documentElement.childNodes: 406 for type_node in self.dom.documentElement.childNodes:
387 if type_node.nodeName == 'general' or type_node.nodeName == 'individual': # we use all params, general and individual 407 if type_node.nodeName != 'general' and type_node.nodeName != 'individual':
388 for cat_node in type_node.childNodes: 408 continue
389 if cat_node.nodeName == 'category': 409 # we use all params, general and individual
390 category = cat_node.getAttribute('name') 410 for cat_node in type_node.childNodes:
391 if category not in cache: 411 if cat_node.nodeName != 'category':
392 cache[category] = dest_cat = cat_node.cloneNode(True) # we make a copy for the new xml 412 continue
393 new_node = True 413 category = cat_node.getAttribute('name')
394 else: 414 dest_params = {} # result (merged) params for category
395 dest_cat = cache[category] 415 if category not in cache:
396 new_node = False # It's not a new node, we will merge information 416 # we make a copy for the new xml
397 params = cat_node.getElementsByTagName("param") 417 cache[category] = dest_cat = cat_node.cloneNode(True)
398 dest_params = {} 418 for node in dest_cat.childNodes:
399 for node in dest_cat.childNodes: 419 if node.nodeName != "param":
400 if node.nodeName != "param": 420 continue
401 continue 421 if filterParam(node):
402 dest_params[node.getAttribute('name')] = node 422 dest_cat.removeChild(node)
403 423 continue
404 for param_node in params: 424 dest_params[node.getAttribute('name')] = node
405 name = param_node.getAttribute('name') 425 new_node = True
406 426 else:
407 if name not in dest_params: 427 # It's not a new node, we use the previously cloned one
408 dest_params[name] = param_node.cloneNode(True) 428 dest_cat = cache[category]
409 dest_cat.appendChild(dest_params[name]) 429 new_node = False
410 430 params = cat_node.getElementsByTagName("param")
411 profile_value = self.__getParam(profile, category, name, type_node.nodeName, cache=profile_cache) 431
412 if profile_value is not None: # there is a value for this profile, we must change the default 432 for param_node in params:
413 dest_params[name].setAttribute('value', profile_value) 433 # we have to merge new params (we are parsing individual parameters, we have to add them
414 if new_node: 434 # to the previously parsed general ones)
415 prof_xml.documentElement.appendChild(dest_cat) 435 name = param_node.getAttribute('name')
436 if filterParam(param_node):
437 continue
438 if name not in dest_params:
439 # this is reached when a previous category exists
440 dest_params[name] = param_node.cloneNode(True)
441 dest_cat.appendChild(dest_params[name])
442
443 profile_value = self.__getParam(profile, category,
444 name, type_node.nodeName,
445 cache=profile_cache)
446 if profile_value is not None:
447 # there is a value for this profile, we must change the default
448 dest_params[name].setAttribute('value', profile_value)
449 if new_node:
450 prof_xml.documentElement.appendChild(dest_cat)
451
452 to_remove = []
453 for cat_node in prof_xml.documentElement.childNodes:
454 # we remove empty categories
455 if cat_node.getElementsByTagName("param").length == 0:
456 to_remove.append(cat_node)
457 for node in to_remove:
458 prof_xml.documentElement.removeChild(node)
416 return prof_xml 459 return prof_xml
417 460
418 if profile in self.params: 461 if profile in self.params:
419 d = defer.succeed(None) 462 d = defer.succeed(None)
420 profile_cache = self.params[profile] 463 profile_cache = self.params[profile]
423 profile_cache = {} 466 profile_cache = {}
424 d = self.loadIndParams(profile, profile_cache) 467 d = self.loadIndParams(profile, profile_cache)
425 468
426 return d.addCallback(constructProfile, profile_cache) 469 return d.addCallback(constructProfile, profile_cache)
427 470
428 def getParamsUI(self, profile_key): 471 def getParamsUI(self, security_limit, profile_key):
429 """Return a SàT XMLUI for parameters, with given profile""" 472 """Return a SàT XMLUI for parameters, with given profile"""
430 profile = self.getProfileName(profile_key) 473 profile = self.getProfileName(profile_key)
431 if not profile: 474 if not profile:
432 error(_("Asking params for inexistant profile")) 475 error(_("Asking params for inexistant profile"))
433 return "" 476 return ""
434 d = self.getParams(profile) 477 d = self.getParams(security_limit, profile)
435 return d.addCallback(lambda param_xml: paramsXml2xmlUI(param_xml)) 478 return d.addCallback(lambda param_xml: paramsXml2xmlUI(param_xml))
436 479
437 def getParams(self, profile_key): 480 def getParams(self, security_limit, profile_key):
438 """Construct xml for asked profile 481 """Construct xml for asked profile
439 Take params xml as skeleton""" 482 Take params xml as skeleton"""
440 profile = self.getProfileName(profile_key) 483 profile = self.getProfileName(profile_key)
441 if not profile: 484 if not profile:
442 error(_("Asking params for inexistant profile")) 485 error(_("Asking params for inexistant profile"))
443 return "" 486 return ""
444 487
445 def returnXML(prof_xml): 488 def returnXML(prof_xml):
446 return_xml = prof_xml.toxml() 489 return_xml = prof_xml.toxml()
447 prof_xml.unlink() 490 prof_xml.unlink()
448 return return_xml 491 return '\n'.join([line for line in return_xml.split('\n')])
449 492
450 return self.__constructProfileXml(profile).addCallback(returnXML) 493 return self.__constructProfileXml(security_limit, profile).addCallback(returnXML)
451 494
452 def getParamsForCategory(self, category, profile_key): 495 def getParamsForCategory(self, category, security_limit, profile_key):
453 """Return node's xml for selected category""" 496 """Return node's xml for selected category"""
454 #TODO: manage category of general type (without existant profile) 497 #TODO: manage category of general type (without existant profile)
455 profile = self.getProfileName(profile_key) 498 profile = self.getProfileName(profile_key)
456 if not profile: 499 if not profile:
457 error(_("Asking params for inexistant profile")) 500 error(_("Asking params for inexistant profile"))
465 return result 508 return result
466 509
467 prof_xml.unlink() 510 prof_xml.unlink()
468 return "<category />" 511 return "<category />"
469 512
470 d = self.__constructProfileXml(profile) 513 d = self.__constructProfileXml(security_limit, profile)
471 return d.addCallback(returnCategoryXml) 514 return d.addCallback(returnCategoryXml)
472 515
473 def __getParamNode(self, name, category, _type="@ALL@"): # FIXME: is _type useful ? 516 def __getParamNode(self, name, category, _type="@ALL@"): # FIXME: is _type useful ?
474 """Return a node from the param_xml 517 """Return a node from the param_xml
475 @param name: name of the node 518 @param name: name of the node
849 return self.params.asyncGetParamA(name, category, attr, profile_key) 892 return self.params.asyncGetParamA(name, category, attr, profile_key)
850 893
851 def asyncGetStringParamA(self, name, category, attr="value", profile_key='@DEFAULT@'): 894 def asyncGetStringParamA(self, name, category, attr="value", profile_key='@DEFAULT@'):
852 return self.params.asyncGetStringParamA(name, category, attr, profile_key) 895 return self.params.asyncGetStringParamA(name, category, attr, profile_key)
853 896
854 def getParamsUI(self, profile_key): 897 def getParamsUI(self, security_limit, profile_key):
855 return self.params.getParamsUI(profile_key) 898 return self.params.getParamsUI(security_limit, profile_key)
856 899
857 def getParams(self, profile_key): 900 def getParams(self, security_limit, profile_key):
858 return self.params.getParams(profile_key) 901 return self.params.getParams(security_limit, profile_key)
859 902
860 def getParamsForCategory(self, category, profile_key): 903 def getParamsForCategory(self, category, security_limit, profile_key):
861 return self.params.getParamsForCategory(category, profile_key) 904 return self.params.getParamsForCategory(category, security_limit, profile_key)
862 905
863 def getParamsCategories(self): 906 def getParamsCategories(self):
864 return self.params.getParamsCategories() 907 return self.params.getParamsCategories()
865 908
866 def setParam(self, name, value, category, profile_key): 909 def setParam(self, name, value, category, profile_key):