Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
485:ee95ff721b68 | 486:0d9908ac775e |
---|---|
30 from twisted.words.protocols.jabber import jid | 30 from twisted.words.protocols.jabber import jid |
31 from sat.tools.xml_tools import paramsXml2xmlUI | 31 from sat.tools.xml_tools import paramsXml2xmlUI |
32 from sat.core.default_config import default_config | 32 from sat.core.default_config import default_config |
33 from sat.memory.sqlite import SqliteStorage | 33 from sat.memory.sqlite import SqliteStorage |
34 from sat.memory.persistent import PersistentDict | 34 from sat.memory.persistent import PersistentDict |
35 from sat.core import exceptions | |
35 | 36 |
36 SAVEFILE_PARAM_XML="/param" #xml parameters template | 37 SAVEFILE_PARAM_XML="/param" #xml parameters template |
37 SAVEFILE_DATABASE="/sat.db" | 38 SAVEFILE_DATABASE="/sat.db" |
38 | 39 |
39 class ProfileNotInCacheError(Exception): | |
40 pass | |
41 | |
42 class ConnectedProfileError(Exception): | |
43 pass | |
44 | 40 |
45 class Params(): | 41 class Params(): |
46 """This class manage parameters with xml""" | 42 """This class manage parameters with xml""" |
47 ### TODO: add desciption in params | 43 ### TODO: add desciption in params |
48 | 44 |
152 if not self.storage.hasProfile(profile): | 148 if not self.storage.hasProfile(profile): |
153 error(_('Trying to delete an unknown profile')) | 149 error(_('Trying to delete an unknown profile')) |
154 return True | 150 return True |
155 if self.host.isConnected(profile): | 151 if self.host.isConnected(profile): |
156 error(_("Trying to delete a connected profile")) | 152 error(_("Trying to delete a connected profile")) |
157 raise ConnectedProfileError | 153 raise exceptions.ConnectedProfileError |
158 self.storage.deleteProfile(profile) | 154 self.storage.deleteProfile(profile) |
159 return False | 155 return False |
160 | 156 |
161 def getProfileName(self, profile_key): | 157 def getProfileName(self, profile_key): |
162 """return profile according to profile_key | 158 """return profile according to profile_key |
292 return defer.succeed(node[1].getAttribute(attr)) | 288 return defer.succeed(node[1].getAttribute(attr)) |
293 default = node[1].getAttribute(attr) | 289 default = node[1].getAttribute(attr) |
294 try: | 290 try: |
295 value = self.__getParam(profile, category, name) | 291 value = self.__getParam(profile, category, name) |
296 return defer.succeed(value if value!=None else default) | 292 return defer.succeed(value if value!=None else default) |
297 except ProfileNotInCacheError: | 293 except exceptions.ProfileNotInCacheError: |
298 #We have to ask data to the storage manager | 294 #We have to ask data to the storage manager |
299 d = self.storage.getIndParam(category, name, profile) | 295 d = self.storage.getIndParam(category, name, profile) |
300 return d.addCallback(lambda value: value if value!=None else default) | 296 return d.addCallback(lambda value: value if value!=None else default) |
301 | 297 |
302 def __getParam(self, profile, category, name, _type='individual', cache=None): | 298 def __getParam(self, profile, category, name, _type='individual', cache=None): |
315 assert (_type == 'individual') | 311 assert (_type == 'individual') |
316 if self.params.has_key(profile): | 312 if self.params.has_key(profile): |
317 cache = self.params[profile] # if profile is in main cache, we use it, | 313 cache = self.params[profile] # if profile is in main cache, we use it, |
318 # ignoring the temporary cache | 314 # ignoring the temporary cache |
319 elif cache == None: #else we use the temporary cache if it exists, or raise an exception | 315 elif cache == None: #else we use the temporary cache if it exists, or raise an exception |
320 raise ProfileNotInCacheError | 316 raise exceptions.ProfileNotInCacheError |
321 if not cache.has_key((category, name)): | 317 if not cache.has_key((category, name)): |
322 return None | 318 return None |
323 return cache[(category, name)] | 319 return cache[(category, name)] |
324 | 320 |
325 def __constructProfileXml(self, profile): | 321 def __constructProfileXml(self, profile): |
681 try: | 677 try: |
682 return self.entitiesCache[profile][entity]["last_resource"] | 678 return self.entitiesCache[profile][entity]["last_resource"] |
683 except KeyError: | 679 except KeyError: |
684 return "" | 680 return "" |
685 | 681 |
686 def setPresenceStatus(self, contact_jid, show, priority, statuses, profile_key): | |
687 """Change the presence status of an entity""" | |
688 profile = self.getProfileName(profile_key) | |
689 if not profile: | |
690 error(_('Trying to add presence status to a non-existant profile')) | |
691 return | |
692 entity_data = self.entitiesCache[profile].setdefault(contact_jid.userhost(),{}) | |
693 resource = jid.parse(contact_jid.full())[2] or '' | |
694 if resource: | |
695 entity_data["last_resource"] = resource | |
696 if not "last_resource" in entity_data: | |
697 entity_data["last_resource"] = '' | |
698 | |
699 entity_data.setdefault("presence",{})[resource] = (show, priority, statuses) | |
700 | |
701 def addWaitingSub(self, _type, contact_jid, profile_key): | |
702 """Called when a subcription request is received""" | |
703 profile = self.getProfileName(profile_key) | |
704 assert(profile) | |
705 if not self.subscriptions.has_key(profile): | |
706 self.subscriptions[profile] = {} | |
707 self.subscriptions[profile][contact_jid] = _type | |
708 | |
709 def delWaitingSub(self, contact_jid, profile_key): | |
710 """Called when a subcription request is finished""" | |
711 profile = self.getProfileName(profile_key) | |
712 assert(profile) | |
713 if self.subscriptions.has_key(profile) and self.subscriptions[profile].has_key(contact_jid): | |
714 del self.subscriptions[profile][contact_jid] | |
715 | |
716 def getWaitingSub(self, profile_key): | |
717 """Called to get a list of currently waiting subscription requests""" | |
718 profile = self.getProfileName(profile_key) | |
719 if not profile: | |
720 error(_('Asking waiting subscriptions for a non-existant profile')) | |
721 return {} | |
722 if not self.subscriptions.has_key(profile): | |
723 return {} | |
724 | |
725 return self.subscriptions[profile] | |
726 | |
727 def getPresenceStatus(self, profile_key): | 682 def getPresenceStatus(self, profile_key): |
728 profile = self.getProfileName(profile_key) | 683 profile = self.getProfileName(profile_key) |
729 if not profile: | 684 if not profile: |
730 error(_('Asking contacts for a non-existant profile')) | 685 error(_('Asking contacts for a non-existant profile')) |
731 return {} | 686 return {} |
735 entities_presence[entity] = self.entitiesCache[profile][entity]["presence"] | 690 entities_presence[entity] = self.entitiesCache[profile][entity]["presence"] |
736 | 691 |
737 debug ("Memory getPresenceStatus (%s)", entities_presence) | 692 debug ("Memory getPresenceStatus (%s)", entities_presence) |
738 return entities_presence | 693 return entities_presence |
739 | 694 |
695 def setPresenceStatus(self, entity_jid, show, priority, statuses, profile_key): | |
696 """Change the presence status of an entity""" | |
697 profile = self.getProfileName(profile_key) | |
698 if not profile: | |
699 error(_('Trying to add presence status to a non-existant profile')) | |
700 return | |
701 entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(),{}) | |
702 resource = jid.parse(entity_jid.full())[2] or '' | |
703 if resource: | |
704 entity_data["last_resource"] = resource | |
705 if not "last_resource" in entity_data: | |
706 entity_data["last_resource"] = '' | |
707 | |
708 entity_data.setdefault("presence",{})[resource] = (show, priority, statuses) | |
709 | |
710 def updateEntityData(self, entity_jid, key, value, profile_key): | |
711 """Set a misc data for an entity | |
712 @param entity_jid: JID of the entity | |
713 @param key: key to set (eg: "type") | |
714 @param value: value for this key (eg: "chatroom") | |
715 @param profile_key: %(doc_profile_key)s | |
716 """ | |
717 profile = self.getProfileName(profile_key) | |
718 if not profile: | |
719 raise exceptions.UnknownProfileError(_('Trying to get entity data for a non-existant profile')) | |
720 if not profile in self.entitiesCache: | |
721 raise exceptions.ProfileNotInCacheError | |
722 entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(),{}) | |
723 entity_data[key] = value | |
724 | |
725 def getEntityData(self, entity_jid, keys_list, profile_key): | |
726 """Get a list of cached values for entity | |
727 @param entity_jid: JID of the entity | |
728 @param keys_list: list of keys to get, empty list for everything | |
729 @param profile_key: %(doc_profile_key)s | |
730 @return: dict withs values for each key in keys_list. | |
731 if there is no value of a given key, resultint dict will | |
732 have nothing with that key nether | |
733 @raise: exceptions.UnknownEntityError if entity is not in cache | |
734 exceptions.ProfileNotInCacheError if profile is not in cache | |
735 """ | |
736 profile = self.getProfileName(profile_key) | |
737 if not profile: | |
738 raise exceptions.UnknownProfileError(_('Trying to get entity data for a non-existant profile')) | |
739 if not profile in self.entitiesCache: | |
740 raise exceptions.ProfileNotInCacheError | |
741 if not entity_jid.userhost() in self.entitiesCache[profile]: | |
742 raise exceptions.UnknownEntityError | |
743 entity_data = self.entitiesCache[profile][entity_jid.userhost()] | |
744 if not keys_list: | |
745 return entity_data | |
746 ret = {} | |
747 for key in keys_list: | |
748 if key in entity_data: | |
749 ret[key] = entity_data[key] | |
750 return ret | |
751 | |
752 def addWaitingSub(self, _type, entity_jid, profile_key): | |
753 """Called when a subcription request is received""" | |
754 profile = self.getProfileName(profile_key) | |
755 assert(profile) | |
756 if not self.subscriptions.has_key(profile): | |
757 self.subscriptions[profile] = {} | |
758 self.subscriptions[profile][entity_jid] = _type | |
759 | |
760 def delWaitingSub(self, entity_jid, profile_key): | |
761 """Called when a subcription request is finished""" | |
762 profile = self.getProfileName(profile_key) | |
763 assert(profile) | |
764 if self.subscriptions.has_key(profile) and self.subscriptions[profile].has_key(entity_jid): | |
765 del self.subscriptions[profile][entity_jid] | |
766 | |
767 def getWaitingSub(self, profile_key): | |
768 """Called to get a list of currently waiting subscription requests""" | |
769 profile = self.getProfileName(profile_key) | |
770 if not profile: | |
771 error(_('Asking waiting subscriptions for a non-existant profile')) | |
772 return {} | |
773 if not self.subscriptions.has_key(profile): | |
774 return {} | |
775 | |
776 return self.subscriptions[profile] | |
777 | |
740 def getParamA(self, name, category, attr="value", profile_key='@DEFAULT@'): | 778 def getParamA(self, name, category, attr="value", profile_key='@DEFAULT@'): |
741 return self.params.getParamA(name, category, attr, profile_key) | 779 return self.params.getParamA(name, category, attr, profile_key) |
742 | 780 |
743 def asyncGetParamA(self, name, category, attr="value", profile_key='@DEFAULT@'): | 781 def asyncGetParamA(self, name, category, attr="value", profile_key='@DEFAULT@'): |
744 return self.params.asyncGetParamA(name, category, attr, profile_key) | 782 return self.params.asyncGetParamA(name, category, attr, profile_key) |