U
    zbG                     @   s  d dl Z d dlZd dl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 ejrzd dlmZ d dlmZ d d	lmZ ejd
ejdejf dZejd ddddZG dd dZG dd dZG dd dZG dd dZG dd deZeedddZG dd dZdS )     N)
ContextVarpartial)update_wrapper   )ClosingIterator)StartResponse)WSGIApplication)WSGIEnvironmentF.)bound)Local
LocalStack)localreturnc                 C   s   |    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__)r    r   2/tmp/pip-unpacked-wheel-lj3h0ozw/werkzeug/local.pyrelease_local   s    r   c                   @   s   e Zd ZdZddddZejejeej	f  dddZ
edd	d
dZddddZeej	dddZeej	ddddZeddddZdS )r   )_storageNr   c                 C   s   t | dtd d S )Nr   Zlocal_storage)object__setattr__r   selfr   r   r   __init__-   s    zLocal.__init__c                 C   s   t | ji  S N)iterr   getitemsr   r   r   r   __iter__0   s    zLocal.__iter__
LocalProxy)proxyr   c                 C   s
   t | |S )zCreate a proxy for a name.)r!   )r   r"   r   r   r   __call__3   s    zLocal.__call__c                 C   s   | j i  d S r   )r   setr   r   r   r   r   7   s    zLocal.__release_local__)namer   c                 C   s:   | j i }z
|| W S  tk
r4   t|d Y nX d S r   )r   r   KeyErrorAttributeErrorr   r%   valuesr   r   r   __getattr__:   s
    
zLocal.__getattr__)r%   valuer   c                 C   s(   | j i  }|||< | j | d S r   )r   r   copyr$   )r   r%   r+   r)   r   r   r   r   A   s    zLocal.__setattr__c                 C   sJ   | j i  }z||= | j | W n tk
rD   t|d Y nX d S r   )r   r   r,   r$   r&   r'   r(   r   r   r   __delattr__F   s    zLocal.__delattr__)__name__
__module____qualname__	__slots__r   tIteratorTupleintAnyr    strr#   r   r*   r   r-   r   r   r   r   r   *   s    r   c                   @   sx   e Zd ZdZddddZddddZddd	d
Zejej	ej dddZ
ejdddZeejdddZdS )r   a  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
    Nr   c                 C   s   t  | _d S r   )r   _localr   r   r   r   r   j   s    zLocalStack.__init__c                 C   s   | j   d S r   )r8   r   r   r   r   r   r   m   s    zLocalStack.__release_local__r!   c                    s   t jd fdd}t|S )Nr   c                     s    j } | d krtd| S )Nzobject unbound)topRuntimeError)rvr   r   r   _lookupq   s    z$LocalStack.__call__.<locals>._lookup)r2   r6   r!   )r   r<   r   r   r   r#   p   s    zLocalStack.__call__)objr   c                 C   s(   t | jdg  }|| || j_|S )zPushes a new item to the stackstack)getattrr8   r,   appendr>   )r   r=   r;   r   r   r   pushy   s    
zLocalStack.pushc                 C   sD   t | jdd}|dkrdS t|dkr8t| j |d S | S d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?   r8   lenr   pop)r   r>   r   r   r   rD      s    
zLocalStack.popc              	   C   s.   z| j jd W S  ttfk
r(   Y dS X dS )z[The topmost item on the stack.  If the stack is empty,
        `None` is returned.
        rB   N)r8   r>   r'   
IndexErrorr   r   r   r   r9      s    zLocalStack.top)r.   r/   r0   __doc__r   r   r#   r2   r6   ListrA   rD   propertyr9   r   r   r   r   r   O   s   	r   c                   @   st   e Zd ZdZdejejejee	f   ddddZ
ddddZd	d	d
ddZd	d	dddZedddZdS )LocalManagera[  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.

    .. versionchanged:: 2.0
        ``ident_func`` is deprecated and will be removed in Werkzeug
         2.1.

    .. versionchanged:: 0.6.1
        The :func:`release_local` function can be used instead of a
        manager.

    .. versionchanged:: 0.7
        The ``ident_func`` parameter was added.
    N)localsr   c                 C   s2   |d krg | _ nt|tr$|g| _ n
t|| _ d S r   )rJ   
isinstancer   list)r   rJ   r   r   r   r      s
    

zLocalManager.__init__r   c                 C   s   | j D ]}t| qd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)rJ   r   )r   r   r   r   r   cleanup   s    
zLocalManager.cleanupr	   )appr   c                    s"   ddt jt d fdd}|S )zWWrap a WSGI application so that cleaning up happens after
        request end.
        r
   r   )environstart_responser   c                    s   t  | |jS r   )r   rM   )rO   rP   rN   r   r   r   application   s    z1LocalManager.make_middleware.<locals>.application)r2   Iterablebytes)r   rN   rR   r   rQ   r   make_middleware   s
     zLocalManager.make_middleware)funcr   c                 C   s   t | ||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   rU   )r   rV   r   r   r   
