Mercurial > libervia-backend
diff src/memory/memory.py @ 486:0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 17 Aug 2012 03:07:22 +0200 |
parents | 23cbdf0a0777 |
children | 385cd2169eb5 |
line wrap: on
line diff
--- a/src/memory/memory.py Thu Aug 16 15:17:16 2012 +0200 +++ b/src/memory/memory.py Fri Aug 17 03:07:22 2012 +0200 @@ -32,15 +32,11 @@ from sat.core.default_config import default_config from sat.memory.sqlite import SqliteStorage from sat.memory.persistent import PersistentDict +from sat.core import exceptions SAVEFILE_PARAM_XML="/param" #xml parameters template SAVEFILE_DATABASE="/sat.db" -class ProfileNotInCacheError(Exception): - pass - -class ConnectedProfileError(Exception): - pass class Params(): """This class manage parameters with xml""" @@ -154,7 +150,7 @@ return True if self.host.isConnected(profile): error(_("Trying to delete a connected profile")) - raise ConnectedProfileError + raise exceptions.ConnectedProfileError self.storage.deleteProfile(profile) return False @@ -294,7 +290,7 @@ try: value = self.__getParam(profile, category, name) return defer.succeed(value if value!=None else default) - except ProfileNotInCacheError: + except exceptions.ProfileNotInCacheError: #We have to ask data to the storage manager d = self.storage.getIndParam(category, name, profile) return d.addCallback(lambda value: value if value!=None else default) @@ -317,7 +313,7 @@ cache = self.params[profile] # if profile is in main cache, we use it, # ignoring the temporary cache elif cache == None: #else we use the temporary cache if it exists, or raise an exception - raise ProfileNotInCacheError + raise exceptions.ProfileNotInCacheError if not cache.has_key((category, name)): return None return cache[(category, name)] @@ -683,47 +679,6 @@ except KeyError: return "" - def setPresenceStatus(self, contact_jid, show, priority, statuses, profile_key): - """Change the presence status of an entity""" - profile = self.getProfileName(profile_key) - if not profile: - error(_('Trying to add presence status to a non-existant profile')) - return - entity_data = self.entitiesCache[profile].setdefault(contact_jid.userhost(),{}) - resource = jid.parse(contact_jid.full())[2] or '' - if resource: - entity_data["last_resource"] = resource - if not "last_resource" in entity_data: - entity_data["last_resource"] = '' - - entity_data.setdefault("presence",{})[resource] = (show, priority, statuses) - - def addWaitingSub(self, _type, contact_jid, profile_key): - """Called when a subcription request is received""" - profile = self.getProfileName(profile_key) - assert(profile) - if not self.subscriptions.has_key(profile): - self.subscriptions[profile] = {} - self.subscriptions[profile][contact_jid] = _type - - def delWaitingSub(self, contact_jid, profile_key): - """Called when a subcription request is finished""" - profile = self.getProfileName(profile_key) - assert(profile) - if self.subscriptions.has_key(profile) and self.subscriptions[profile].has_key(contact_jid): - del self.subscriptions[profile][contact_jid] - - def getWaitingSub(self, profile_key): - """Called to get a list of currently waiting subscription requests""" - profile = self.getProfileName(profile_key) - if not profile: - error(_('Asking waiting subscriptions for a non-existant profile')) - return {} - if not self.subscriptions.has_key(profile): - return {} - - return self.subscriptions[profile] - def getPresenceStatus(self, profile_key): profile = self.getProfileName(profile_key) if not profile: @@ -737,6 +692,89 @@ debug ("Memory getPresenceStatus (%s)", entities_presence) return entities_presence + def setPresenceStatus(self, entity_jid, show, priority, statuses, profile_key): + """Change the presence status of an entity""" + profile = self.getProfileName(profile_key) + if not profile: + error(_('Trying to add presence status to a non-existant profile')) + return + entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(),{}) + resource = jid.parse(entity_jid.full())[2] or '' + if resource: + entity_data["last_resource"] = resource + if not "last_resource" in entity_data: + entity_data["last_resource"] = '' + + entity_data.setdefault("presence",{})[resource] = (show, priority, statuses) + + def updateEntityData(self, entity_jid, key, value, profile_key): + """Set a misc data for an entity + @param entity_jid: JID of the entity + @param key: key to set (eg: "type") + @param value: value for this key (eg: "chatroom") + @param profile_key: %(doc_profile_key)s + """ + profile = self.getProfileName(profile_key) + if not profile: + raise exceptions.UnknownProfileError(_('Trying to get entity data for a non-existant profile')) + if not profile in self.entitiesCache: + raise exceptions.ProfileNotInCacheError + entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(),{}) + entity_data[key] = value + + def getEntityData(self, entity_jid, keys_list, profile_key): + """Get a list of cached values for entity + @param entity_jid: JID of the entity + @param keys_list: list of keys to get, empty list for everything + @param profile_key: %(doc_profile_key)s + @return: dict withs values for each key in keys_list. + if there is no value of a given key, resultint dict will + have nothing with that key nether + @raise: exceptions.UnknownEntityError if entity is not in cache + exceptions.ProfileNotInCacheError if profile is not in cache + """ + profile = self.getProfileName(profile_key) + if not profile: + raise exceptions.UnknownProfileError(_('Trying to get entity data for a non-existant profile')) + if not profile in self.entitiesCache: + raise exceptions.ProfileNotInCacheError + if not entity_jid.userhost() in self.entitiesCache[profile]: + raise exceptions.UnknownEntityError + entity_data = self.entitiesCache[profile][entity_jid.userhost()] + if not keys_list: + return entity_data + ret = {} + for key in keys_list: + if key in entity_data: + ret[key] = entity_data[key] + return ret + + def addWaitingSub(self, _type, entity_jid, profile_key): + """Called when a subcription request is received""" + profile = self.getProfileName(profile_key) + assert(profile) + if not self.subscriptions.has_key(profile): + self.subscriptions[profile] = {} + self.subscriptions[profile][entity_jid] = _type + + def delWaitingSub(self, entity_jid, profile_key): + """Called when a subcription request is finished""" + profile = self.getProfileName(profile_key) + assert(profile) + if self.subscriptions.has_key(profile) and self.subscriptions[profile].has_key(entity_jid): + del self.subscriptions[profile][entity_jid] + + def getWaitingSub(self, profile_key): + """Called to get a list of currently waiting subscription requests""" + profile = self.getProfileName(profile_key) + if not profile: + error(_('Asking waiting subscriptions for a non-existant profile')) + return {} + if not self.subscriptions.has_key(profile): + return {} + + return self.subscriptions[profile] + def getParamA(self, name, category, attr="value", profile_key='@DEFAULT@'): return self.params.getParamA(name, category, attr, profile_key)