
_#8                 @   s"  d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m Z y d d l	 m
 Z WnK e k
 r y d d	 l m Z Wn" e k
 r d d	 l m Z Yn XYn Xd
 d   Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z e Gd d   d e   Z d S)z
    werkzeug.local
    ~~~~~~~~~~~~~~

    This module implements context-local objects.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
    N)update_wrapper   )implements_bool)PY2)ClosingIterator)
getcurrent)	get_identc             C   s   |  j    d S)aM  Releases the contents of the local for the current context.
    This makes it possible to use locals without a manager.

    Example::

        >>> loc = Local()
        >>> loc.foo = 42
        >>> release_local(loc)
        >>> hasattr(loc, 'foo')
        False

    With this function one can release :class:`Local` objects as well
    as :class:`LocalStack` objects.  However it is not possible to
    release data held by proxies that way, one always has to retain
    a reference to the underlying local object in order to be able
    to release it.

    .. versionadded:: 0.6.1
    N)__release_local__)local r   2/tmp/pip-build-5gj8f0j9/Werkzeug/werkzeug/local.pyrelease_local   s    r   c               @   sj   e  Z d  Z d Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d d   Z
 d S)Local__storage____ident_func__c             C   s*   t  j |  d i   t  j |  d t  d  S)Nr   r   )object__setattr__r   )selfr   r   r   __init__8   s    zLocal.__init__c             C   s   t  |  j j    S)N)iterr   items)r   r   r   r   __iter__<   s    zLocal.__iter__c             C   s   t  |  |  S)zCreate a proxy for a name.)
LocalProxy)r   proxyr   r   r   __call__?   s    zLocal.__call__c             C   s   |  j  j |  j   d   d  S)N)r   popr   )r   r   r   r   r	   C   s    zLocal.__release_local__c             C   s>   y |  j  |  j   | SWn t k
 r9 t |   Yn Xd  S)N)r   r   KeyErrorAttributeError)r   namer   r   r   __getattr__F   s    zLocal.__getattr__c             C   sP   |  j    } |  j } y | | | | <Wn" t k
 rK | | i | | <Yn Xd  S)N)r   r   r   )r   r   valueidentZstorager   r   r   r   L   s    	zLocal.__setattr__c             C   s=   y |  j  |  j   | =Wn t k
 r8 t |   Yn Xd  S)N)r   r   r   r   )r   r   r   r   r   __delattr__T   s    zLocal.__delattr__N)r   r   )__name__
__module____qualname__	__slots__r   r   r   r	   r   r   r"   r   r   r   r   r   5   s   r   c               @   s   e  Z d  Z d Z d d   Z d d   Z e d d    Z e j d d    Z d	 d
   Z	 d d   Z
 d d   Z e d d    Z d S)
LocalStacka  This class works similar to a :class:`Local` but keeps a stack
    of objects instead.  This is best explained with an example::

        >>> ls = LocalStack()
        >>> ls.push(42)
        >>> ls.top
        42
        >>> ls.push(23)
        >>> ls.top
        23
        >>> ls.pop()
        23
        >>> ls.top
        42

    They can be force released by using a :class:`LocalManager` or with
    the :func:`release_local` function but the correct way is to pop the
    item from the stack after using.  When the stack is empty it will
    no longer be bound to the current context (and as such released).

    By calling the stack without arguments it returns a proxy that resolves to
    the topmost item on the stack.

    .. versionadded:: 0.6.1
    c             C   s   t    |  _ d  S)N)r   _local)r   r   r   r   r   v   s    zLocalStack.__init__c             C   s   |  j  j   d  S)N)r(   r	   )r   r   r   r   r	   y   s    zLocalStack.__release_local__c             C   s
   |  j  j S)N)r(   r   )r   r   r   r   r   |   s    zLocalStack.__ident_func__c             C   s   t  j |  j d |  d  S)Nr   )r   r   r(   )r   r    r   r   r   r      s    c                s     f d d   } t  |  S)Nc                 s%     j  }  |  d  k r! t d   |  S)Nzobject unbound)topRuntimeError)rv)r   r   r   _lookup   s    	z$LocalStack.__call__.<locals>._lookup)r   )r   r,   r   )r   r   r      s    zLocalStack.__call__c             C   sB   t  |  j d d  } | d k r1 g  |  j _ } | j |  | S)zPushes a new item to the stackstackN)getattrr(   r-   append)r   objr+   r   r   r   push   s
    zLocalStack.pushc             C   sZ   t  |  j d d  } | d k r% d St |  d k rL t |  j  | d S| j   Sd S)z}Removes the topmost item from the stack, will return the
        old value or `None` if the stack was already empty.
        r-   Nr   )r.   r(   lenr   r   )r   r-   r   r   r   r      s    zLocalStack.popc             C   s5   y |  j  j d SWn t t f k
 r0 d SYn Xd S)z[The topmost item on the stack.  If the stack is empty,
        `None` is returned.
        r   Nr2   )r(   r-   r   
IndexError)r   r   r   r   r)      s    zLocalStack.topN)r#   r$   r%   __doc__r   r	   propertyr   setterr   r1   r   r)   r   r   r   r   r'   [   s   	r'   c               @   sd   e  Z d  Z d Z d d d d  Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d S)LocalManageraN  Local objects cannot manage themselves. For that you need a local
    manager.  You can pass a local manager multiple locals or add them later
    by appending them to `manager.locals`.  Every time the manager cleans up,
    it will clean up all the data left in the locals for this context.

    The `ident_func` parameter can be added to override the default ident
    function for the wrapped locals.

    .. versionchanged:: 0.6.1
       Instead of a manager the :func:`release_local` function can be used
       as well.

    .. versionchanged:: 0.7
       `ident_func` was added.
    Nc             C   s   | d  k r g  |  _  n- t | t  r6 | g |  _  n t |  |  _  | d  k	 r | |  _ x0 |  j  D] } t j | d |  qd Wn	 t |  _ d  S)Nr   )locals
