memory: add class ProfileSessions based on Sessions

This commit is contained in:
souliane 2014-05-10 17:37:32 +02:00
commit c9f4397000

View file

@ -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