<s> import os <EOL> import random <EOL> import time <EOL> try : <EOL> from hashlib import md5 <EOL> except ImportError : <EOL> from md5 import md5 <EOL> from backends . base import KeyError <EOL> class SessionException ( Exception ) : pass <EOL> class NValue ( object ) : pass <EOL> getpid = hasattr ( os , '<STR_LIT>' ) and os . getpid or ( lambda : '<STR_LIT>' ) <EOL> def _get_id ( ) : <EOL> return md5 ( <EOL> md5 ( "<STR_LIT>" % ( time . time ( ) , id ( { } ) , random . random ( ) , <EOL> getpid ( ) ) ) . hexdigest ( ) , <EOL> ) . hexdigest ( ) <EOL> class SessionCookie ( object ) : <EOL> default_expiry_time = None <EOL> default_domain = '<STR_LIT>' <EOL> default_secure = False <EOL> default_path = '<STR_LIT:/>' <EOL> default_cookie_id = '<STR_LIT>' <EOL> def __init__ ( self , session ) : <EOL> self . session = session <EOL> self . domain = self . default_domain <EOL> self . path = self . default_path <EOL> self . secure = self . default_secure <EOL> self . expiry_time = self . default_expiry_time <EOL> self . cookie_id = self . default_cookie_id <EOL> def save ( self ) : <EOL> self . expiry_time = self . expiry_time or self . session . expiry_time <EOL> from cache import Serial <EOL> class Session ( dict ) : <EOL> force = False <EOL> def __init__ ( self , key = None , storage_type = '<STR_LIT:file>' , options = None , expiry_time = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> , <EOL> serial_cls = None ) : <EOL> """<STR_LIT>""" <EOL> dict . __init__ ( self ) <EOL> self . _old_value = { } <EOL> self . _storage_type = storage_type <EOL> self . _options = options or { } <EOL> self . _storage_cls = self . __get_storage ( ) <EOL> self . _storage = None <EOL> self . _accessed_time = None <EOL> self . expiry_time = expiry_time <EOL> self . key = key <EOL> self . deleted = False <EOL> self . cookie = SessionCookie ( self ) <EOL> self . _serial_cls = serial_cls or Serial <EOL> self . serial_obj = self . _serial_cls ( ) <EOL> self . load ( self . key ) <EOL> def __get_storage ( self ) : <EOL> modname = '<STR_LIT>' % self . _storage_type <EOL> mod = __import__ ( modname , fromlist = [ '<STR_LIT:*>' ] ) <EOL> _class = getattr ( mod , '<STR_LIT>' , None ) <EOL> return _class <EOL> def _set_remember ( self , v ) : <EOL> self [ '<STR_LIT>' ] = v <EOL> def _get_remember ( self ) : <EOL> return self . get ( '<STR_LIT>' , False ) <EOL> remember = property ( _get_remember , _set_remember ) <EOL> @ property <EOL> def storage ( self ) : <EOL> if not self . _storage : <EOL> self . _storage = self . _storage_cls ( self , self . _options ) <EOL> return self . _storage <EOL> def load ( self , key = None ) : <EOL> self . deleted = False <EOL> self . clear ( ) <EOL> self . key = key <EOL> if not self . key : <EOL> return <EOL> try : <EOL> value = self . storage . get ( key ) <EOL> except KeyError , e : <EOL> value = { } <EOL> self . update ( value ) <EOL> self . _old_value = self . copy ( ) <EOL> def _is_modified ( self ) : <EOL> return self . _old_value != dict ( self ) <EOL> def save ( self , force = False ) : <EOL> flag = force <EOL> if not flag : <EOL> if not self . deleted and self . force and ( bool ( self ) or ( not bool ( self ) and self . _is_modified ( ) ) ) : <EOL> flag = True <EOL> elif not self . deleted and not self . force and self . _is_modified ( ) : <EOL> flag = True <EOL> if flag : <EOL> self . key = self . key or _get_id ( ) <EOL> d = { } <EOL> for k , value in dict ( self ) . items ( ) : <EOL> v , accessed_time , expiry_time = self . _parse_value ( value ) <EOL> if expiry_time and accessed_time : <EOL> if self . _is_not_expiry ( accessed_time , expiry_time ) : <EOL> d [ k ] = value <EOL> else : <EOL> d [ k ] = v <EOL> self . storage . set ( self . key , d , self . expiry_time ) <EOL> self . cookie . save ( ) <EOL> return True <EOL> else : <EOL> return False <EOL> def delete ( self ) : <EOL> if self . key : <EOL> self . storage . delete ( self . key ) <EOL> self . clear ( ) <EOL> self . _old_value = self . copy ( ) <EOL> self . deleted = True <EOL> def set_expiry ( self , value ) : <EOL> self . expiry_time = value <EOL> self . cookie . expiry_time = value <EOL> def _is_not_expiry ( self , accessed_time , expiry_time ) : <EOL> return time . time ( ) < accessed_time + expiry_time <EOL> def _check ( f ) : <EOL> def _func ( self , * args , ** kw ) : <EOL> try : <EOL> if self . deleted : <EOL> raise SessionException , "<STR_LIT>" <EOL> return f ( self , * args , ** kw ) <EOL> finally : <EOL> self . _accessed_time = time . time ( ) <EOL> return _func <EOL> def _parse_value ( self , v ) : <EOL> if isinstance ( v , dict ) and '<STR_LIT>' in v and '<STR_LIT>' in v and '<STR_LIT>' in v : <EOL> v , accessed_time , expiry_time = v [ '<STR_LIT>' ] , v [ '<STR_LIT>' ] , v [ '<STR_LIT>' ] <EOL> else : <EOL> accessed_time , expiry_time = None , None <EOL> return v , accessed_time , expiry_time <EOL> @ _check <EOL> def __getitem__ ( self , name ) : <EOL> v = dict . get ( self , name , NValue ) <EOL> if v is NValue : <EOL> raise KeyError ( "<STR_LIT>" % name ) <EOL> v , accessed_time , expiry_time = self . _parse_value ( v ) <EOL> if expiry_time and accessed_time : <EOL> if self . _is_not_expiry ( accessed_time , expiry_time ) : <EOL> return v <EOL> else : <EOL> del self [ name ] <EOL> raise KeyError ( "<STR_LIT>" % name ) <EOL> else : <EOL> return v <EOL> @ _check <EOL> def get ( self , name , default = None ) : <EOL> try : <EOL> v = self . __getitem__ ( name ) <EOL> return v <EOL> except KeyError : <EOL> return default <EOL> def set ( self , name , value , expiry_time = None ) : <EOL> if expiry_time : <EOL> v = { '<STR_LIT>' : value , '<STR_LIT>' : expiry_time , '<STR_LIT>' : time . time ( ) } <EOL> else : <EOL> v = value <EOL> self . __setitem__ ( name , v ) <EOL> clear = _check ( dict . clear ) <EOL> __setitem__ = _check ( dict . __setitem__ ) <EOL> __delitem__ = _check ( dict . __delitem__ ) <EOL> pop = _check ( dict . pop ) <EOL> popitem = _check ( dict . popitem ) <EOL> setdefault = _check ( dict . setdefault ) <EOL> update = _check ( dict . update ) </s>