isinstancer   list
ident_funcr   r   r   )r   r9   r<   r
   r   r   r   r      s    	zLocalManager.__init__c             C   s
   |  j    S)a  Return the context identifier the local objects use internally for
        this context.  You cannot override this method to change the behavior
        but use it to link other context local objects (such as SQLAlchemy's
        scoped sessions) to the Werkzeug locals.

        .. versionchanged:: 0.7
           You can pass a different ident function to the local manager that
           will then be propagated to all the locals passed to the
           constructor.
        )r<   )r   r   r   r   r      s    zLocalManager.get_identc             C   s"   x |  j  D] } t |  q
 Wd S)zManually clean up the data in the locals for this context.  Call
        this at the end of the request or use `make_middleware()`.
        N)r9   r   )r   r
   r   r   r   cleanup   s    zLocalManager.cleanupc                s      f d d   } | S)zWWrap a WSGI application so that cleaning up happens after
        request end.
        c                s   t    |  |   j  S)N)r   r=   )environZstart_response)appr   r   r   application   s    z1LocalManager.make_middleware.<locals>.applicationr   )r   r?   r@   r   )r?   r   r   make_middleware   s    zLocalManager.make_middlewarec             C   s   t  |  j |  |  S)as  Like `make_middleware` but for decorating functions.

        Example usage::

            @manager.middleware
            def application(environ, start_response):
                ...

        The difference to `make_middleware` is that the function passed
        will have all the arguments copied from the inner application
        (name, docstring, module).
        )r   rA   )r   funcr   r   r   
