<s> from time import time <EOL> from xierpa3 . toolbox . dating import uniqueId , uniqueLong <EOL> from xierpa3 . constants . constants import Constants <EOL> from session import Session <EOL> class SessionManager ( dict ) : <EOL> C = Constants <EOL> @ classmethod <EOL> def getNewSessionId ( cls , size = None ) : <EOL> u"""<STR_LIT>""" <EOL> return str ( uniqueId ( size or cls . C . SESSION_SIDDIGITS ) ) <EOL> @ classmethod <EOL> def newTimeStamp ( cls ) : <EOL> u"""<STR_LIT>""" <EOL> return uniqueLong ( ) <EOL> def getTimeStamps ( self ) : <EOL> u"""<STR_LIT>""" <EOL> self . garbageCollect ( ) <EOL> timestamps = [ ] <EOL> for session in self . values ( ) : <EOL> timestamps . append ( session . timestamp ) <EOL> return timestamps <EOL> def getSession ( self , sid , limit = None , name = None , user = None , keep = False , protected = False ) : <EOL> u"""<STR_LIT>""" <EOL> session = self . get ( sid ) <EOL> if session is None : <EOL> sid = self . getNewSessionId ( ) <EOL> timestamp = self . newTimeStamp ( ) <EOL> session = self [ sid ] = Session ( sid , timestamp , limit = limit , name = name , user = user , keep = keep , protected = protected ) <EOL> return session <EOL> def newSession ( self ) : <EOL> u"""<STR_LIT>""" <EOL> return self . getSession ( <NUM_LIT:0> ) <EOL> def refresh ( self , sid ) : <EOL> u"""<STR_LIT>""" <EOL> if self . has_key ( sid ) : <EOL> self [ sid ] . refresh ( ) <EOL> def getExpire ( self , sid ) : <EOL> u"""<STR_LIT>""" <EOL> if self . has_key ( sid ) : <EOL> return self [ sid ] . expire - time ( ) <EOL> return <NUM_LIT:0> <EOL> def garbageCollect ( self , force = False ) : <EOL> if force or ( len ( self ) % <NUM_LIT:100> ) == <NUM_LIT:0> : <EOL> for keyv , session in self . items ( ) : <EOL> if session . expired ( ) : <EOL> self . delete ( keyv ) <EOL> def getLoggedUserIds ( self , applicationname ) : <EOL> u"""<STR_LIT>""" <EOL> ids = [ ] <EOL> for session in self . values ( ) : <EOL> if not session . expired ( ) and session [ '<STR_LIT>' ] == applicationname : <EOL> ids . append ( session [ '<STR_LIT>' ] ) <EOL> return ids <EOL> def getSessionById ( self , applicationname , sid ) : <EOL> u"""<STR_LIT>""" <EOL> session = self . get ( sid ) <EOL> if session [ '<STR_LIT>' ] == applicationname : <EOL> return session <EOL> return None <EOL> sessionmanager = SessionManager ( ) </s>
