<s> """<STR_LIT>""" <EOL> try : <EOL> from http . cookies import SimpleCookie <EOL> except ImportError : <EOL> from Cookie import SimpleCookie <EOL> import time <EOL> import random <EOL> import os <EOL> import datetime <EOL> import six <EOL> import threading <EOL> import tempfile <EOL> try : <EOL> import cPickle <EOL> except ImportError : <EOL> import pickle as cPickle <EOL> try : <EOL> from hashlib import md5 <EOL> except ImportError : <EOL> from md5 import md5 <EOL> from paste import wsgilib <EOL> from paste import request <EOL> class SessionMiddleware ( object ) : <EOL> def __init__ ( self , application , global_conf = None , ** factory_kw ) : <EOL> self . application = application <EOL> self . factory_kw = factory_kw <EOL> def __call__ ( self , environ , start_response ) : <EOL> session_factory = SessionFactory ( environ , ** self . factory_kw ) <EOL> environ [ '<STR_LIT>' ] = session_factory <EOL> remember_headers = [ ] <EOL> def session_start_response ( status , headers , exc_info = None ) : <EOL> if not session_factory . created : <EOL> remember_headers [ : ] = [ status , headers ] <EOL> return start_response ( status , headers ) <EOL> headers . append ( session_factory . set_cookie_header ( ) ) <EOL> return start_response ( status , headers , exc_info ) <EOL> app_iter = self . application ( environ , session_start_response ) <EOL> def start ( ) : <EOL> if session_factory . created and remember_headers : <EOL> status , headers = remember_headers <EOL> headers . append ( session_factory . set_cookie_header ( ) ) <EOL> exc = ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> start_response ( status , headers , ( exc . __class__ , exc , None ) ) <EOL> def close ( ) : <EOL> if session_factory . used : <EOL> session_factory . close ( ) <EOL> return wsgilib . add_start_close ( app_iter , start , close ) <EOL> class SessionFactory ( object ) : <EOL> def __init__ ( self , environ , cookie_name = '<STR_LIT>' , <EOL> session_class = None , <EOL> session_expiration = <NUM_LIT> * <NUM_LIT:12> , <EOL> ** session_class_kw ) : <EOL> self . created = False <EOL> self . used = False <EOL> self . environ = environ <EOL> self . cookie_name = cookie_name <EOL> self . session = None <EOL> self . session_class = session_class or FileSession <EOL> self . session_class_kw = session_class_kw <EOL> self . expiration = session_expiration <EOL> def __call__ ( self ) : <EOL> self . used = True <EOL> if self . session is not None : <EOL> return self . session . data ( ) <EOL> cookies = request . get_cookies ( self . environ ) <EOL> session = None <EOL> if self . cookie_name in cookies : <EOL> self . sid = cookies [ self . cookie_name ] . value <EOL> try : <EOL> session = self . session_class ( self . sid , create = False , <EOL> ** self . session_class_kw ) <EOL> except KeyError : <EOL> pass <EOL> if session is None : <EOL> self . created = True <EOL> self . sid = self . make_sid ( ) <EOL> session = self . session_class ( self . sid , create = True , <EOL> ** self . session_class_kw ) <EOL> session . clean_up ( ) <EOL> self . session = session <EOL> return session . data ( ) <EOL> def has_session ( self ) : <EOL> if self . session is not None : <EOL> return True <EOL> cookies = request . get_cookies ( self . environ ) <EOL> if cookies . has_key ( self . cookie_name ) : <EOL> return True <EOL> return False <EOL> def make_sid ( self ) : <EOL> return ( '<STR_LIT>' . join ( [ '<STR_LIT>' % x for x in time . localtime ( time . time ( ) ) [ : <NUM_LIT:6> ] ] ) <EOL> + '<STR_LIT:->' + self . unique_id ( ) ) <EOL> def unique_id ( self , for_object = None ) : <EOL> """<STR_LIT>""" <EOL> r = [ time . time ( ) , random . random ( ) ] <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> r . append ( os . times ( ) ) <EOL> if for_object is not None : <EOL> r . append ( id ( for_object ) ) <EOL> content = str ( r ) <EOL> if six . PY3 : <EOL> content = content . encode ( '<STR_LIT:utf8>' ) <EOL> md5_hash = md5 ( content ) <EOL> try : <EOL> return md5_hash . hexdigest ( ) <EOL> except AttributeError : <EOL> hexdigest = [ ] <EOL> for char in md5_hash . digest ( ) : <EOL> hexdigest . append ( '<STR_LIT>' % ord ( char ) ) <EOL> return '<STR_LIT>' . join ( hexdigest ) <EOL> def set_cookie_header ( self ) : <EOL> c = SimpleCookie ( ) <EOL> c [ self . cookie_name ] = self . sid <EOL> c [ self . cookie_name ] [ '<STR_LIT:path>' ] = '<STR_LIT:/>' <EOL> gmt_expiration_time = time . gmtime ( time . time ( ) + ( self . expiration * <NUM_LIT> ) ) <EOL> c [ self . cookie_name ] [ '<STR_LIT>' ] = time . strftime ( "<STR_LIT>" , gmt_expiration_time ) <EOL> name , value = str ( c ) . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> return ( name , value ) <EOL> def close ( self ) : <EOL> if self . session is not None : <EOL> self . session . close ( ) <EOL> last_cleanup = None <EOL> cleaning_up = False <EOL> cleanup_cycle = datetime . timedelta ( seconds = <NUM_LIT:15> * <NUM_LIT> ) <EOL> class FileSession ( object ) : <EOL> def __init__ ( self , sid , create = False , session_file_path = tempfile . gettempdir ( ) , <EOL> chmod = None , <EOL> expiration = <NUM_LIT> , <EOL> ) : <EOL> if chmod and isinstance ( chmod , ( six . binary_type , six . text_type ) ) : <EOL> chmod = int ( chmod , <NUM_LIT:8> ) <EOL> self . chmod = chmod <EOL> if not sid : <EOL> raise KeyError <EOL> self . session_file_path = session_file_path <EOL> self . sid = sid <EOL> if not create : <EOL> if not os . path . exists ( self . filename ( ) ) : <EOL> raise KeyError <EOL> self . _data = None <EOL> self . expiration = expiration <EOL> def filename ( self ) : <EOL> return os . path . join ( self . session_file_path , self . sid ) <EOL> def data ( self ) : <EOL> if self . _data is not None : <EOL> return self . _data <EOL> if os . path . exists ( self . filename ( ) ) : <EOL> f = open ( self . filename ( ) , '<STR_LIT:rb>' ) <EOL> self . _data = cPickle . load ( f ) <EOL> f . close ( ) <EOL> else : <EOL> self . _data = { } <EOL> return self . _data <EOL> def close ( self ) : <EOL> if self . _data is not None : <EOL> filename = self . filename ( ) <EOL> exists = os . path . exists ( filename ) <EOL> if not self . _data : <EOL> if exists : <EOL> os . unlink ( filename ) <EOL> else : <EOL> f = open ( filename , '<STR_LIT:wb>' ) <EOL> cPickle . dump ( self . _data , f ) <EOL> f . close ( ) <EOL> if not exists and self . chmod : <EOL> os . chmod ( filename , self . chmod ) <EOL> def _clean_up ( self ) : <EOL> global cleaning_up <EOL> try : <EOL> exp_time = datetime . timedelta ( seconds = self . expiration * <NUM_LIT> ) <EOL> now = datetime . datetime . now ( ) <EOL> for root , dirs , files in os . walk ( self . session_file_path ) : <EOL> for f in files : <EOL> self . _clean_up_file ( f , exp_time = exp_time , now = now ) <EOL> finally : <EOL> cleaning_up = False <EOL> def _clean_up_file ( self , f , exp_time , now ) : <EOL> t = f . split ( "<STR_LIT:->" ) <EOL> if len ( t ) != <NUM_LIT:2> : <EOL> return <EOL> t = t [ <NUM_LIT:0> ] <EOL> try : <EOL> sess_time = datetime . datetime ( <EOL> int ( t [ <NUM_LIT:0> : <NUM_LIT:4> ] ) , <EOL> int ( t [ <NUM_LIT:4> : <NUM_LIT:6> ] ) , <EOL> int ( t [ <NUM_LIT:6> : <NUM_LIT:8> ] ) , <EOL> int ( t [ <NUM_LIT:8> : <NUM_LIT:10> ] ) , <EOL> int ( t [ <NUM_LIT:10> : <NUM_LIT:12> ] ) , <EOL> int ( t [ <NUM_LIT:12> : <NUM_LIT> ] ) ) <EOL> except ValueError : <EOL> return <EOL> if sess_time + exp_time < now : <EOL> os . remove ( os . path . join ( self . session_file_path , f ) ) <EOL> def clean_up ( self ) : <EOL> global last_cleanup , cleanup_cycle , cleaning_up <EOL> now = datetime . datetime . now ( ) <EOL> if cleaning_up : <EOL> return <EOL> if not last_cleanup or last_cleanup + cleanup_cycle < now : <EOL> if not cleaning_up : <EOL> cleaning_up = True <EOL> try : <EOL> last_cleanup = now <EOL> t = threading . Thread ( target = self . _clean_up ) <EOL> t . start ( ) <EOL> except : <EOL> cleaning_up = False <EOL> raise <EOL> class _NoDefault ( object ) : <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> NoDefault = _NoDefault ( ) <EOL> def make_session_middleware ( <EOL> app , global_conf , <EOL> session_expiration = NoDefault , <EOL> expiration = NoDefault , <EOL> cookie_name = NoDefault , <EOL> session_file_path = NoDefault , <EOL> chmod = NoDefault ) : <EOL> """<STR_LIT>""" <EOL> if session_expiration is NoDefault : <EOL> session_expiration = global_conf . get ( '<STR_LIT>' , <NUM_LIT> * <NUM_LIT:12> ) <EOL> session_expiration = int ( session_expiration ) <EOL> if expiration is NoDefault : <EOL> expiration = global_conf . get ( '<STR_LIT>' , <NUM_LIT> * <NUM_LIT> ) <EOL> expiration = int ( expiration ) <EOL> if cookie_name is NoDefault : <EOL> cookie_name = global_conf . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if session_file_path is NoDefault : <EOL> session_file_path = global_conf . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if chmod is NoDefault : <EOL> chmod = global_conf . get ( '<STR_LIT>' , None ) <EOL> return SessionMiddleware ( <EOL> app , session_expiration = session_expiration , <EOL> expiration = expiration , cookie_name = cookie_name , <EOL> session_file_path = session_file_path , chmod = chmod ) </s>
