changeset 1029:f6182f6418ea

memory: add class ProfileSessions based on Sessions
author souliane <souliane@mailoo.org>
date Sat, 10 May 2014 17:37:32 +0200
parents 127c96020022
children 15f43b54d697
files src/memory/memory.py src/memory/persistent.py
diffstat 2 files changed, 57 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/memory/memory.py	Wed May 07 15:46:43 2014 +0200
+++ b/src/memory/memory.py	Sat May 10 17:37:32 2014 +0200
@@ -37,9 +37,10 @@
 
 
 class Sessions(object):
+    """Sessions are data associated to key used for a temporary moment, with optional profile checking."""
     DEFAULT_TIMEOUT = 600
 
-    def __init__(self, timeout = None):
+    def __init__(self, timeout=None):
         """
         @param timeout: nb of seconds before session destruction
         """
@@ -98,7 +99,7 @@
             timer.cancel()
             self._purgeSession(session_id)
         except KeyError:
-            log.debug ("Session [%s] doesn't exists, timeout expired ?" % session_id)
+            log.debug("Session [%s] doesn't exists, timeout expired?" % session_id)
 
     def keys(self):
         return self._sessions.keys()
@@ -107,6 +108,54 @@
         return self._sessions.iterkeys()
 
 
+class ProfileSessions(Sessions):
+    """ProfileSessions extends the Sessions class, but here the profile can be
+    used as the key to retrieve data or delete a session (instead of session id).
+    """
+
+    def _profileGetAllIds(self, profile):
+        """Return a list of the sessions ids that are associated to the given profile.
+
+        @param profile: %(doc_profile)s
+        @return: a list containing the sessions ids
+        """
+        ret = []
+        for session_id in self._sessions:
+            try:
+                timer, session_data, profile_set = self._sessions[session_id]
+            except ValueError:
+                continue
+            if profile == profile_set:
+                ret.append(session_id)
+        return ret
+
+    def profileGetUnique(self, profile):
+        """Return the data of the unique session that is associated to the given profile.
+
+        @param profile: %(doc_profile)s
+        @return:
+            - mutable data (default: dict) of the unique session
+            - None if no session is associated to the profile
+            - raise an error if more than one session are found
+        """
+        ids = self._profileGetAllIds(profile)
+        if len(ids) > 1:
+            raise exceptions.InternalError('profileGetUnique has been used but more than one session has been found!')
+        return self._sessions[ids[0]][1] if len(ids) == 1 else None
+
+    def profileDelUnique(self, profile):
+        """Delete the unique session that is associated to the given profile.
+
+        @param profile: %(doc_profile)s
+        @return: None, but raise an error if more than one session are found
+        """
+        ids = self._profileGetAllIds(profile)
+        if len(ids) > 1:
+            raise exceptions.InternalError('profileDelUnique has been used but more than one session has been found!')
+        if len(ids) == 1:
+            del self._sessions[ids[0]]
+
+
 # XXX: tmp update code, will be removed in the future
 # When you remove this, please add the default value for
 # 'local_dir' in sat.core.constants.Const.DEFAULT_CONFIG
--- a/src/memory/persistent.py	Wed May 07 15:46:43 2014 +0200
+++ b/src/memory/persistent.py	Sat May 10 17:37:32 2014 +0200
@@ -43,12 +43,15 @@
         self.profile = profile
 
     def load(self):
-        """load persistent data from storage
+        """Load persistent data from storage.
+
+        @return: defers the PersistentDict instance itself
         """
         if not self.profile:
-            return self.storage.loadGenPrivates(self._cache, self.namespace)
+            d = self.storage.loadGenPrivates(self._cache, self.namespace)
         else:
-            return self.storage.loadIndPrivates(self._cache, self.namespace, self.profile)
+            d = self.storage.loadIndPrivates(self._cache, self.namespace, self.profile)
+        return d.addCallback(lambda dummy: self)
 
     def __repr__(self):
         return self._cache.__repr__()