U
    9%epA                     @   s   d Z ddlZ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m	Z	m
Z
 dZedZejjZejjZG dd deZd	d
 ZdddZdd ZdddZG dd deZeeZdd Zdd Zdd ZdS )zv
Decorator module, see
https://github.com/micheles/decorator/blob/master/docs/documentation.md
for the documentation.
    N)_GeneratorContextManager)getfullargspeciscoroutinefunctionisgeneratorfunctionz5.1.1z\s*def\s*([_\w][_\w\d]*)\s*\(c                   @   sZ   e Zd ZdZe Zd Z Z Z	 Z
 ZZdddZdd Zdd	d
ZedddZdS )FunctionMakerz
    An object with the ability to create functions with a given signature.
    It has attributes name, doc, module, signature, defaults, dict and
    methods update and make.
     Nc                 C   s  || _ |rZ|j| _| jdkr$d| _|j| _|j| _t|rZt	|}t
|di | _dD ]}	t| |	t
||	 qZt| jD ]\}
}t| d|
 | q|t| j}t| j}| jr|d| j  |d| j  n| jr|d | jD ]$}	|d|	  |d|	|	f  q| jr6|d	| j  |d	| j  d
|| _d
|| _ |j | _|rf|| _|d k	rv|| _|r|| _|r|| _|r|| _|r|| _t| dstt| dstd| d S )Nz<lambda>Z_lambda___annotations__)argsvarargsvarkwdefaults
kwonlyargskwonlydefaultszarg%d*z%s=Nonez%s=%sz**, name	signaturez%You are decorating a non function: %s)shortsignature__name__r   __doc__doc
__module__moduleinspect	isroutiner   getattrannotationssetattr	enumerater	   listr
   appendr   r   joinr   __dict__copydictr   hasattrAssertionError	TypeError)selffuncr   r   r   r   r   ZfuncdictZargspecaiargZallargsZallshortargsr   r   H/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/decorator.py__init__@   sZ    





zFunctionMaker.__init__c                 K   s   | j |_t| dd|_t| di |_| j|_| jp4d|_t| dd|_	zt
d}W n tk
rl   d}Y nX |jdd}t| d||_|j| dS )	zD
        Update the signature of func with the data in self
        r   Nr$   r      ?r   r   )r   r   r   r   r"   r   __defaults__r   __kwdefaults__r   sys	_getframeAttributeError	f_globalsgetr   update)r(   r)   kwframeZcallermoduler   r   r-   r8   t   s    
zFunctionMaker.updateFc                 K   s  |t |  }|pi }t|}|dkr2td| |d}t|gdd | jdD  }|D ]}	|	dkr`td|	|f q`|	d	s|d	7 }d
t
| j }
zt||
d}t|| W n2 tk
r   tdtjd t|tjd  Y nX || }|r||d< | j|f| |S )zT
        Make a new function from a given template and update the signature
        Nz not a valid function template
%s   c                 S   s   g | ]}| d qS )z *)strip.0r,   r   r   r-   
<listcomp>   s     z&FunctionMaker.make.<locals>.<listcomp>,)_func__call_z%s is overridden in
%s
z<decorator-gen-%d>ZsinglezError in generated code:)fileZ
__source__)varsDEFsearchSyntaxErrorgroupsetr   split	NameErrorendswithnext_compile_countcompileexec	Exceptionprintr3   stderrr8   )r(   Z	src_templevaldict	addsourceattrssrcmor   namesnfilenamecoder)   r   r   r-   make   s6    




zFunctionMaker.makeTc                 K   s   t |tr0| dd\}	}
|
dd }d}nd}	d}|}| ||	||||}ddd | D }|d}|rt|rd	| d
d}nd| }|j	|||f|S )z
        Create a function from the strings name, signature and body.
        evaldict is the evaluation dictionary. If addsource is true an
        attribute __source__ is added to the result. The attributes attrs
        are added, if any.
        (r;   NrC   c                 s   s   | ]}d | V  qdS )z    Nr   )r>   liner   r   r-   	<genexpr>   s     z'FunctionMaker.create.<locals>.<genexpr>rB   z#async def %(name)s(%(signature)s):
returnzreturn awaitzdef %(name)s(%(signature)s):
)

