memory: add class ProfileSessions based on Sessions
This commit is contained in:
parent
7a29fae242
commit
c9f4397000
2 changed files with 57 additions and 5 deletions
|
|
@ -37,9 +37,10 @@ from sat.memory.disco import Discovery
|
|||
|
||||
|
||||
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 @@ class Sessions(object):
|
|||
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 @@ class Sessions(object):
|
|||
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
|
||||
|
|
|
|||
|
|
@ -43,12 +43,15 @@ class PersistentDict(object):
|
|||
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__()
|
||||
|
|
|
|||
Loading…
Reference in a new issue