<s> '''<STR_LIT>''' <EOL> from __future__ import with_statement <EOL> from Cookie import CookieError , SimpleCookie <EOL> from atexit import register as atexit_register <EOL> from datetime import datetime , timedelta <EOL> from hashlib import sha224 <EOL> from os import close as os_close , makedirs , remove <EOL> from os . path import exists , dirname , join as path_join , isfile <EOL> from shutil import copy <EOL> from shutil import move <EOL> from tempfile import mkstemp <EOL> try : <EOL> from cPickle import dump as pickle_dump , load as pickle_load <EOL> except ImportError : <EOL> from pickle import dump as pickle_dump , load as pickle_load <EOL> from config import WORK_DIR <EOL> CURRENT_SESSION = None <EOL> SESSION_COOKIE_KEY = '<STR_LIT>' <EOL> SESSIONS_DIR = path_join ( WORK_DIR , '<STR_LIT>' ) <EOL> EXPIRATION_DELTA = timedelta ( days = <NUM_LIT:30> ) <EOL> class NoSessionError ( Exception ) : <EOL> pass <EOL> class SessionStoreError ( Exception ) : <EOL> pass <EOL> class SessionCookie ( SimpleCookie ) : <EOL> def __init__ ( self , sid = None ) : <EOL> if sid is not None : <EOL> self [ SESSION_COOKIE_KEY ] = sid <EOL> def set_expired ( self ) : <EOL> self [ SESSION_COOKIE_KEY ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> def set_sid ( self , sid ) : <EOL> self [ SESSION_COOKIE_KEY ] = sid <EOL> def get_sid ( self ) : <EOL> return self [ SESSION_COOKIE_KEY ] . value <EOL> def hdrs ( self ) : <EOL> hdrs = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> for cookie_line in self . output ( header = '<STR_LIT>' , <EOL> sep = '<STR_LIT:\n>' ) . split ( '<STR_LIT:\n>' ) : <EOL> hdrs . append ( tuple ( cookie_line . split ( '<STR_LIT>' , <NUM_LIT:1> ) ) ) <EOL> return tuple ( hdrs ) <EOL> @ classmethod <EOL> def load ( cls , cookie_data ) : <EOL> cookie = SessionCookie ( ) <EOL> SimpleCookie . load ( cookie , cookie_data ) <EOL> return cookie <EOL> class Session ( dict ) : <EOL> def __init__ ( self , cookie ) : <EOL> self . cookie = cookie <EOL> sid = self . cookie . get_sid ( ) <EOL> self . init_cookie ( sid ) <EOL> def init_cookie ( self , sid ) : <EOL> self . cookie . clear ( ) <EOL> self . cookie [ SESSION_COOKIE_KEY ] = sid <EOL> self . cookie [ SESSION_COOKIE_KEY ] [ '<STR_LIT:path>' ] = '<STR_LIT>' <EOL> self . cookie [ SESSION_COOKIE_KEY ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . cookie [ SESSION_COOKIE_KEY ] [ '<STR_LIT>' ] = ( <EOL> datetime . utcnow ( ) + EXPIRATION_DELTA <EOL> ) . strftime ( '<STR_LIT>' ) <EOL> try : <EOL> self . cookie [ SESSION_COOKIE_KEY ] [ '<STR_LIT>' ] = True <EOL> except CookieError : <EOL> pass <EOL> def get ( self , key , default = None ) : <EOL> try : <EOL> return self [ key ] <EOL> except KeyError : <EOL> return default <EOL> def get_sid ( self ) : <EOL> return self . cookie . get_sid ( ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . get_sid ( ) , self . cookie , dict . __str__ ( self ) , ) <EOL> def get_session_pickle_path ( sid ) : <EOL> return path_join ( SESSIONS_DIR , '<STR_LIT>' % ( sid , ) ) <EOL> def init_session ( remote_address , cookie_data = None ) : <EOL> if cookie_data is not None : <EOL> cookie = SessionCookie . load ( cookie_data ) <EOL> else : <EOL> cookie = None <EOL> sid = sha224 ( '<STR_LIT>' % ( remote_address , datetime . utcnow ( ) ) ) . hexdigest ( ) <EOL> if cookie is None : <EOL> cookie = SessionCookie ( sid ) <EOL> else : <EOL> try : <EOL> cookie . get_sid ( ) <EOL> except KeyError : <EOL> cookie . set_sid ( sid ) <EOL> global CURRENT_SESSION <EOL> ppath = get_session_pickle_path ( cookie . get_sid ( ) ) <EOL> if isfile ( ppath ) : <EOL> try : <EOL> with open ( ppath , '<STR_LIT:rb>' ) as session_pickle : <EOL> CURRENT_SESSION = pickle_load ( session_pickle ) <EOL> CURRENT_SESSION . init_cookie ( CURRENT_SESSION . get_sid ( ) ) <EOL> except Exception , e : <EOL> CURRENT_SESSION = Session ( cookie ) <EOL> else : <EOL> CURRENT_SESSION = Session ( cookie ) <EOL> def get_session ( ) : <EOL> if CURRENT_SESSION is None : <EOL> raise NoSessionError <EOL> return CURRENT_SESSION <EOL> def invalidate_session ( ) : <EOL> global CURRENT_SESSION <EOL> if CURRENT_SESSION is None : <EOL> return <EOL> CURRENT_SESSION . cookie . set_expired ( ) <EOL> ppath = get_session_pickle_path ( CURRENT_SESSION . get_sid ( ) ) <EOL> if isfile ( ppath ) : <EOL> remove ( ppath ) <EOL> def close_session ( ) : <EOL> if CURRENT_SESSION is None : <EOL> return <EOL> try : <EOL> makedirs ( SESSIONS_DIR ) <EOL> except OSError , e : <EOL> if e . errno == <NUM_LIT> : <EOL> pass <EOL> else : <EOL> raise <EOL> tmp_file_path = None <EOL> try : <EOL> tmp_file_fh , tmp_file_path = mkstemp ( ) <EOL> os_close ( tmp_file_fh ) <EOL> with open ( tmp_file_path , '<STR_LIT:wb>' ) as tmp_file : <EOL> pickle_dump ( CURRENT_SESSION , tmp_file ) <EOL> copy ( tmp_file_path , get_session_pickle_path ( CURRENT_SESSION . get_sid ( ) ) ) <EOL> except IOError : <EOL> raise SessionStoreError <EOL> finally : <EOL> if tmp_file_path is not None : <EOL> remove ( tmp_file_path ) <EOL> def save_conf ( config ) : <EOL> get_session ( ) [ '<STR_LIT>' ] = config <EOL> return { } <EOL> def load_conf ( ) : <EOL> try : <EOL> return { <EOL> '<STR_LIT>' : get_session ( ) [ '<STR_LIT>' ] , <EOL> } <EOL> except KeyError : <EOL> return { } <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> try : <EOL> get_session ( ) <EOL> assert False <EOL> except NoSessionError : <EOL> pass <EOL> init_session ( '<STR_LIT:127.0.0.1>' ) <EOL> try : <EOL> session = get_session ( ) <EOL> session [ '<STR_LIT:foo>' ] = '<STR_LIT:bar>' <EOL> except NoSessionError : <EOL> assert False <EOL> init_session ( '<STR_LIT:127.0.0.1>' ) <EOL> tmp_file_path = None <EOL> try : <EOL> tmp_file_fh , tmp_file_path = mkstemp ( ) <EOL> os_close ( tmp_file_fh ) <EOL> session = get_session ( ) <EOL> session [ '<STR_LIT:foo>' ] = '<STR_LIT:bar>' <EOL> with open ( tmp_file_path , '<STR_LIT:wb>' ) as tmp_file : <EOL> pickle_dump ( session , tmp_file ) <EOL> del session <EOL> with open ( tmp_file_path , '<STR_LIT:rb>' ) as tmp_file : <EOL> session = pickle_load ( tmp_file ) <EOL> assert session [ '<STR_LIT:foo>' ] == '<STR_LIT:bar>' <EOL> finally : <EOL> if tmp_file_path is not None : <EOL> remove ( tmp_file_path ) </s>