middleware   s    zLocalManager.middlewarec                 C   s   dt | j dt| j dS )N<z storages: >)typer.   rC   rJ   r   r   r   r   __repr__   s    zLocalManager.__repr__)N)r.   r/   r0   rF   r2   OptionalrS   Unionr   r   r   rM   rU   rW   r7   r[   r   r   r   r   rI      s    
rI   c                   @   s   e Zd ZdZdZdejej ejej ejej e	ddddZ
dedd	d
dZddeje ejdddZedddZdejejejdddZdS )_ProxyLookupa  Descriptor that handles proxied attribute lookup for
    :class:`LocalProxy`.

    :param f: The built-in function this attribute is accessed through.
        Instead of looking up the special method, the function call
        is redone on the object.
    :param fallback: Return this function if the proxy is unbound
        instead of raising a :exc:`RuntimeError`.
    :param is_attr: This proxied name is an attribute, not a function.
        Call the fallback immediately to get the value.
    :param class_value: Value to return when accessed from the
        ``LocalProxy`` class directly. Used for ``__doc__`` so building
        docs still works.
    )bind_ffallbackis_attrclass_valuer%   NF)fr`   rb   ra   r   c                    sj   t  dr&dtjtjd fdd}n( d k	rJdtjtjd fdd}nd }|| _|| _|| _|| _d S )N__get__r!   instancer=   r   c                    s     |t|S r   rd   rZ   rf   r=   rc   r   r   r_      s    z%_ProxyLookup.__init__.<locals>.bind_fc                    s
   t  |S r   r   rh   ri   r   r   r_      s    )hasattrr2   r6   Callabler_   r`   rb   ra   )r   rc   r`   rb   ra   r_   r   ri   r   r      s    	
z_ProxyLookup.__init__r!   )ownerr%   r   c                 C   s
   || _ d S r   r%   )r   rl   r%   r   r   r   __set_name__  s    z_ProxyLookup.__set_name__)rf   rl   r   c                 C   s   |d kr| j d k	r| j S | S z| }W nB tk
rj   | jd krD | j||}| jrb|  Y S | Y S X | jd k	r| ||S t|| jS r   )	rb   _get_current_objectr:   r`   rd   ra   r_   r?   r%   )r   rf   rl   r=   r`   r   r   r   rd     s     




z_ProxyLookup.__get__r   c                 C   s   d| j  S )Nzproxy rm   r   r   r   r   r[   )  s    z_ProxyLookup.__repr__)rf   argskwargsr   c                 O   s   |  |t|||S )zSupport calling unbound methods from the class. For example,
        this happens with ``copy.copy``, which does
        ``type(x).__copy__(x)``. ``type(x)`` can't be proxied, so it
        returns the proxy type and descriptor.
        rg   )r   rf   rp   rq   r   r   r   r#   ,  s    z_ProxyLookup.__call__)NNNF)N)r.   r/   r0   rF   r1   r2   r\   rk   r6   boolr   r7   rn   rZ   rd   r[   r#   r   r   r   r   r^      s"       


r^   c                       s@   e Zd ZdZdZdejej ejej dd fddZ  Z	S )	_ProxyIOpzLook up an augmented assignment method on a proxied object. The
    method is wrapped to return the proxy instead of the object.
    r   N)rc   r`   r   c                    s2   t   | dtjtjd fdd}|| _d S )Nr!   re   c                    s,   t jt jdd fdd}||t|S )Nr!   )r   otherr   c                    s    | | S r   r   r   rt   )rc   rf   r   r   i_opB  s    
z0_ProxyIOp.__init__.<locals>.bind_f.<locals>.i_op)r2   r6   rd   rZ   )rf   r=   rv   ri   )rf   r   r_   A  s    z"_ProxyIOp.__init__.<locals>.bind_f)superr   r2   r6   rk   r_   )r   rc   r`   r_   	__class__ri   r   r   <  s    z_ProxyIOp.__init__)NN)
r.   r/   r0   rF   r1   r2   r\   rk   r   __classcell__r   r   rx   r   rs   5  s      
 
rs   )opr   c                    s(   t jt jt jd fdd}t t|S )z5Swap the argument order to turn an l-op into an r-op.)r=   rt   r   c                    s
    || S r   r   )r=   rt   r{   r   r   r_opN  s    z_l_to_r_op.<locals>.r_op)r2   r6   castr   )r{   r}   r   r|   r   
_l_to_r_opK  s    r   c                   @   s  e Zd ZdZdZdejdejg ejf f ej	e
 ddddZejdd	d
