<s> """<STR_LIT>""" <EOL> from functools import update_wrapper <EOL> from werkzeug . wsgi import ClosingIterator <EOL> from werkzeug . _compat import PY2 , implements_bool <EOL> try : <EOL> from greenlet import getcurrent as get_ident <EOL> except ImportError : <EOL> try : <EOL> from thread import get_ident <EOL> except ImportError : <EOL> from _thread import get_ident <EOL> def release_local ( local ) : <EOL> """<STR_LIT>""" <EOL> local . __release_local__ ( ) <EOL> class Local ( object ) : <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> object . __setattr__ ( self , '<STR_LIT>' , { } ) <EOL> object . __setattr__ ( self , '<STR_LIT>' , get_ident ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . __storage__ . items ( ) ) <EOL> def __call__ ( self , proxy ) : <EOL> """<STR_LIT>""" <EOL> return LocalProxy ( self , proxy ) <EOL> def __release_local__ ( self ) : <EOL> self . __storage__ . pop ( self . __ident_func__ ( ) , None ) <EOL> def __getattr__ ( self , name ) : <EOL> try : <EOL> return self . __storage__ [ self . __ident_func__ ( ) ] [ name ] <EOL> except KeyError : <EOL> raise AttributeError ( name ) <EOL> def __setattr__ ( self , name , value ) : <EOL> ident = self . __ident_func__ ( ) <EOL> storage = self . __storage__ <EOL> try : <EOL> storage [ ident ] [ name ] = value <EOL> except KeyError : <EOL> storage [ ident ] = { name : value } <EOL> def __delattr__ ( self , name ) : <EOL> try : <EOL> del self . __storage__ [ self . __ident_func__ ( ) ] [ name ] <EOL> except KeyError : <EOL> raise AttributeError ( name ) <EOL> class LocalStack ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _local = Local ( ) <EOL> def __release_local__ ( self ) : <EOL> self . _local . __release_local__ ( ) <EOL> def _get__ident_func__ ( self ) : <EOL> return self . _local . __ident_func__ <EOL> def _set__ident_func__ ( self , value ) : <EOL> object . __setattr__ ( self . _local , '<STR_LIT>' , value ) <EOL> __ident_func__ = property ( _get__ident_func__ , _set__ident_func__ ) <EOL> del _get__ident_func__ , _set__ident_func__ <EOL> def __call__ ( self ) : <EOL> def _lookup ( ) : <EOL> rv = self . top <EOL> if rv is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return rv <EOL> return LocalProxy ( _lookup ) <EOL> def push ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> rv = getattr ( self . _local , '<STR_LIT>' , None ) <EOL> if rv is None : <EOL> self . _local . stack = rv = [ ] <EOL> rv . append ( obj ) <EOL> return rv <EOL> def pop ( self ) : <EOL> """<STR_LIT>""" <EOL> stack = getattr ( self . _local , '<STR_LIT>' , None ) <EOL> if stack is None : <EOL> return None <EOL> elif len ( stack ) == <NUM_LIT:1> : <EOL> release_local ( self . _local ) <EOL> return stack [ - <NUM_LIT:1> ] <EOL> else : <EOL> return stack . pop ( ) <EOL> @ property <EOL> def top ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _local . stack [ - <NUM_LIT:1> ] <EOL> except ( AttributeError , IndexError ) : <EOL> return None <EOL> class LocalManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , locals = None , ident_func = None ) : <EOL> if locals is None : <EOL> self . locals = [ ] <EOL> elif isinstance ( locals , Local ) : <EOL> self . locals = [ locals ] <EOL> else : <EOL> self . locals = list ( locals ) <EOL> if ident_func is not None : <EOL> self . ident_func = ident_func <EOL> for local in self . locals : <EOL> object . __setattr__ ( local , '<STR_LIT>' , ident_func ) <EOL> else : <EOL> self . ident_func = get_ident <EOL> def get_ident ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . ident_func ( ) <EOL> def cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> for local in self . locals : <EOL> release_local ( local ) <EOL> def make_middleware ( self , app ) : <EOL> """<STR_LIT>""" <EOL> def application ( environ , start_response ) : <EOL> return ClosingIterator ( app ( environ , start_response ) , self . cleanup ) <EOL> return application <EOL> def middleware ( self , func ) : <EOL> """<STR_LIT>""" <EOL> return update_wrapper ( self . make_middleware ( func ) , func ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> len ( self . locals ) <EOL> ) <EOL> @ implements_bool <EOL> class LocalProxy ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , local , name = None ) : <EOL> object . __setattr__ ( self , '<STR_LIT>' , local ) <EOL> object . __setattr__ ( self , '<STR_LIT>' , name ) <EOL> def _get_current_object ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self . __local , '<STR_LIT>' ) : <EOL> return self . __local ( ) <EOL> try : <EOL> return getattr ( self . __local , self . __name__ ) <EOL> except AttributeError : <EOL> raise RuntimeError ( '<STR_LIT>' % self . __name__ ) <EOL> @ property <EOL> def __dict__ ( self ) : <EOL> try : <EOL> return self . _get_current_object ( ) . __dict__ <EOL> except RuntimeError : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> def __repr__ ( self ) : <EOL> try : <EOL> obj = self . _get_current_object ( ) <EOL> except RuntimeError : <EOL> return '<STR_LIT>' % self . __class__ . __name__ <EOL> return repr ( obj ) <EOL> def __bool__ ( self ) : <EOL> try : <EOL> return bool ( self . _get_current_object ( ) ) <EOL> except RuntimeError : <EOL> return False <EOL> def __unicode__ ( self ) : <EOL> try : <EOL> return unicode ( self . _get_current_object ( ) ) <EOL> except RuntimeError : <EOL> return repr ( self ) <EOL> def __dir__ ( self ) : <EOL> try : <EOL> return dir ( self . _get_current_object ( ) ) <EOL> except RuntimeError : <EOL> return [ ] <EOL> def __getattr__ ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> return dir ( self . _get_current_object ( ) ) <EOL> return getattr ( self . _get_current_object ( ) , name ) <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _get_current_object ( ) [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> del self . _get_current_object ( ) [ key ] <EOL> if PY2 : <EOL> __getslice__ = lambda x , i , j : x . _get_current_object ( ) [ i : j ] <EOL> def __setslice__ ( self , i , j , seq ) : <EOL> self . _get_current_object ( ) [ i : j ] = seq <EOL> def __delslice__ ( self , i , j ) : <EOL> del self . _get_current_object ( ) [ i : j ] <EOL> __setattr__ = lambda x , n , v : setattr ( x . _get_current_object ( ) , n , v ) <EOL> __delattr__ = lambda x , n : delattr ( x . _get_current_object ( ) , n ) <EOL> __str__ = lambda x : str ( x . _get_current_object ( ) ) <EOL> __lt__ = lambda x , o : x . _get_current_object ( ) < o <EOL> __le__ = lambda x , o : x . _get_current_object ( ) <= o <EOL> __eq__ = lambda x , o : x . _get_current_object ( ) == o <EOL> __ne__ = lambda x , o : x . _get_current_object ( ) != o <EOL> __gt__ = lambda x , o : x . _get_current_object ( ) > o <EOL> __ge__ = lambda x , o : x . _get_current_object ( ) >= o <EOL> __cmp__ = lambda x , o : cmp ( x . _get_current_object ( ) , o ) <EOL> __hash__ = lambda x : hash ( x . _get_current_object ( ) ) <EOL> __call__ = lambda x , * a , ** kw : x . _get_current_object ( ) ( * a , ** kw ) <EOL> __len__ = lambda x : len ( x . _get_current_object ( ) ) <EOL> __getitem__ = lambda x , i : x . _get_current_object ( ) [ i ] <EOL> __iter__ = lambda x : iter ( x . _get_current_object ( ) ) <EOL> __contains__ = lambda x , i : i in x . _get_current_object ( ) <EOL> __add__ = lambda x , o : x . _get_current_object ( ) + o <EOL> __sub__ = lambda x , o : x . _get_current_object ( ) - o <EOL> __mul__ = lambda x , o : x . _get_current_object ( ) * o <EOL> __floordiv__ = lambda x , o : x . _get_current_object ( ) // o <EOL> __mod__ = lambda x , o : x . _get_current_object ( ) % o <EOL> __divmod__ = lambda x , o : x . _get_current_object ( ) . __divmod__ ( o ) <EOL> __pow__ = lambda x , o : x . _get_current_object ( ) ** o <EOL> __lshift__ = lambda x , o : x . _get_current_object ( ) << o <EOL> __rshift__ = lambda x , o : x . _get_current_object ( ) >> o <EOL> __and__ = lambda x , o : x . _get_current_object ( ) & o <EOL> __xor__ = lambda x , o : x . _get_current_object ( ) ^ o <EOL> __or__ = lambda x , o : x . _get_current_object ( ) | o <EOL> __div__ = lambda x , o : x . _get_current_object ( ) . __div__ ( o ) <EOL> __truediv__ = lambda x , o : x . _get_current_object ( ) . __truediv__ ( o ) <EOL> __neg__ = lambda x : - ( x . _get_current_object ( ) ) <EOL> __pos__ = lambda x : + ( x . _get_current_object ( ) ) <EOL> __abs__ = lambda x : abs ( x . _get_current_object ( ) ) <EOL> __invert__ = lambda x : ~ ( x . _get_current_object ( ) ) <EOL> __complex__ = lambda x : complex ( x . _get_current_object ( ) ) <EOL> __int__ = lambda x : int ( x . _get_current_object ( ) ) <EOL> __long__ = lambda x : long ( x . _get_current_object ( ) ) <EOL> __float__ = lambda x : float ( x . _get_current_object ( ) ) <EOL> __oct__ = lambda x : oct ( x . _get_current_object ( ) ) <EOL> __hex__ = lambda x : hex ( x . _get_current_object ( ) ) <EOL> __index__ = lambda x : x . _get_current_object ( ) . __index__ ( ) <EOL> __coerce__ = lambda x , o : x . _get_current_object ( ) . __coerce__ ( x , o ) <EOL> __enter__ = lambda x : x . _get_current_object ( ) . __enter__ ( ) <EOL> __exit__ = lambda x , * a , ** kw : x . _get_current_object ( ) . __exit__ ( * a , ** kw ) <EOL> __radd__ = lambda x , o : o + x . _get_current_object ( ) <EOL> __rsub__ = lambda x , o : o - x . _get_current_object ( ) <EOL> __rmul__ = lambda x , o : o * x . _get_current_object ( ) <EOL> __rdiv__ = lambda x , o : o / x . _get_current_object ( ) <EOL> if PY2 : <EOL> __rtruediv__ = lambda x , o : x . _get_current_object ( ) . __rtruediv__ ( o ) <EOL> else : <EOL> __rtruediv__ = __rdiv__ <EOL> __rfloordiv__ = lambda x , o : o // x . _get_current_object ( ) <EOL> __rmod__ = lambda x , o : o % x . _get_current_object ( ) <EOL> __rdivmod__ = lambda x , o : x . _get_current_object ( ) . __rdivmod__ ( o ) </s>
