changeset 757:bbe55c7bee43

core (memory): added optional profile checking in Sessions: if profile is set in newSession, getProfile must be used instead of __getitem__, and it is checked to insure session is not used by the wrong profile
author Goffi <goffi@goffi.org>
date Tue, 24 Dec 2013 15:19:08 +0100
parents efa0e0f57950
children 86224a13cc1d
files src/memory/memory.py
diffstat 1 files changed, 18 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/memory/memory.py	Tue Dec 24 15:19:08 2013 +0100
+++ b/src/memory/memory.py	Tue Dec 24 15:19:08 2013 +0100
@@ -50,16 +50,18 @@
         self._sessions = dict()
         self.timeout = timeout or Sessions.DEFAULT_TIMEOUT
 
-    def newSession(self, session_data=None):
+    def newSession(self, session_data=None, profile=None):
         """ Create a new session
         @param session_data: mutable data to use, default to a dict
+        @param profile: if set, the session is owned by the profile,
+                        and profileGet must be used instead of __getitem__
         @return: session_id, session_data
         """
         session_id = str(uuid4())
         timer = reactor.callLater(self.timeout, self._purgeSession, session_id)
         if session_data is None:
             session_data = {}
-        self._sessions[session_id] = (timer, session_data)
+        self._sessions[session_id] = (timer, session_data) if profile is None else (timer, session_data, profile)
         return session_id, session_data
 
     def _purgeSession(self, session_id):
@@ -72,8 +74,21 @@
     def __contains__(self, session_id):
         return session_id in self._sessions
 
+    def profileGet(self, session_id, profile):
+        try:
+            timer, session_data, profile_set = self._sessions[session_id]
+        except ValueError:
+            raise exceptions.InternalError("You need to use __getitem__ when profile is not set")
+        if profile_set != profile:
+            raise exceptions.InternalError("current profile differ from set profile !")
+        timer.reset(self.timeout)
+        return session_data
+
     def __getitem__(self, session_id):
-        timer, session_data = self._sessions[session_id]
+        try:
+            timer, session_data = self._sessions[session_id]
+        except ValueError:
+            raise exceptions.InternalError("You need to use profileGet instead of __getitem__ when profile is set")
         timer.reset(self.timeout)
         return session_data