# HG changeset patch # User Goffi # Date 1387894748 -3600 # Node ID bbe55c7bee43eb89c08b345ee3def11d8c842092 # Parent efa0e0f579502aec674b51b1e1e415ec8acbfd9c 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 diff -r efa0e0f57950 -r bbe55c7bee43 src/memory/memory.py --- 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