# HG changeset patch # User souliane # Date 1399736252 -7200 # Node ID f6182f6418ead0c6f155954223cc3a09438f05b3 # Parent 127c96020022588eb9eca780975ef0369a691616 memory: add class ProfileSessions based on Sessions diff -r 127c96020022 -r f6182f6418ea src/memory/memory.py --- 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 diff -r 127c96020022 -r f6182f6418ea src/memory/persistent.py --- 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__()