isinstancestrr<   rK   r!   
splitlinesr7   r   replacer^   )clsobjbodyrU   r   r   r   rV   rW   r   restr   r)   r(   Zibodycallerr   r   r-   create   s"    	

 zFunctionMaker.create)NNNNNNN)NF)NNNT)r   r   __qualname__r   	itertoolscountrO   r	   r
   r   r   r   r   r.   r8   r^   classmethodrm   r   r   r   r-   r   3   s"           
4
$      r   c                 C   s    |j | |}|  |j|jfS )zA
    Fix args and kwargs to be consistent with the signature
    )bindapply_defaultsr	   kwargs)r	   rt   sigbar   r   r-   fix   s    rw   r   Fc                    s6  t t r( fdd}n2t rF fdd}n fdd}j|_j|_|_|_j|_zj	|_	W n t
k
r   Y nX zj|_W n t
k
r   Y nX zj|_W n t
k
r   Y nX zj|_W n t
k
r   Y nX z|jj W n t
k
r0   Y nX |S )af  
    Decorates a function/generator/coroutine using a caller.
    If kwsyntax is True calling the decorated functions with keyword
    syntax will pass the named arguments inside the ``kw`` dictionary,
    even if such argument are positional, similarly to what functools.wraps
    does. By default kwsyntax is False and the the arguments are untouched.
    c                     s.   st | |\} } f|  |I d H S Nrw   r	   r9   rl   extrasr)   kwsyntaxru   r   r-   fun   s    zdecorate.<locals>.func                  ?   s8   st | |\} } f|  |D ]
}|V  q(d S rx   ry   )r	   r9   resr{   r   r-   r~      s    c                     s(   st | |\} } f|  |S rx   ry   rz   r{   r   r-   r~      s    )r   r   r   r   r   r   __wrapped____signature__rn   r1   r5   r2   r   r   r"   r8   )r)   rl   r|   r}   r~   r   r{   r-   decorate   s@    
r   c                    s    fdd}|S )z
    A version of "decorator" implemented via "exec" and not via the
    Signature object. Use this if you are want to preserve the `.__code__`
    object properties (https://github.com/micheles/decorator/issues/129).
    c                    s   t j| dt | d| | jdS )Nz)return _call_(_func_, %(shortsignature)s))rB   rA   )r   rn   )r   rm   r$   rn   r)   rl   r   r-   dec  s    
 zdecoratorx.<locals>.decr   )rl   r   r   r   r-   
decoratorx  s    r   c                    s   |dk	rt | dS t }dd |j D d	 fdd	}|jd|_ j|_ j|_ |_	 j
|_
t dd|_|j j |S )
zG
    decorator(caller) converts a caller function into a decorator
    Nr   c                 S   s   g | ]}|j tkr|qS r   )kindPOSr>   pr   r   r-   r?     s     
 zdecorator.<locals>.<listcomp>c                    sX   t |d }|tfdd|d  D   | d krF fddS t|  S d S )Nr;   c                 3   s(   | ] }|j tk	r |j|j V  qd S rx   )defaultEMPTYr7   r   r   )r9   r   r-   rb   "  s   
z)decorator.<locals>.dec.<locals>.<genexpr>c                    s   t |  S rx   r   r   )rl   r|   r}   r   r-   <lambda>&      z(decorator.<locals>.dec.<locals>.<lambda>)lentupler   )r)   r	   r9   narl   Z
dec_paramsr}   )r|   r9   r-   r      s    

zdecorator.<locals>.dec)
parametersr2   )N)r   r   r   r   valuesrg   r   r   r   r   rn   r   r2   r"   r8   )rl   Z_funcr}   ru   r   r   r   r-   	decorator  s    
	r   c                   @   s   e Zd Zdd Zdd ZdS )ContextManagerc                 O   s   t | ||| d S rx   )r   r.   )r(   gr*   kr   r   r-   r.   7  s    zContextManager.__init__c                    s    fdd}t ||S )Nc              
      s:    j  jf j j | ||W  5 Q R  S Q R X d S rx   )	__class__r)   r	   kwds)fr*   r   r(   r   r-   rl   ;  s    z'ContextManager.__call__.<locals>.callerr   )r(   r)   rl   r   r   r-   __call__:  s    zContextManager.__call__N)r   r   rn   r.   r   r   r   r   r-   r   6  s   r   c                 C   s   t | S rx   )_contextmanagerr   r   r   r-   contextmanagerD  s    r   c                 C   sP   d}t |D ]0\}}t|| r&d} q>t| |r| ||< d}q|rL||  dS )z_
    Append ``a`` to the list of the virtual ancestors, unless it is already
    included.
    TFN)r   