middleware   s    zLocalManager.middlewarec             C   s   d |  j  j t |  j  f S)Nz<%s storages: %d>)	__class__r#   r3   r9   )r   r   r   r   __repr__   s    zLocalManager.__repr__)
r#   r$   r%   r5   r   r   r=   rA   rC   rE   r   r   r   r   r8      s   
r8   c               @   s^  e  Z d  Z d Z dW Z d d d  Z d	 d
   Z e d d    Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z e r d d   Z d d   Z d d    Z d! d   Z d" d   Z d# d   Z d$ d   Z d% d   Z d& d   Z d' d   Z d( d   Z d) d   Z d* d   Z d+ d   Z d, d   Z d- d   Z  d. d   Z! d/ d   Z" d0 d   Z# d1 d   Z$ d2 d   Z% d3 d   Z& d4 d   Z' d5 d   Z( d6 d   Z) d7 d   Z* d8 d   Z+ d9 d   Z, d: d   Z- d; d   Z. d< d   Z/ d= d   Z0 d> d   Z1 d? d   Z2 d@ d   Z3 dA d   Z4 dB d   Z5 dC d   Z6 dD d   Z7 dE d   Z8 dF d   Z9 dG d   Z: dH d   Z; dI d   Z< dJ d   Z= dK d   Z> dL d   Z? dM d   Z@ dN d   ZA dO d   ZB dP d   ZC e rdQ d   ZD n eC ZD dR d   ZE dS d   ZF dT d   ZG dU d   ZH dV d   ZI d S)Xr   ah  Acts as a proxy for a werkzeug local.  Forwards all operations to
    a proxied object.  The only operations not supported for forwarding
    are right handed operands and any kind of assignment.

    Example usage::

        from werkzeug.local import Local
        l = Local()

        # these are proxies
        request = l('request')
        user = l('user')


        from werkzeug.local import LocalStack
        _response_local = LocalStack()

        # this is a proxy
        response = _response_local()

    Whenever something is bound to l.user / l.request the proxy objects
    will forward all operations.  If no object is bound a :exc:`RuntimeError`
    will be raised.

    To create proxies to :class:`Local` or :class:`LocalStack` objects,
    call the object as shown above.  If you want to have a proxy to an
    object looked up by a function, you can (as of Werkzeug 0.6.1) pass
    a function to the :class:`LocalProxy` constructor::

        session = LocalProxy(lambda: get_current_request().session)

    .. versionchanged:: 0.6.1
       The class can be instantiated with a callable as well now.
    __local__dict__r#   __wrapped__Nc             C   sY   t  j |  d |  t  j |  d |  t |  rU t | d  rU t  j |  d |  d  S)N_LocalProxy__localr#   r	   rH   )r   r   callablehasattr)r   r
   r   r   r   r   r   $  s    zLocalProxy.__init__c             C   s_   t  |  j d  s |  j   Sy t |  j |  j  SWn% t k
 rZ t d |  j   Yn Xd S)zReturn the current object.  This is useful if you want the real
        object behind the proxy at a time for performance reasons or because
        you want to pass the object into a different context.
        r	   zno object bound to %sN)rK   rI   r.   r#   r   r*   )r   r   r   r   _get_current_object,  s    
zLocalProxy._get_current_objectc             C   s6   y |  j    j SWn t k
 r1 t d   Yn Xd  S)NrG   )rL   rG   r*   r   )r   r   r   r   rG   8  s    zLocalProxy.__dict__c             C   s=   y |  j    } Wn  t k
 r2 d |  j j SYn Xt |  S)Nz<%s unbound>)rL   r*   rD   r#   repr)r   r0   r   r   r   rE   ?  s
    zLocalProxy.__repr__c             C   s1   y t  |  j    SWn t k
 r, d SYn Xd  S)NF)boolrL   r*   )r   r   r   r   __bool__F  s    zLocalProxy.__bool__c             C   s7   y t  |  j    SWn t k
 r2 t |   SYn Xd  S)N)unicoderL   r*   rM   )r   r   r   r   __unicode__L  s    zLocalProxy.__unicode__c             C   s1   y t  |  j    SWn t k
 r, g  SYn Xd  S)N)dirrL   r*   )r   r   r   r   __dir__R  s    zLocalProxy.__dir__c             C   s/   | d k r t  |  j    St |  j   |  S)N__members__)rR   rL   r.   )r   r   r   r   r   r   X  s    zLocalProxy.__getattr__c             C   s   | |  j    | <d  S)N)rL   )r   keyr    r   r   r   __setitem__]  s    zLocalProxy.__setitem__c             C   s   |  j    | =d  S)N)rL   )r   rU   r   r   r   __delitem__`  s    zLocalProxy.__delitem__c             C   s   |  j    | |  S)N)rL   )xijr   r   r   <lambda>d  s    zLocalProxy.<lambda>c             C   s   | |  j    | |  <d  S)N)rL   )r   rY   rZ   seqr   r   r   __setslice__f  s    zLocalProxy.__setslice__c             C   s   |  j    | |  =d  S)N)rL   )r   rY   rZ   r   r   r   __delslice__i  s    zLocalProxy.__delslice__c             C   s   t  |  j   | |  S)N)setattrrL   )rX   nvr   r   r   r[   l  s    c             C   s   t  |  j   |  S)N)delattrrL   )rX   r`   r   r   r   r[   m  s    c             C   s   t  |  j    S)N)strrL   )rX   r   r   r   r[   n  s    c             C   s   |  j    | k  S)N)rL   )rX   or   r   r   r[   o  s    c             C   s   |  j    | k S)N)rL   )rX   rd   r   r   r   r[   p  s    c             C   s   |  j    | k S)N)rL   )rX   rd   r   r   r   r[   q  s    c             C   s   |  j    | k S)N)rL   )rX   rd   r   r   r   r[   r  s    c             C   s   |  j    | k S)N)rL   )rX   rd   r   r   r   r[   s  s    c             C   s   |  j    | k S)N)rL   )rX   rd   r   r   r   r[   t  s    c             C   s   t  |  j   |  S)N)cmprL   )rX   rd   r   r   r   r[   u  s    c             C   s   t  |  j    S)N)hashrL   )rX   r   r   r   r[   v  s    c             O   s   |  j    | |   S)N)rL   )rX   akwr   r   r   r[   w  s    c             C   s   t  |  j    S)N)r3   rL   )rX   r   r   r   r[   x  s    c             C   s   |  j    | S)N)rL   )rX   rY   r   r   r   r[   y  s    c             C   s   t  |  j    S)N)r   rL   )rX   r   r   r   r[   z  s    c             C   s   | |  j    k S)N)rL   )rX   rY   r   r   r   r[   {  s    c             C   s   |  j    | S)N)rL   )rX   rd   r   r   r   r[   |  s    c             C   s   |  j    | S)N)rL   )rX   rd   r   r   r   r[   }  s    c             C   s   |  j    | S)N)rL   )rX   rd   r   r   r   r[   ~  s    c             C   s   |  j    | S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   |  j    | S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   |  j    j |  S)N)rL   
