<s> r"""<STR_LIT>""" <EOL> import re <EOL> import os <EOL> import sys <EOL> import tempfile <EOL> from os import path <EOL> from time import time <EOL> from random import random <EOL> try : <EOL> from hashlib import sha1 <EOL> except ImportError : <EOL> from sha import new as sha1 <EOL> from cPickle import dump , load , HIGHEST_PROTOCOL <EOL> from werkzeug . datastructures import CallbackDict <EOL> from werkzeug . utils import dump_cookie , parse_cookie <EOL> from werkzeug . wsgi import ClosingIterator <EOL> from werkzeug . posixemulation import rename <EOL> _sha1_re = re . compile ( r'<STR_LIT>' ) <EOL> def _urandom ( ) : <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> return os . urandom ( <NUM_LIT:30> ) <EOL> return random ( ) <EOL> def generate_key ( salt = None ) : <EOL> return sha1 ( '<STR_LIT>' % ( salt , time ( ) , _urandom ( ) ) ) . hexdigest ( ) <EOL> class ModificationTrackingDict ( CallbackDict ) : <EOL> __slots__ = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> def on_update ( self ) : <EOL> self . modified = True <EOL> self . modified = False <EOL> CallbackDict . __init__ ( self , on_update = on_update ) <EOL> dict . update ( self , * args , ** kwargs ) <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> missing = object ( ) <EOL> result = object . __new__ ( self . __class__ ) <EOL> for name in self . __slots__ : <EOL> val = getattr ( self , name , missing ) <EOL> if val is not missing : <EOL> setattr ( result , name , val ) <EOL> return result <EOL> def __copy__ ( self ) : <EOL> return self . copy ( ) <EOL> class Session ( ModificationTrackingDict ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ModificationTrackingDict . __slots__ + ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , data , sid , new = False ) : <EOL> ModificationTrackingDict . __init__ ( self , data ) <EOL> self . sid = sid <EOL> self . new = new <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> dict . __repr__ ( self ) , <EOL> self . should_save and '<STR_LIT:*>' or '<STR_LIT>' <EOL> ) <EOL> @ property <EOL> def should_save ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . modified <EOL> class SessionStore ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , session_class = None ) : <EOL> if session_class is None : <EOL> session_class = Session <EOL> self . session_class = session_class <EOL> def is_valid_key ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return _sha1_re . match ( key ) is not None <EOL> def generate_key ( self , salt = None ) : <EOL> """<STR_LIT>""" <EOL> return generate_key ( salt ) <EOL> def new ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . session_class ( { } , self . generate_key ( ) , True ) <EOL> def save ( self , session ) : <EOL> """<STR_LIT>""" <EOL> def save_if_modified ( self , session ) : <EOL> """<STR_LIT>""" <EOL> if session . should_save : <EOL> self . save ( session ) <EOL> def delete ( self , session ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , sid ) : <EOL> """<STR_LIT>""" <EOL> return self . session_class ( { } , sid , True ) <EOL> _fs_transaction_suffix = '<STR_LIT>' <EOL> class FilesystemSessionStore ( SessionStore ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path = None , filename_template = '<STR_LIT>' , <EOL> session_class = None , renew_missing = False , mode = <NUM_LIT:0> <NUM_LIT> ) : <EOL> SessionStore . __init__ ( self , session_class ) <EOL> if path is None : <EOL> path = tempfile . gettempdir ( ) <EOL> self . path = path <EOL> if isinstance ( filename_template , unicode ) : <EOL> filename_template = filename_template . encode ( <EOL> sys . getfilesystemencoding ( ) or '<STR_LIT:utf-8>' ) <EOL> assert not filename_template . endswith ( _fs_transaction_suffix ) , '<STR_LIT>' % _fs_transaction_suffix <EOL> self . filename_template = filename_template <EOL> self . renew_missing = renew_missing <EOL> self . mode = mode <EOL> def get_session_filename ( self , sid ) : <EOL> if isinstance ( sid , unicode ) : <EOL> sid = sid . encode ( sys . getfilesystemencoding ( ) or '<STR_LIT:utf-8>' ) <EOL> return path . join ( self . path , self . filename_template % sid ) <EOL> def save ( self , session ) : <EOL> fn = self . get_session_filename ( session . sid ) <EOL> fd , tmp = tempfile . mkstemp ( suffix = _fs_transaction_suffix , <EOL> dir = self . path ) <EOL> f = os . fdopen ( fd , '<STR_LIT:wb>' ) <EOL> try : <EOL> dump ( dict ( session ) , f , HIGHEST_PROTOCOL ) <EOL> finally : <EOL> f . close ( ) <EOL> try : <EOL> rename ( tmp , fn ) <EOL> os . chmod ( fn , self . mode ) <EOL> except ( IOError , OSError ) : <EOL> pass <EOL> def delete ( self , session ) : <EOL> fn = self . get_session_filename ( session . sid ) <EOL> try : <EOL> os . unlink ( fn ) <EOL> except OSError : <EOL> pass <EOL> def get ( self , sid ) : <EOL> if not self . is_valid_key ( sid ) : <EOL> return self . new ( ) <EOL> try : <EOL> f = open ( self . get_session_filename ( sid ) , '<STR_LIT:rb>' ) <EOL> except IOError : <EOL> if self . renew_missing : <EOL> return self . new ( ) <EOL> data = { } <EOL> else : <EOL> try : <EOL> try : <EOL> data = load ( f ) <EOL> except Exception : <EOL> data = { } <EOL> finally : <EOL> f . close ( ) <EOL> return self . session_class ( data , sid , False ) <EOL> def list ( self ) : <EOL> """<STR_LIT>""" <EOL> before , after = self . filename_template . split ( '<STR_LIT:%s>' , <NUM_LIT:1> ) <EOL> filename_re = re . compile ( r'<STR_LIT>' % ( re . escape ( before ) , <EOL> re . escape ( after ) ) ) <EOL> result = [ ] <EOL> for filename in os . listdir ( self . path ) : <EOL> if filename . endswith ( _fs_transaction_suffix ) : <EOL> continue <EOL> match = filename_re . match ( filename ) <EOL> if match is not None : <EOL> result . append ( match . group ( <NUM_LIT:1> ) ) <EOL> return result <EOL> class SessionMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , store , cookie_name = '<STR_LIT>' , <EOL> cookie_age = None , cookie_expires = None , cookie_path = '<STR_LIT:/>' , <EOL> cookie_domain = None , cookie_secure = None , <EOL> cookie_httponly = False , environ_key = '<STR_LIT>' ) : <EOL> self . app = app <EOL> self . store = store <EOL> self . cookie_name = cookie_name <EOL> self . cookie_age = cookie_age <EOL> self . cookie_expires = cookie_expires <EOL> self . cookie_path = cookie_path <EOL> self . cookie_domain = cookie_domain <EOL> self . cookie_secure = cookie_secure <EOL> self . cookie_httponly = cookie_httponly <EOL> self . environ_key = environ_key <EOL> def __call__ ( self , environ , start_response ) : <EOL> cookie = parse_cookie ( environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> sid = cookie . get ( self . cookie_name , None ) <EOL> if sid is None : <EOL> session = self . store . new ( ) <EOL> else : <EOL> session = self . store . get ( sid ) <EOL> environ [ self . environ_key ] = session <EOL> def injecting_start_response ( status , headers , exc_info = None ) : <EOL> if session . should_save : <EOL> self . store . save ( session ) <EOL> headers . append ( ( '<STR_LIT>' , dump_cookie ( self . cookie_name , <EOL> session . sid , self . cookie_age , <EOL> self . cookie_expires , self . cookie_path , <EOL> self . cookie_domain , self . cookie_secure , <EOL> self . cookie_httponly ) ) ) <EOL> return start_response ( status , headers , exc_info ) <EOL> return ClosingIterator ( self . app ( environ , injecting_start_response ) , <EOL> lambda : self . store . save_if_modified ( session ) ) </s>
