Mercurial > libervia-backend
diff 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 |
line wrap: on
line diff
--- a/src/memory/memory.py Sun Sep 08 18:05:19 2013 +0200 +++ b/src/memory/memory.py Thu Sep 05 20:28:44 2013 +0200 @@ -372,47 +372,90 @@ return None return cache[(category, name)] - def __constructProfileXml(self, profile): + def __constructProfileXml(self, security_limit, profile): """Construct xml for asked profile, filling values when needed /!\ as noticed in doc, don't forget to unlink the minidom.Document + @security_limit: -1 to return all the params. Otherwise sole the + params which have a security level defined *and* lower or equal to + the specified value are returned. @param profile: profile name (not key !) @return: a deferred that fire a minidom.Document of the profile xml (cf warning above) """ def constructProfile(ignore, profile_cache): + + def filterParam(node): + """Filter with security level. + @return: True is this param must be filtered""" + if security_limit < 0: + return False + if not node.hasAttribute('security'): + debug("filtered param: %s (no security set)" + % node.getAttribute("name")) + return True + if int(node.getAttribute('security')) > security_limit: + debug("filtered param: %s (security level > %i)" + % (node.getAttribute("name"), security_limit)) + return True + return False + + # init the result document prof_xml = minidom.parseString('<params/>') cache = {} for type_node in self.dom.documentElement.childNodes: - if type_node.nodeName == 'general' or type_node.nodeName == 'individual': # we use all params, general and individual - for cat_node in type_node.childNodes: - if cat_node.nodeName == 'category': - category = cat_node.getAttribute('name') - if category not in cache: - cache[category] = dest_cat = cat_node.cloneNode(True) # we make a copy for the new xml - new_node = True - else: - dest_cat = cache[category] - new_node = False # It's not a new node, we will merge information - params = cat_node.getElementsByTagName("param") - dest_params = {} - for node in dest_cat.childNodes: - if node.nodeName != "param": - continue - dest_params[node.getAttribute('name')] = node + if type_node.nodeName != 'general' and type_node.nodeName != 'individual': + continue + # we use all params, general and individual + for cat_node in type_node.childNodes: + if cat_node.nodeName != 'category': + continue + category = cat_node.getAttribute('name') + dest_params = {} # result (merged) params for category + if category not in cache: + # we make a copy for the new xml + cache[category] = dest_cat = cat_node.cloneNode(True) + for node in dest_cat.childNodes: + if node.nodeName != "param": + continue + if filterParam(node): + dest_cat.removeChild(node) + continue + dest_params[node.getAttribute('name')] = node + new_node = True + else: + # It's not a new node, we use the previously cloned one + dest_cat = cache[category] + new_node = False + params = cat_node.getElementsByTagName("param") - for param_node in params: - name = param_node.getAttribute('name') + for param_node in params: + # we have to merge new params (we are parsing individual parameters, we have to add them + # to the previously parsed general ones) + name = param_node.getAttribute('name') + if filterParam(param_node): + continue + if name not in dest_params: + # this is reached when a previous category exists + dest_params[name] = param_node.cloneNode(True) + dest_cat.appendChild(dest_params[name]) - if name not in dest_params: - dest_params[name] = param_node.cloneNode(True) - dest_cat.appendChild(dest_params[name]) + profile_value = self.__getParam(profile, category, + name, type_node.nodeName, + cache=profile_cache) + if profile_value is not None: + # there is a value for this profile, we must change the default + dest_params[name].setAttribute('value', profile_value) + if new_node: + prof_xml.documentElement.appendChild(dest_cat) - profile_value = self.__getParam(profile, category, name, type_node.nodeName, cache=profile_cache) - if profile_value is not None: # there is a value for this profile, we must change the default - dest_params[name].setAttribute('value', profile_value) - if new_node: - prof_xml.documentElement.appendChild(dest_cat) + to_remove = [] + for cat_node in prof_xml.documentElement.childNodes: + # we remove empty categories + if cat_node.getElementsByTagName("param").length == 0: + to_remove.append(cat_node) + for node in to_remove: + prof_xml.documentElement.removeChild(node) return prof_xml if profile in self.params: @@ -425,16 +468,16 @@ return d.addCallback(constructProfile, profile_cache) - def getParamsUI(self, profile_key): + def getParamsUI(self, security_limit, profile_key): """Return a SàT XMLUI for parameters, with given profile""" profile = self.getProfileName(profile_key) if not profile: error(_("Asking params for inexistant profile")) return "" - d = self.getParams(profile) + d = self.getParams(security_limit, profile) return d.addCallback(lambda param_xml: paramsXml2xmlUI(param_xml)) - def getParams(self, profile_key): + def getParams(self, security_limit, profile_key): """Construct xml for asked profile Take params xml as skeleton""" profile = self.getProfileName(profile_key) @@ -445,11 +488,11 @@ def returnXML(prof_xml): return_xml = prof_xml.toxml() prof_xml.unlink() - return return_xml + return '\n'.join([line for line in return_xml.split('\n')]) - return self.__constructProfileXml(profile).addCallback(returnXML) + return self.__constructProfileXml(security_limit, profile).addCallback(returnXML) - def getParamsForCategory(self, category, profile_key): + def getParamsForCategory(self, category, security_limit, profile_key): """Return node's xml for selected category""" #TODO: manage category of general type (without existant profile) profile = self.getProfileName(profile_key) @@ -467,7 +510,7 @@ prof_xml.unlink() return "<category />" - d = self.__constructProfileXml(profile) + d = self.__constructProfileXml(security_limit, profile) return d.addCallback(returnCategoryXml) def __getParamNode(self, name, category, _type="@ALL@"): # FIXME: is _type useful ? @@ -851,14 +894,14 @@ def asyncGetStringParamA(self, name, category, attr="value", profile_key='@DEFAULT@'): return self.params.asyncGetStringParamA(name, category, attr, profile_key) - def getParamsUI(self, profile_key): - return self.params.getParamsUI(profile_key) + def getParamsUI(self, security_limit, profile_key): + return self.params.getParamsUI(security_limit, profile_key) - def getParams(self, profile_key): - return self.params.getParams(profile_key) + def getParams(self, security_limit, profile_key): + return self.params.getParams(security_limit, profile_key) - def getParamsForCategory(self, category, profile_key): - return self.params.getParamsForCategory(category, profile_key) + def getParamsForCategory(self, category, security_limit, profile_key): + return self.params.getParamsForCategory(category, security_limit, profile_key) def getParamsCategories(self): return self.params.getParamsCategories()