comparison src/memory/memory.py @ 1029:f6182f6418ea

memory: add class ProfileSessions based on Sessions
author souliane <souliane@mailoo.org>
date Sat, 10 May 2014 17:37:32 +0200
parents fee00f2e11c2
children 15f43b54d697
comparison
equal deleted inserted replaced
1028:127c96020022 1029:f6182f6418ea
35 from sat.memory.params import Params 35 from sat.memory.params import Params
36 from sat.memory.disco import Discovery 36 from sat.memory.disco import Discovery
37 37
38 38
39 class Sessions(object): 39 class Sessions(object):
40 """Sessions are data associated to key used for a temporary moment, with optional profile checking."""
40 DEFAULT_TIMEOUT = 600 41 DEFAULT_TIMEOUT = 600
41 42
42 def __init__(self, timeout = None): 43 def __init__(self, timeout=None):
43 """ 44 """
44 @param timeout: nb of seconds before session destruction 45 @param timeout: nb of seconds before session destruction
45 """ 46 """
46 self._sessions = dict() 47 self._sessions = dict()
47 self.timeout = timeout or Sessions.DEFAULT_TIMEOUT 48 self.timeout = timeout or Sessions.DEFAULT_TIMEOUT
96 try: 97 try:
97 timer = self._sessions[session_id][0] 98 timer = self._sessions[session_id][0]
98 timer.cancel() 99 timer.cancel()
99 self._purgeSession(session_id) 100 self._purgeSession(session_id)
100 except KeyError: 101 except KeyError:
101 log.debug ("Session [%s] doesn't exists, timeout expired ?" % session_id) 102 log.debug("Session [%s] doesn't exists, timeout expired?" % session_id)
102 103
103 def keys(self): 104 def keys(self):
104 return self._sessions.keys() 105 return self._sessions.keys()
105 106
106 def iterkeys(self): 107 def iterkeys(self):
107 return self._sessions.iterkeys() 108 return self._sessions.iterkeys()
109
110
111 class ProfileSessions(Sessions):
112 """ProfileSessions extends the Sessions class, but here the profile can be
113 used as the key to retrieve data or delete a session (instead of session id).
114 """
115
116 def _profileGetAllIds(self, profile):
117 """Return a list of the sessions ids that are associated to the given profile.
118
119 @param profile: %(doc_profile)s
120 @return: a list containing the sessions ids
121 """
122 ret = []
123 for session_id in self._sessions:
124 try:
125 timer, session_data, profile_set = self._sessions[session_id]
126 except ValueError:
127 continue
128 if profile == profile_set:
129 ret.append(session_id)
130 return ret
131
132 def profileGetUnique(self, profile):
133 """Return the data of the unique session that is associated to the given profile.
134
135 @param profile: %(doc_profile)s
136 @return:
137 - mutable data (default: dict) of the unique session
138 - None if no session is associated to the profile
139 - raise an error if more than one session are found
140 """
141 ids = self._profileGetAllIds(profile)
142 if len(ids) > 1:
143 raise exceptions.InternalError('profileGetUnique has been used but more than one session has been found!')
144 return self._sessions[ids[0]][1] if len(ids) == 1 else None
145
146 def profileDelUnique(self, profile):
147 """Delete the unique session that is associated to the given profile.
148
149 @param profile: %(doc_profile)s
150 @return: None, but raise an error if more than one session are found
151 """
152 ids = self._profileGetAllIds(profile)
153 if len(ids) > 1:
154 raise exceptions.InternalError('profileDelUnique has been used but more than one session has been found!')
155 if len(ids) == 1:
156 del self._sessions[ids[0]]
108 157
109 158
110 # XXX: tmp update code, will be removed in the future 159 # XXX: tmp update code, will be removed in the future
111 # When you remove this, please add the default value for 160 # When you remove this, please add the default value for
112 # 'local_dir' in sat.core.constants.Const.DEFAULT_CONFIG 161 # 'local_dir' in sat.core.constants.Const.DEFAULT_CONFIG