o
    2hcR                     @   s   d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	 ddl
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dd Ze e_dS )zoThis module implements decorators for implementing other decorators
as well as some commonly used decorators.

    N)partial)isclass	signature)LockRLock   )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__ K/var/www/html/chatgem/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!   *   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   1   r   z"_AdapterFunctionSurrogate.__init__c                 C   s   t | jj| jjS r   )r   __wrapped____code__r(   r   r   r   r   r,   5      z"_AdapterFunctionSurrogate.__code__c                 C   r   r   )r(   __defaults__r   r   r   r   r.   ;   r   z&_AdapterFunctionSurrogate.__defaults__c                 C   r   r   )r(   __kwdefaults__r   r   r   r   r/   ?   r   z(_AdapterFunctionSurrogate.__kwdefaults__c                 C   s   dt  vr	| jjS t| jS Nr   )globalsr(   __signature__r   r   r   r   r   r2   C   s   

z'_AdapterFunctionSurrogate.__signature__)
r"   r#   r$   r   r%   r,   r.   r/   r2   r&   r   r   r   r   r'   /   s    


r'   c                   @   s$   e Zd Zedd Zedd ZdS )_BoundAdapterWrapperc                 C   s   t | jj| jjS r   )r'   r+   __func___self_parentr(   r   r   r   r   r4   M   r-   z_BoundAdapterWrapper.__func__c                 C   s   dt  vr	| jjS t| jjS r0   )r1   r+   r2   r   r5   r(   r   r   r   r   r2   S   s   
z"_BoundAdapterWrapper.__signature__N)r"   r#   r$   r%   r4   r2   r   r   r   r   r3   K   s
    
r3   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 )AdapterWrapperc                    s8   | d}tt| j|i | t| j|| _|| _d S )Nr*   )popr   r6   r   r'   r+   _self_surrogater(   )r   argskwargsr*   r   r   r   r   _   s   

zAdapterWrapper.__init__c                 C   r   r   )r8   r,   r   r   r   r   r,   e   r   zAdapterWrapper.__code__c                 C   r   r   )r8   r.   r   r   r   r   r.   i   r   zAdapterWrapper.__defaults__c                 C   r   r   )r8   r/   r   r   r   r   r/   m   r   zAdapterWrapper.__kwdefaults__c                 C   r   r   )r8   r2   r   r   r   r   r2   q   r   zAdapterWrapper.__signature__)r"   r#   r$   r3   __bound_function_wrapper__r   r%   r,   r.   r/   r2   r&   r   r   r   r   r6   [   s    


r6   c                   @   s   e Zd Zdd ZdS )AdapterFactoryc                 C   s   t  r   )NotImplementedErrorr   r)   r   r   r   __call__w   s   zAdapterFactory.__call__N)r"   r#   r$   r?   r   r   r   r   r<   v   s    r<   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   rA   r   r   r   r   |   s   
z DelegatedAdapterFactory.__init__c                 C   s
   |  |S r   )rA   r>   r   r   r   r?      s   
z DelegatedAdapterFactory.__call__)r"   r#   r$   r   r?   r&   r   r   r   r   r@   {   s    r@   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)   wrapperrC   r*   )r)   rG   rC   )	
isinstancer<   callablestrlenr   exec__annotations__r6   )r)   rG   rC   r*   nsannotations)rD   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)rP   r*   rC   r:   r)   r   r   _capture   s   z-decorator.<locals>._wrapper.<locals>._capturer   )r   rQ   rR   __get__)r)   instancer9   r:   rV   rS   rT   rU   )rP   r*   rC   rG   )r:   r)   r   _wrapper   s"   zdecorator.<locals>._wrapper)r*   rB   )NN)	decoratorr   )rG   rC   r*   rD   rY   r   )rP   r*   rC   rD   rG   r   rZ      s   / 0rZ   c                    sz   t | dr#t | dr#| tfdd}G fdddt}||dS dd	   fd
d}G  fdddt}|| |dS )a  Depending on the nature of the `wrapped` object, will either return a
    decorator which can be used to wrap a function or method, or a context
    manager, both of which will act accordingly depending on how used, to
    synchronize access to calling of the wrapped function, or the block of
    code within the context manager. If it is an object which is a
    synchronization primitive, such as a threading Lock, RLock, Semaphore,
    Condition, or Event, then it is assumed that the object is to be used
    directly as the synchronization primitive, otherwise a lock is created
    automatically and attached to the wrapped object and used as the
    synchronization primitive.
    acquirereleasec                    s6     | |i |W  d    S 1 sw   Y  d S r   r   r)   rX   r9   r:   lockr   r   _synchronized  s   $z#synchronized.<locals>._synchronizedc                       s$   e Zd Z fddZ fddZdS )z'synchronized.<locals>._PartialDecoratorc                    s        S r   )r[   r   r^   r   r   	__enter__  s   z1synchronized.<locals>._PartialDecorator.__enter__c                    s       d S r   )r\   r   r9   r^   r   r   __exit__  s   z0synchronized.<locals>._PartialDecorator.__exit__Nr"   r#   r$   ra   rc   r   r^   r   r   _PartialDecorator  s    re   )r)   c                 S   s~   t | dd }|d u r=tj& t | dd }|d u r-t }t| d| W d    |S W d    |S 1 s8w   Y  |S )N_synchronized_lock)varsgetsynchronized_synchronized_meta_lockr   setattr)contextr_   r   r   r   rf     s   

z(synchronized.<locals>._synchronized_lockc                    sF    |d ur|n|  | |i |W  d    S 1 sw   Y  d S r   r   r]   rf   r   r   _synchronized_wrapper  s   $z+synchronized.<locals>._synchronized_wrapperc                       s    e Zd Z fddZdd ZdS )z%synchronized.<locals>._FinalDecoratorc                    s    | j | _| j  | jS r   )r+   
_self_lockr[   r   rm   r   r   ra     s   
z/synchronized.<locals>._FinalDecorator.__enter__c                 W   s   | j   d S r   )ro   r\   rb   r   r   r   rc     s   z.synchronized.<locals>._FinalDecorator.__exit__Nrd   r   rm   r   r   _FinalDecorator  s    rp   )r)   rG   )hasattrrZ   r	   r
   )r)   r`   re   rn   rp   r   )rf   r_   r   ri     s   
	 
ri   r   )__doc__sys	functoolsr   inspectr   r   	threadingr   r   	__wrapt__r   r	   r
   	argumentsr   r   r'   r3   r6   r<   r@   adapter_factoryrZ   ri   rj   r   r   r   r   <module>   s&    	
  j