a
    `*jI                     @   s   d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZ G dd de	ZG d	d
 d
e	ZG dd deZG dd de
ZG dd dZG dd deZeZddde
dddZG dd deZeZdS )zoThis module implements decorators for implementing other decorators
as well as some commonly used decorators.

    )partial)isclass	signature   )BaseObjectProxyBoundFunctionWrapperCallableObjectProxyFunctionWrapper)formatargspecc                       sX   e Zd Z fddZedd Zedd Zedd Zed	d
 Zedd Z	  Z
S )_AdapterFunctionCodec                    s   t t| | || _d S N)superr   __init___self_adapter_code)selfZwrapped_codeZadapter_code	__class__ L/var/www/html/assistant/venv/lib/python3.9/site-packages/wrapt/decorators.pyr      s    z_AdapterFunctionCode.__init__c                 C   s   | j jS r   )r   co_argcountr   r   r   r   r       s    z _AdapterFunctionCode.co_argcountc                 C   s   | j jS r   )r   co_coder   r   r   r   r   $   s    z_AdapterFunctionCode.co_codec                 C   s   | j jS r   )r   co_flagsr   r   r   r   r   (   s    z_AdapterFunctionCode.co_flagsc                 C   s   | j jS r   )r   co_kwonlyargcountr   r   r   r   r   ,   s    z&_AdapterFunctionCode.co_kwonlyargcountc                 C   s   | j jS r   )r   co_varnamesr   r   r   r   r   0   s    z _AdapterFunctionCode.co_varnames)__name__
__module____qualname__r   propertyr   r   r   r   r   __classcell__r   r   r   r   r      s   



r   c                       sL   e Zd Z fddZedd Zedd Zedd Zed	d
 Z  Z	S )_AdapterFunctionSurrogatec                    s   t t| | || _d S r   )r   r    r   _self_adapter)r   wrappedadapterr   r   r   r   7   s    z"_AdapterFunctionSurrogate.__init__c                 C   s   t | jj| jjS r   )r   __wrapped____code__r!   r   r   r   r   r%   >   s    z"_AdapterFunctionSurrogate.__code__c                 C   s   | j jS r   )r!   __defaults__r   r   r   r   r&   D   s    z&_AdapterFunctionSurrogate.__defaults__c                 C   s   | j jS r   )r!   __kwdefaults__r   r   r   r   r'   H   s    z(_AdapterFunctionSurrogate.__kwdefaults__c                 C   s
   t | jS r   )r   r!   r   r   r   r   __signature__L   s    z'_AdapterFunctionSurrogate.__signature__)
r   r   r   r   r   r%   r&   r'   r(   r   r   r   r   r   r    5   s   


r    c                   @   s$   e Zd Zedd Zedd ZdS )_BoundAdapterFunctionWrapperc                 C   s   t | jj| jjS r   )r    r$   __func___self_parentr!   r   r   r   r   r*   S   s    z%_BoundAdapterFunctionWrapper.__func__c                 C   s   t | jjS r   )r   r+   r!   r   r   r   r   r(   Y   s    z*_BoundAdapterFunctionWrapper.__signature__N)r   r   r   r   r*   r(   r   r   r   r   r)   Q   s   
r)   c                       sP   e Zd ZeZ fddZedd Zedd Zedd Z	ed	d
 Z
  ZS )_AdapterFunctionWrapperc                    s8   | d}tt| j|i | t| j|| _|| _d S )Nr#   )popr   r,   r   r    r$   _self_surrogater!   )r   argskwargsr#   r   r   r   r   b   s    
z _AdapterFunctionWrapper.__init__c                 C   s   | j jS r   )r.   r%   r   r   r   r   r%   k   s    z _AdapterFunctionWrapper.__code__c                 C   s   | j jS r   )r.   r&   r   r   r   r   r&   o   s    z$_AdapterFunctionWrapper.__defaults__c                 C   s   | j jS r   )r.   r'   r   r   r   r   r'   s   s    z&_AdapterFunctionWrapper.__kwdefaults__c                 C   s   | j jS r   )r.   r(   r   r   r   r   r(   w   s    z%_AdapterFunctionWrapper.__signature__)r   r   r   r)   Z__bound_function_wrapper__r   r   r%   r&   r'   r(   r   r   r   r   r   r,   ^   s   	


r,   c                   @   s   e Zd Zdd ZdS )AdapterFactoryc                 C   s
   t  d S r   )NotImplementedErrorr   r"   r   r   r   __call__}   s    zAdapterFactory.__call__N)r   r   r   r4   r   r   r   r   r1   |   s   r1   c                       s$   e Zd Z fddZdd Z  ZS )_DelegatedAdapterFactoryc                    s   t t|   || _d S r   )r   r5   r   factory)r   r6   r   r   r   r      s    z!_DelegatedAdapterFactory.__init__c                 C   s
   |  |S r   )r6   r3   r   r   r   r4      s    z!_DelegatedAdapterFactory.__call__)r   r   r   r   r4   r   r   r   r   r   r5      s   r5   Nenabledr#   proxyc                  sJ   dur6dfdd	  fdd} |t dS tt dS dS )	a  
    The decorator should be supplied with a single positional argument
    which is the `wrapper` function to be used to implement the
    decorator. This may be preceded by a step whereby the keyword
    arguments are supplied to customise the behaviour of the
    decorator. The `adapter` argument is used to optionally denote a
    separate function which is notionally used by an adapter
    decorator. In that case parts of the function `__code__` and
    `__defaults__` attributes are used from the adapter function
    rather than those of the wrapped function. This allows for the
    argument specification from `inspect.getfullargspec()` and similar
    functions to be overridden with a prototype for a different
    function than what was wrapped. The `enabled` argument provides a
    way to enable/disable the use of the decorator. If the type of
    `enabled` is a boolean, then it is evaluated immediately and the
    wrapper not even applied if it is `False`. If not a boolean, it will
    be evaluated when the wrapper is called for an unbound wrapper,
    and when binding occurs for a bound wrapper. When being evaluated,
    if `enabled` is callable it will be called to obtain the value to
    be checked. If `False`, the wrapper will not be called and instead
    the original wrapped function will be called directly instead.
    The `proxy` argument provides a way of passing a custom version of
    the `FunctionWrapper` class used in decorating the function.
    Nc                    s   |rt |tr|| }t|s~i }i }t |tsXt|dkrP|d }|d d }t| }td| d|| |d }|r~||_t| |||dS  | ||dS )N   zdef adapterz: passr#   )r"   wrapperr8   r#   )r"   r<   r8   )	
isinstancer1   callablestrlenr
   exec__annotations__r,   )r"   r<   r8   r#   nsannotations)r9   r   r   _build   s&    


zdecorator.<locals>._buildc                    s   |d u r,t r,|s, fdd}|S |d }}t|tu rP|sL|S d }|d u rnt rh }q}n&t |rd |}n|t|}|||S )Nc                    s8   }t |tu r|s| S d }f i } | ||S r   )typebool)target_wrapped_enabledtarget_wrapper)rE   r#   r8   r0   r"   r   r   _capture   s    z-decorator.<locals>._wrapper.<locals>._capturer   )r   rF   rG   __get__)r"   instancer/   r0   rK   rH   rI   rJ   )rE   r#   r8   r<   )r0   r"   r   _wrapper   s"    zdecorator.<locals>._wrapper)r#   r7   )NN)	decoratorr   )r<   r8   r#   r9   rN   r   )rE   r#   r8   r9   r<   r   rO      s    / 0rO   c                       s6   e Zd ZdZdd fdd
Zdd Zdd	 Z  ZS )
_StateBindingWrappera<  A descriptor decorator that binds the owner instance to wrappers
    produced by a wrapper factory method.

    When applied on top of a method decorated with ``function_wrapper``
    or ``decorator``, it intercepts the descriptor ``__get__`` so that
    when the method is accessed through an instance, the owner instance
    is automatically attached as an attribute on the resulting wrapper
    via ``__self_setattr__``.

    The *name* keyword argument controls the attribute name under which
    the owner instance is stored on the wrapper (default ``"state"``).
    state)namec                   s   t  d  || _d S r   )r   r   
_self_name)r   rR   r   r   r   r     s    z_StateBindingWrapper.__init__c                 C   s
   || _ | S r   )r$   )r   wrapper_factoryr   r   r   r4     s    z_StateBindingWrapper.__call__c                    s4    d u r| S | j  || j fdd}|S )Nc                    s   | }|   |S r   )Z__self_setattr__)funcr<   rM   rR   rT   r   r   _bind  s    z+_StateBindingWrapper.__get__.<locals>._bind)r$   rL   rS   )r   rM   ownerrW   r   rV   r   rL     s    
z_StateBindingWrapper.__get__)r   r   r   __doc__r   r4   rL   r   r   r   r   r   rP     s   rP   )N)rY   	functoolsr   inspectr   r   Z	__wrapt__r   r   r   r	   	argumentsr
   r   r    r)   r,   r1   r5   Zadapter_factoryrO   rP   Zbind_state_to_wrapperr   r   r   r   <module>   s    

  :