comparison src/memory/memory.py @ 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 93bd868b8fb6
comparison
equal deleted inserted replaced
756:efa0e0f57950 757:bbe55c7bee43
48 @param timeout: nb of seconds before session destruction 48 @param timeout: nb of seconds before session destruction
49 """ 49 """
50 self._sessions = dict() 50 self._sessions = dict()
51 self.timeout = timeout or Sessions.DEFAULT_TIMEOUT 51 self.timeout = timeout or Sessions.DEFAULT_TIMEOUT
52 52
53 def newSession(self, session_data=None): 53 def newSession(self, session_data=None, profile=None):
54 """ Create a new session 54 """ Create a new session
55 @param session_data: mutable data to use, default to a dict 55 @param session_data: mutable data to use, default to a dict
56 @param profile: if set, the session is owned by the profile,
57 and profileGet must be used instead of __getitem__
56 @return: session_id, session_data 58 @return: session_id, session_data
57 """ 59 """
58 session_id = str(uuid4()) 60 session_id = str(uuid4())
59 timer = reactor.callLater(self.timeout, self._purgeSession, session_id) 61 timer = reactor.callLater(self.timeout, self._purgeSession, session_id)
60 if session_data is None: 62 if session_data is None:
61 session_data = {} 63 session_data = {}
62 self._sessions[session_id] = (timer, session_data) 64 self._sessions[session_id] = (timer, session_data) if profile is None else (timer, session_data, profile)
63 return session_id, session_data 65 return session_id, session_data
64 66
65 def _purgeSession(self, session_id): 67 def _purgeSession(self, session_id):
66 del self._sessions[session_id] 68 del self._sessions[session_id]
67 debug("Session [%s] purged" % session_id) 69 debug("Session [%s] purged" % session_id)
70 return len(self._sessions) 72 return len(self._sessions)
71 73
72 def __contains__(self, session_id): 74 def __contains__(self, session_id):
73 return session_id in self._sessions 75 return session_id in self._sessions
74 76
77 def profileGet(self, session_id, profile):
78 try:
79 timer, session_data, profile_set = self._sessions[session_id]
80 except ValueError:
81 raise exceptions.InternalError("You need to use __getitem__ when profile is not set")
82 if profile_set != profile:
83 raise exceptions.InternalError("current profile differ from set profile !")
84 timer.reset(self.timeout)
85 return session_data
86
75 def __getitem__(self, session_id): 87 def __getitem__(self, session_id):
76 timer, session_data = self._sessions[session_id] 88 try:
89 timer, session_data = self._sessions[session_id]
90 except ValueError:
91 raise exceptions.InternalError("You need to use profileGet instead of __getitem__ when profile is set")
77 timer.reset(self.timeout) 92 timer.reset(self.timeout)
78 return session_data 93 return session_data
79 94
80 def __setitem__(self, key, value): 95 def __setitem__(self, key, value):
81 raise NotImplementedError("You need do use newSession to create a session") 96 raise NotImplementedError("You need do use newSession to create a session")