issubclassr    )r*   
vancestorsaddjvar   r   r-   r    K  s    

r    c                     sL   st ddd tjdffdd	  fdd}d	 |_|S )
zr
    Factory of decorators turning a function into a generic function
    dispatching on the given arguments.
    zNo dispatch args passedz(%s,)r    c                    s0   |t | t  r,tdt  t | |f dS )z5Make sure one passes the expected number of argumentszExpected %d arguments, got %d%sN)r   r'   )	argumentsZwrongmsg)dispatch_argsr   r-   checke  s    zdispatch_on.<locals>.checkc                    s   t tj}t |ks&td i fddfdd fdd} fdd	} fd
d}tjd t|d| |d
S )z4Decorator turning a function into a generic functionzUnknown dispatch arguments %sc                     sn    |  dd t tD }D ]<}t| ||D ]*\}}}t||r2|| kr2t|| q2q"dd |D S )zU
            Get a list of sets of virtual ancestors for the given types
            c                 S   s   g | ]}g qS r   r   )r>   _r   r   r-   r?   z  s     zIdispatch_on.<locals>.gen_func_dec.<locals>.vancestors.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )rJ   )r>   rar   r   r-   r?     s     )ranger   zipr   mror    )typesZrastypes_ttype_r   )r   r   typemapr   r-   r   u  s    z5dispatch_on.<locals>.gen_func_dec.<locals>.vancestorsc                     s    |  g }t | |  D ]p\}}t|}|dkrDtd||f n4|dkrp|\}td||fi  dd }n| }||dd  q|S )zG
            Get a list of virtual MROs, one for each type
            r;   zAmbiguous dispatch for %s: %sr   Nr`   )r   r   RuntimeErrortyper   r    )r   listsr   ZvasZn_vasr   r   )r   r   r   r-   	ancestors  s    
z4dispatch_on.<locals>.gen_func_dec.<locals>.ancestorsc                     s      fdd}|S )zU
            Decorator to register an implementation for the given types
            c                    s&    t | jtjd| j  | < | S )Nz in )r   r	   operatorltr   )r   )r   r   r   r   r-   r     s    z@dispatch_on.<locals>.gen_func_dec.<locals>.register.<locals>.decr   )r   r   )r   r   )r   r-   register  s    z3dispatch_on.<locals>.gen_func_dec.<locals>.registerc                     s<   |  g }t j |   D ]}|tdd |D  q|S )zI
            An utility to introspect the dispatch algorithm
            c                 s   s   | ]}|j V  qd S rx   )r   )r>   r*   r   r   r-   rb     s     zKdispatch_on.<locals>.gen_func_dec.<locals>.dispatch_info.<locals>.<genexpr>)ro   productr    r   )r   lstZanc)r   r   r   r-   dispatch_info  s
    z8dispatch_on.<locals>.gen_func_dec.<locals>.dispatch_infoc                    s   t dd | D }z| }W n tk
r2   Y nX |||S tj |  }t| |D ]$}|}|d k	rX|||  S qX||S )Nc                 s   s   | ]}t |V  qd S rx   )r   r=   r   r   r-   rb     s     zGdispatch_on.<locals>.gen_func_dec.<locals>._dispatch.<locals>.<genexpr>)r   KeyErrorro   r   rN   r7   )r   r	   r9   r   r   combinationsr   )r   r)   r   r   r-   	_dispatch  s    

z4dispatch_on.<locals>.gen_func_dec.<locals>._dispatchz#return _f_(%s, %%(shortsignature)s))Z_f_)r   r   r   r   r   r   r   )rJ   r   r	   rL   r   rm   r$   )r)   Zargsetr   r   r   r   r   Zdispatch_str)r   r)   r   r   r-   gen_func_deck  s*    
      z!dispatch_on.<locals>.gen_func_decdispatch_on)r&   r!   r   ner   )r   r   r   r   r-   r   ]  s    W
r   )r   F)NF)r   rer3   r   r   ro   
contextlibr   r   r   r   __version__rP   rF   	ParameterPOSITIONAL_OR_KEYWORDr   emptyr   objectr   rw   r   r   r   r   r   r   r    r   r   r   r   r-   <module>   s,   
 	
6
!