Zeedd ddZeedd dZee
ZeeZe ZeejZeejZeejZeejZeejZeejZ ee!Z"ee#dd dZ$ee%Z&ee'Z(ee)Z*ee+dd dZ,edd ddZ-edd Z.edd Z/edd Z0ee1Z2eej3Z4eej5Z6eej7Z8eej9Z:ee;Z<ee=Z>ee?Z@eejAZBeejCZDeejEZFeejGZHeejIZJeejKZLeejMZNeejOZPeeQZReeSZTeejUZVeejWZXeejYZZeej[Z\eej]Z^ee_ejCZ`ee_ejEZaee_ejGZbee_ejIZcee_ejKZdee_ejMZeee_ejOZfee_eQZgee_eSZhee_ejUZiee_ejWZjee_ejYZkee_ej[Zlee_ej]ZmenejoZpenejqZrenejsZtenejuZvenejwZxenejyZzenej{Z|enej}Z~enejZenejZenejZenejZenejZeejZeejZeeZeejZeeZeeZeeZeejZeeZeejZeejZeejZe Ze Ze Ze Ze Ze Ze ZeejZeejZdS )r!   au  A proxy to the object bound to a :class:`Local`. All operations
    on the proxy are forwarded to the bound object. If no object is
    bound, a :exc:`RuntimeError` is raised.

    .. code-block:: python

        from werkzeug.local import Local
        l = Local()

        # a proxy to whatever l.user is set to
        user = l("user")

        from werkzeug.local import LocalStack
        _request_stack = LocalStack()

        # a proxy to _request_stack.top
        request = _request_stack()

        # a proxy to the session attribute of the request proxy
        session = LocalProxy(lambda: request.session)

    ``__repr__`` and ``__class__`` are forwarded, so ``repr(x)`` and
    ``isinstance(x, cls)`` will look like the proxied object. Use
    ``issubclass(type(x), LocalProxy)`` to check if an object is a
    proxy.

    .. code-block:: python

        repr(user)  # <User admin>
        isinstance(user, User)  # True
        issubclass(type(user), LocalProxy)  # True

    :param local: The :class:`Local` or callable that provides the
        proxied object.
    :param name: The attribute name to look up on a :class:`Local`. Not
        used if a callable is given.

    .. versionchanged:: 2.0
        Updated proxied attributes and methods to reflect the current
        data model.

    .. versionchanged:: 0.6.1
        The class can be instantiated with a callable.
    )Z__localZ__name__wrapped__Nr   )r   r%   r   c                 C   s@   t | d| t | d| t|r<t|ds<t | d| d S )N_LocalProxy__local_LocalProxy__namer   r   )r   r   callablerj   )r   r   r%   r   r   r   r     s    zLocalProxy.__init__r   c                 C   sT   t | jds|  S zt| j| jW S  tk
rN   | j}td| dY nX d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 N)rj   r   r?   r   r'   r:   )r   r%   r   r   r   ro     s    zLocalProxy._get_current_objectc                 C   s
   t | jS r   )rZ   rF   r   r   r   r   <lambda>      zLocalProxy.<lambda>T)rb   r`   ra   c                 C   s   dt | j dS )NrX   z	 unbound>)rZ   r.   r   r   r   r   r     r   )r`   c                 C   s   dS )NFr   r   r   r   r   r     r   c                 C   s   g S r   r   r   r   r   r   r     r   c                 C   s   t | S r   )rZ   r   r   r   r   r     r   )r`   ra   c                 C   s
   t || S r   )rK   ru   r   r   r   r     r   c                 C   s
   t || S r   )
issubclassru   r   r   r   r     r   c                 O   s
   | ||S r   r   )r   rp   rq   r   r   r   r     r   )N)r.   r/   r0   rF   r1   r2   r]   rk   r6   r\   r7   r   ro   r^   reprr[   __str__rT   	__bytes__
__format__operatorlt__lt__le__le__eq__eq__ne__ne__gt__gt__ge__ge__hash__hash__rr   __bool__r?   r*   setattrr   delattrr-   dir__dir__ry   __instancecheck____subclasscheck__r#   rC   __len__length_hint__length_hint__getitem__getitem__setitem__setitem__delitem__delitem__r   r    next__next__reversed__reversed__contains__contains__add__add__sub__sub__mul__mul__matmul
__matmul__truediv__truediv__floordiv__floordiv__mod__mod__divmod
__divmod__pow__pow__lshift
__lshift__rshift
__rshift__and___and__xor__xor__or___or__r   __radd____rsub____rmul____rmatmul____rtruediv____rfloordiv____rmod____rdivmod____rpow____rlshift____rrshift____rand____rxor____ror__rs   iadd__iadd__isub__isub__imul__imul__imatmul__imatmul__itruediv__itruediv__	ifloordiv__ifloordiv__imod__imod__ipow__ipow__ilshift__ilshift__irshift__irshift__iand__iand__ixor__ixor__ior__ior__neg__neg__pos__pos__abs__abs__invert
__invert__complex__complex__r5   __int__float	__float__index	__index__round	__round__mathtrunc	__trunc__floor	__floor__ceil__ceil__	__enter____exit__	__await__	__aiter__	__anext__
__aenter__	__aexit__r,   __copy__deepcopy__deepcopy__r   r   r   r   r!   T  s   -    





 





































r!   )r,   r  r   typingr2   Zcontextvarsr   	functoolsr   r   Zwsgir   TYPE_CHECKINGZ_typeshed.wsgir   r	   r
   TypeVarrk   r6   r   r]   r   r   r   rI   r^   rs   r   r!   r   r   r   r   <module>   s(   %ICZ	