__divmod__)rX   rd   r   r   r   r[     s    c             C   s   |  j    | S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   |  j    | >S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   |  j    | ?S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   |  j    | @S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   |  j    | AS)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   |  j    | BS)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   |  j    j |  S)N)rL   __div__)rX   rd   r   r   r   r[     s    c             C   s   |  j    j |  S)N)rL   __truediv__)rX   rd   r   r   r   r[     s    c             C   s   |  j    S)N)rL   )rX   r   r   r   r[     s    c             C   s   |  j    
S)N)rL   )rX   r   r   r   r[     s    c             C   s   t  |  j    S)N)absrL   )rX   r   r   r   r[     s    c             C   s   |  j    S)N)rL   )rX   r   r   r   r[     s    c             C   s   t  |  j    S)N)complexrL   )rX   r   r   r   r[     s    c             C   s   t  |  j    S)N)intrL   )rX   r   r   r   r[     s    c             C   s   t  |  j    S)N)longrL   )rX   r   r   r   r[     s    c             C   s   t  |  j    S)N)floatrL   )rX   r   r   r   r[     s    c             C   s   t  |  j    S)N)octrL   )rX   r   r   r   r[     s    c             C   s   t  |  j    S)N)hexrL   )rX   r   r   r   r[     s    c             C   s   |  j    j   S)N)rL   	__index__)rX   r   r   r   r[     s    c             C   s   |  j    j |  |  S)N)rL   
__coerce__)rX   rd   r   r   r   r[     s    c             C   s   |  j    j   S)N)rL   	__enter__)rX   r   r   r   r[     s    c             O   s   |  j    j | |   S)N)rL   __exit__)rX   rg   rh   r   r   r   r[     s    c             C   s   | |  j    S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   | |  j    S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   | |  j    S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   | |  j    S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   |  j    j |  S)N)rL   __rtruediv__)rX   rd   r   r   r   r[     s    c             C   s   | |  j    S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   | |  j    S)N)rL   )rX   rd   r   r   r   r[     s    c             C   s   |  j    j |  S)N)rL   __rdivmod__)rX   rd   r   r   r   r[     s    c             C   s   t  j  |  j    S)N)copyrL   )rX   r   r   r   r[     s    c             C   s   t  j |  j   |  S)N)ry   deepcopyrL   )rX   memor   r   r   r[     s    )rF   rG   r#   rH   )Jr#   r$   r%   r5   r&   r   rL   r6   rG   rE   rO   rQ   rS   r   rV   rW   r   Z__getslice__r]   r^   r   r"   __str____lt____le____eq____ne____gt____ge__Z__cmp____hash__r   __len____getitem__r   __contains____add____sub____mul____floordiv____mod__ri   __pow__
__lshift__
__rshift____and____xor____or__rj   rk   __neg____pos____abs__
__invert____complex____int__Z__long__	__float__Z__oct__Z__hex__rs   rt   ru   rv   __radd____rsub____rmul__Z__rdiv__rw   __rfloordiv____rmod__rx   __copy____deepcopy__r   r   r   r   r      s   #r   )r5   ry   	functoolsr   _compatr   r   Zwsgir   Zgreenletr   r   ImportErrorthread_threadr   r   r   r'   r8   r   r   r   r   r   <module>
   s$   &RP