<s> from __future__ import print_function , unicode_literals <EOL> import time <EOL> import threading <EOL> import weakref <EOL> import weblab . configuration_doc as configuration_doc <EOL> import voodoo . sessions . session_type as SessionType <EOL> import voodoo . sessions . gateway as SessionGateway <EOL> import voodoo . sessions . session_id as SessionId <EOL> import voodoo . sessions . exc as SessionErrors <EOL> DEBUGGING = True <EOL> class SessionManagerCleaner ( threading . Thread ) : <EOL> _session_managers = [ ] <EOL> def __init__ ( self ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . setName ( "<STR_LIT>" ) <EOL> self . stopping = False <EOL> @ staticmethod <EOL> def append_session_manager ( session_manager ) : <EOL> SessionManagerCleaner . _session_managers . append ( weakref . ref ( session_manager ) ) <EOL> def stop ( self ) : <EOL> self . stopping = True <EOL> def run ( self ) : <EOL> while not self . stopping : <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> try : <EOL> session_managers = SessionManagerCleaner . _session_managers [ : ] <EOL> for session_manager_ref in session_managers : <EOL> session_manager = session_manager_ref ( ) <EOL> if session_manager is None : <EOL> try : <EOL> SessionManagerCleaner . _session_managers . remove ( session_manager_ref ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> session_manager . delete_expired_sessions ( ) <EOL> except Exception as e : <EOL> if DEBUGGING : <EOL> print ( "<STR_LIT>" , e ) <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> _cleaner = SessionManagerCleaner ( ) <EOL> _cleaner . setDaemon ( True ) <EOL> _cleaner . start ( ) <EOL> class SessionManager ( object ) : <EOL> def __init__ ( self , cfg_manager , session_type , session_pool_id , timeout = "<STR_LIT:default>" ) : <EOL> """<STR_LIT>""" <EOL> object . __init__ ( self ) <EOL> if not session_type in SessionType . getSessionTypeValues ( ) : <EOL> raise SessionErrors . SessionInvalidSessionTypeError ( <EOL> "<STR_LIT>" % session_type <EOL> ) <EOL> self . _cfg_manager = cfg_manager <EOL> if timeout == "<STR_LIT:default>" : <EOL> timeout = self . _cfg_manager . get_doc_value ( configuration_doc . SESSION_MANAGER_DEFAULT_TIMEOUT ) <EOL> gateway_class = SessionGateway . get_gateway_class ( session_type ) <EOL> self . gateway = gateway_class ( cfg_manager , session_pool_id , timeout ) <EOL> self . _session_type = session_type <EOL> SessionManagerCleaner . append_session_manager ( self ) <EOL> @ property <EOL> def session_type ( self ) : <EOL> return self . _session_type <EOL> def create_session ( self , desired_sess_id = None ) : <EOL> """<STR_LIT>""" <EOL> str_sess_id = self . gateway . create_session ( desired_sess_id ) <EOL> return SessionId . SessionId ( str_sess_id ) <EOL> def has_session ( self , sess_id ) : <EOL> return self . gateway . has_session ( sess_id . id ) <EOL> def get_session ( self , sess_id ) : <EOL> if isinstance ( sess_id , SessionId . SessionId ) : <EOL> return self . gateway . get_session ( sess_id . id ) <EOL> else : <EOL> raise SessionErrors . SessionInvalidSessionIdError ( <EOL> "<STR_LIT>" % sess_id <EOL> ) <EOL> def get_session_locking ( self , sess_id ) : <EOL> if isinstance ( sess_id , SessionId . SessionId ) : <EOL> return self . gateway . get_session_locking ( sess_id . id ) <EOL> else : <EOL> raise SessionErrors . SessionInvalidSessionIdError ( <EOL> "<STR_LIT>" % sess_id <EOL> ) <EOL> def modify_session ( self , sess_id , sess_obj ) : <EOL> if isinstance ( sess_id , SessionId . SessionId ) : <EOL> return self . gateway . modify_session ( sess_id . id , sess_obj ) <EOL> else : <EOL> raise SessionErrors . SessionInvalidSessionIdError ( <EOL> "<STR_LIT>" % sess_id <EOL> ) <EOL> def modify_session_unlocking ( self , sess_id , sess_obj ) : <EOL> if isinstance ( sess_id , SessionId . SessionId ) : <EOL> return self . gateway . modify_session_unlocking ( sess_id . id , sess_obj ) <EOL> else : <EOL> raise SessionErrors . SessionInvalidSessionIdError ( <EOL> "<STR_LIT>" % sess_id <EOL> ) <EOL> def unlock_without_modifying ( self , sess_id ) : <EOL> if isinstance ( sess_id , SessionId . SessionId ) : <EOL> return self . gateway . unlock_without_modifying ( sess_id . id ) <EOL> else : <EOL> raise SessionErrors . SessionInvalidSessionIdError ( <EOL> "<STR_LIT>" % sess_id <EOL> ) <EOL> def list_sessions ( self ) : <EOL> return [ SessionId . SessionId ( sid ) for sid in self . gateway . list_sessions ( ) ] <EOL> def clear ( self ) : <EOL> self . gateway . clear ( ) <EOL> def delete_session ( self , sess_id ) : <EOL> if isinstance ( sess_id , SessionId . SessionId ) : <EOL> return self . gateway . delete_session ( sess_id . id ) <EOL> else : <EOL> raise SessionErrors . SessionInvalidSessionIdError ( <EOL> "<STR_LIT>" % sess_id <EOL> ) <EOL> def delete_session_unlocking ( self , sess_id ) : <EOL> if isinstance ( sess_id , SessionId . SessionId ) : <EOL> return self . gateway . delete_session_unlocking ( sess_id . id ) <EOL> else : <EOL> raise SessionErrors . SessionInvalidSessionIdError ( <EOL> "<STR_LIT>" % sess_id <EOL> ) <EOL> def delete_expired_sessions ( self ) : <EOL> self . gateway . delete_expired_sessions ( ) </s>
