# HG changeset patch # User Goffi # Date 1385655788 -3600 # Node ID 04aabc3f2684c382e02eed592055bc75d8d1ba29 # Parent 0077912bc9ba8f6652d9288910e726c434d18c81 core (memory): fixed setDefault behaviour + minor refactoring diff -r 0077912bc9ba -r 04aabc3f2684 src/memory/memory.py --- a/src/memory/memory.py Tue Nov 26 18:30:35 2013 +0100 +++ b/src/memory/memory.py Thu Nov 28 17:23:08 2013 +0100 @@ -34,6 +34,8 @@ SAVEFILE_DATABASE = "/sat.db" NO_SECURITY_LIMIT = -1 +INDIVIDUAL = "individual" +GENERAL = "general" class Params(object): @@ -239,8 +241,8 @@ import_node(self.dom.documentElement, src_dom.documentElement) def __default_ok(self, value, name, category): - #FIXME: gof: will not work with individual parameters - self.setParam(name, value, category) # FIXME: better to set param xml value ??? + #FIXME: will not work with individual parameters + self.setParam(name, value, category) def __default_ko(self, failure, name, category): error(_("Can't determine default value for [%(category)s/%(name)s]: %(reason)s") % {'category': category, 'name': name, 'reason': str(failure.value)}) @@ -254,17 +256,23 @@ @param errback: must manage the error with args failure, name, category """ #TODO: send signal param update if value changed - node = self.__getParamNode(name, category, '@ALL@') + #TODO: manage individual paramaters + debug ("setDefault called for %(category)s/%(name)s" % {"category": category, "name": name}) + node = self._getParamNode(name, category, '@ALL@') if not node: error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category}) return if node[1].getAttribute('default_cb') == 'yes': - del node[1].attributes['default_cb'] - d = defer.maybeDeferred(callback) - d.addCallback(self.__default_ok, name, category) - d.addErrback(errback or self.__default_ko, name, category) + # del node[1].attributes['default_cb'] # default_cb is not used anymore as a flag to know if we have to set the default value, + # and we can still use it later e.g. to call a generic setDefault method + value = self._getParam(category, name, GENERAL) + if value is None: # no value set by the user: we have the default value + debug ("Default value to set, using callback") + d = defer.maybeDeferred(callback) + d.addCallback(self.__default_ok, name, category) + d.addErrback(errback or self.__default_ko, name, category) - def __getAttr(self, node, attr, value): + def _getAttr(self, node, attr, value): """ get attribute value @param node: XML param node @param attr: name of the attribute to get (e.g.: 'value' or 'type') @@ -295,16 +303,16 @@ @return: attribute""" #FIXME: looks really dirty and buggy, need to be reviewed/refactored - node = self.__getParamNode(name, category) + node = self._getParamNode(name, category) if not node: error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category}) raise exceptions.NotFound - if node[0] == 'general': - value = self.__getParam(None, category, name, 'general') - return self.__getAttr(node[1], attr, value) + if node[0] == GENERAL: + value = self._getParam(category, name, GENERAL) + return self._getAttr(node[1], attr, value) - assert node[0] == 'individual' + assert node[0] == INDIVIDUAL profile = self.getProfileName(profile_key) if not profile: @@ -316,8 +324,8 @@ raise exceptions.NotConnectedProfileError(profile) if attr == "value": - value = self.__getParam(profile, category, name) - return self.__getAttr(node[1], attr, value) + value = self._getParam(category, name, profile=profile) + return self._getAttr(node[1], attr, value) def asyncGetStringParamA(self, name, category, attr="value", security_limit=NO_SECURITY_LIMIT, profile_key="@NONE@"): d = self.asyncGetParamA(name, category, attr, security_limit, profile_key) @@ -330,7 +338,7 @@ @param category: category of the parameter @param attr: name of the attribute (default: "value") @param profile: owner of the param (@ALL@ for everyone)""" - node = self.__getParamNode(name, category) + node = self._getParamNode(name, category) if not node: error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category}) return None @@ -340,11 +348,11 @@ % (name, category))) return None - if node[0] == 'general': - value = self.__getParam(None, category, name, 'general') - return defer.succeed(self.__getAttr(node[1], attr, value)) + if node[0] == GENERAL: + value = self._getParam(category, name, GENERAL) + return defer.succeed(self._getAttr(node[1], attr, value)) - assert node[0] == 'individual' + assert node[0] == INDIVIDUAL profile = self.getProfileName(profile_key) if not profile: @@ -354,27 +362,29 @@ if attr != "value": return defer.succeed(node[1].getAttribute(attr)) try: - value = self.__getParam(profile, category, name) - return defer.succeed(self.__getAttr(node[1], attr, value)) + value = self._getParam(category, name, profile=profile) + return defer.succeed(self._getAttr(node[1], attr, value)) except exceptions.ProfileNotInCacheError: #We have to ask data to the storage manager d = self.storage.getIndParam(category, name, profile) - return d.addCallback(lambda value: self.__getAttr(node[1], attr, value)) + return d.addCallback(lambda value: self._getAttr(node[1], attr, value)) - def __getParam(self, profile, category, name, _type='individual', cache=None): + def _getParam(self, category, name, type_=INDIVIDUAL, cache=None, profile="@NONE@"): """Return the param, or None if it doesn't exist - @param profile: the profile name (not profile key, i.e. name and not something like @DEFAULT@) @param category: param category @param name: param name - @param _type: "general" or "individual" + @param type_: GENERAL or INDIVIDUAL @param cache: temporary cache, to use when profile is not logged + @param profile: the profile name (not profile key, i.e. name and not something like @DEFAULT@) @return: param value or None if it doesn't exist """ - if _type == 'general': + if type_ == GENERAL: if (category, name) in self.params_gen: return self.params_gen[(category, name)] return None # This general param has the default value - assert (_type == 'individual') + assert (type_ == INDIVIDUAL) + if profile == "@NONE@": + raise exceptions.ProfileNotSetError if profile in self.params: cache = self.params[profile] # if profile is in main cache, we use it, # ignoring the temporary cache @@ -400,7 +410,7 @@ cache = {} for type_node in self.dom.documentElement.childNodes: - if type_node.nodeName != 'general' and type_node.nodeName != 'individual': + 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: @@ -436,9 +446,9 @@ dest_params[name] = param_node.cloneNode(True) dest_cat.appendChild(dest_params[name]) - profile_value = self.__getParam(profile, category, + profile_value = self._getParam(category, name, type_node.nodeName, - cache=profile_cache) + cache=profile_cache, profile=profile) 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) @@ -509,19 +519,19 @@ d = self.__constructProfileXml(security_limit, profile) return d.addCallback(returnCategoryXml) - def __getParamNode(self, name, category, _type="@ALL@"): # FIXME: is _type useful ? + def _getParamNode(self, name, category, type_="@ALL@"): # FIXME: is type_ useful ? """Return a node from the param_xml @param name: name of the node @param category: category of the node - @_type: keyword for search: + @type_: keyword for search: @ALL@ search everywhere @GENERAL@ only search in general type @INDIVIDUAL@ only search in individual type @return: a tuple with the node type and the the node, or None if not found""" for type_node in self.dom.documentElement.childNodes: - if (((_type == "@ALL@" or _type == "@GENERAL@") and type_node.nodeName == 'general') - or ((_type == "@ALL@" or _type == "@INDIVIDUAL@") and type_node.nodeName == 'individual')): + if (((type_ == "@ALL@" or type_ == "@GENERAL@") and type_node.nodeName == GENERAL) + or ((type_ == "@ALL@" or type_ == "@INDIVIDUAL@") and type_node.nodeName == INDIVIDUAL)): for node in type_node.getElementsByTagName('category'): if node.getAttribute("name") == category: params = node.getElementsByTagName("param") @@ -548,7 +558,7 @@ error(_('Trying to set parameter for an unknown profile')) return # TODO: throw an error - node = self.__getParamNode(name, category, '@ALL@') + node = self._getParamNode(name, category, '@ALL@') if not node: error(_('Requesting an unknown parameter (%(category)s/%(name)s)') % {'category': category, 'name': name}) @@ -559,7 +569,7 @@ % (name, category))) return - if node[0] == 'general': + if node[0] == GENERAL: self.params_gen[(category, name)] = value self.storage.setGenParam(category, name, value) for profile in self.storage.getProfilesList(): @@ -568,11 +578,11 @@ self.host.trigger.point("paramUpdateTrigger", name, value, category, node[0], profile) return - assert (node[0] == 'individual') + assert (node[0] == INDIVIDUAL) assert (profile_key != "@NONE@") - _type = node[1].getAttribute("type") - if _type == "button": + type_ = node[1].getAttribute("type") + if type_ == "button": print "clique", node.toxml() else: if self.host.isConnected(profile): # key can not exists if profile is not connected @@ -724,11 +734,11 @@ @param name: Name of the profile""" return self.params.deleteProfile(name) - def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile="@NONE@"): + def addToHistory(self, from_jid, to_jid, message, type_='chat', extra=None, timestamp=None, profile="@NONE@"): assert profile != "@NONE@" if extra is None: extra = {} - return self.storage.addToHistory(from_jid, to_jid, message, _type, extra, timestamp, profile) + return self.storage.addToHistory(from_jid, to_jid, message, type_, extra, timestamp, profile) def getHistory(self, from_jid, to_jid, limit=0, between=True, profile="@NONE@"): assert profile != "@NONE@" @@ -742,29 +752,29 @@ self.server_features[profile] = [] self.server_features[profile].append(feature) - def addServerIdentity(self, category, _type, entity, profile): + def addServerIdentity(self, category, type_, entity, profile): """Add an identity discovered from server @param feature: string of the feature @param profile: which profile is using this server ?""" if not profile in self.server_identities: self.server_identities[profile] = {} - if (category, _type) not in self.server_identities[profile]: - self.server_identities[profile][(category, _type)] = set() - self.server_identities[profile][(category, _type)].add(entity) + if (category, type_) not in self.server_identities[profile]: + self.server_identities[profile][(category, type_)] = set() + self.server_identities[profile][(category, type_)].add(entity) - def getServerServiceEntities(self, category, _type, profile): + def getServerServiceEntities(self, category, type_, profile): """Return all available entities for a service""" if profile in self.server_identities: - return self.server_identities[profile].get((category, _type), set()) + return self.server_identities[profile].get((category, type_), set()) else: return None - def getServerServiceEntity(self, category, _type, profile): + def getServerServiceEntity(self, category, type_, profile): """Helper method to get first available entity for a service""" - entities = self.getServerServiceEntities(category, _type, profile) + entities = self.getServerServiceEntities(category, type_, profile) if entities is None: warning(_("Entities (%(category)s/%(type)s) not available, maybe they haven't been asked to server yet ?") % {"category": category, - "type": _type}) + "type": type_}) return None else: return list(entities)[0] if entities else None @@ -887,13 +897,13 @@ except KeyError: pass - def addWaitingSub(self, _type, entity_jid, profile_key): + def addWaitingSub(self, type_, entity_jid, profile_key): """Called when a subcription request is received""" profile = self.getProfileName(profile_key) assert profile if profile not in self.subscriptions: self.subscriptions[profile] = {} - self.subscriptions[profile][entity_jid] = _type + self.subscriptions[profile][entity_jid] = type_ def delWaitingSub(self, entity_jid, profile_key): """Called when a subcription request is finished"""