o
    ¯µjÒI  ã                   @   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                    ó   t t| ƒ |¡ || _d S ©N)Úsuperr   Ú__init__Ú_self_adapter_code)ÚselfÚwrapped_codeÚadapter_code©Ú	__class__© úE/var/www/ai-app/venv/lib/python3.10/site-packages/wrapt/decorators.pyr      ó   
z_AdapterFunctionCode.__init__c                 C   ó   | j jS r   )r   Úco_argcount©r   r   r   r   r       ó   z _AdapterFunctionCode.co_argcountc                 C   r   r   )r   Úco_coder   r   r   r   r   $   r   z_AdapterFunctionCode.co_codec                 C   r   r   )r   Úco_flagsr   r   r   r   r   (   r   z_AdapterFunctionCode.co_flagsc                 C   r   r   )r   Úco_kwonlyargcountr   r   r   r   r   ,   r   z&_AdapterFunctionCode.co_kwonlyargcountc                 C   r   r   )r   Úco_varnamesr   r   r   r   r    0   r   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                    r   r   )r   r&   r   Ú_self_adapter)r   ÚwrappedÚadapterr   r   r   r   7   r   z"_AdapterFunctionSurrogate.__init__c                 C   s   t | jj| jjƒS r   )r   Ú__wrapped__Ú__code__r'   r   r   r   r   r+   >   ó   ÿz"_AdapterFunctionSurrogate.__code__c                 C   r   r   )r'   Ú__defaults__r   r   r   r   r-   D   r   z&_AdapterFunctionSurrogate.__defaults__c                 C   r   r   )r'   Ú__kwdefaults__r   r   r   r   r.   H   r   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   r1   S   r,   z%_BoundAdapterFunctionWrapper.__func__c                 C   s   t | jjƒS r   )r   r2   r'   r   r   r   r   r/   Y   s   z*_BoundAdapterFunctionWrapper.__signature__N)r!   r"   r#   r$   r1   r/   r   r   r   r   r0   Q   s
    
r0   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   r3   r   r&   r*   Ú_self_surrogater'   )r   ÚargsÚkwargsr)   r   r   r   r   b   s   

z _AdapterFunctionWrapper.__init__c                 C   r   r   )r5   r+   r   r   r   r   r+   k   r   z _AdapterFunctionWrapper.__code__c                 C   r   r   )r5   r-   r   r   r   r   r-   o   r   z$_AdapterFunctionWrapper.__defaults__c                 C   r   r   )r5   r.   r   r   r   r   r.   s   r   z&_AdapterFunctionWrapper.__kwdefaults__c                 C   r   r   )r5   r/   r   r   r   r   r/   w   r   z%_AdapterFunctionWrapper.__signature__)r!   r"   r#   r0   Ú__bound_function_wrapper__r   r$   r+   r-   r.   r/   r%   r   r   r   r   r3   ^   s    	


r3   c                   @   s   e Zd Zdd„ ZdS )ÚAdapterFactoryc                 C   s   t ƒ ‚r   )ÚNotImplementedError©r   r(   r   r   r   Ú__call__}   s   zAdapterFactory.__call__N)r!   r"   r#   r<   r   r   r   r   r9   |   s    r9   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )Ú_DelegatedAdapterFactoryc                    s   t t| ƒ ¡  || _d S r   )r   r=   r   Úfactory)r   r>   r   r   r   r   ‚   s   
z!_DelegatedAdapterFactory.__init__c                 C   s
   |   |¡S r   )r>   r;   r   r   r   r<   ‡   s   
z!_DelegatedAdapterFactory.__call__)r!   r"   r#   r   r<   r%   r   r   r   r   r=      s    r=   N©Úenabledr)   Úproxyc                  sF   ˆdurd‡fdd„	‰ ‡ ‡‡‡fdd„}ˆ ˆ|t dS tt ˆˆˆ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œ   |rGt |tƒr|| ƒ}t|ƒs?i }i }t |tƒs,t|ƒdkr(|d }|d d… }t|Ž }td|› d||ƒ |d }|r?||_t| |||dS ˆ | ||dS )Né   éÿÿÿÿzdef adapterz: passr)   )r(   Úwrapperr@   r)   )r(   rD   r@   )	Ú
isinstancer9   ÚcallableÚstrÚlenr
   ÚexecÚ__annotations__r3   )r(   rD   r@   r)   ÚnsÚannotations)rA   r   r   Ú_build³   s&   


ÿzdecorator.<locals>._buildc                    s¢   |d u rt ˆƒr|s‡‡‡‡ ‡fdd„}|S |d }ˆ}t|ƒtu r(|s&|S d }|d u r7t ˆƒr4ˆƒ }nˆ}nt |ƒrBˆ d |¡}nˆ |t|ƒ¡}ˆ|||ˆƒS )Nc                    s8   ˆ}t |ƒtu r|s| S d }ˆdi ˆ¤Ž}ˆ | ||ˆƒS )Nr   )ÚtypeÚbool)Útarget_wrappedÚ_enabledÚtarget_wrapper)rM   r)   r@   r7   r(   r   r   Ú_captureü   s   z-decorator.<locals>._wrapper.<locals>._capturer   )r   rN   rO   Ú__get__)r(   Úinstancer6   r7   rS   rP   rQ   rR   )rM   r)   r@   rD   )r7   r(   r   Ú_wrapperâ   s"   zdecorator.<locals>._wrapper)r)   r?   )NN)Ú	decoratorr   )rD   r@   r)   rA   rV   r   )rM   r)   r@   rA   rD   r   rW   •   s   / 0rW   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   rZ   r   r   r   r   ´  s   
z_StateBindingWrapper.__init__c                 C   s
   || _ | S r   )r*   )r   Úwrapper_factoryr   r   r   r<   ¼  s   z_StateBindingWrapper.__call__c                    s4   ˆ d u r| S | j  ˆ |¡‰| j‰‡ ‡‡fdd„}|S )Nc                    s   ˆ| ƒ}|  ˆˆ ¡ |S r   )Ú__self_setattr__)ÚfuncrD   ©rU   rZ   r\   r   r   Ú_bindÓ  s   z+_StateBindingWrapper.__get__.<locals>._bind)r*   rT   r[   )r   rU   Úownerr`   r   r_   r   rT   Ä  s   
z_StateBindingWrapper.__get__)r!   r"   r#   Ú__doc__r   r<   rT   r%   r   r   r   r   rX   ¦  s
    rX   r   )rb   Ú	functoolsr   Úinspectr   r   Ú	__wrapt__r   r   r   r	   Ú	argumentsr
   r   r&   r0   r3   r9   r=   Úadapter_factoryrW   rX   Úbind_state_to_wrapperr   r   r   r   Ú<module>   s"    

  :