o
    Y^FC                     @   s  d Z ddlmZ ddlZddlZddlZddlZddlZddlZdZ	ej
dkr2ddlmZ dd Zned	d
Zdd Zdd ZzejZW n eyR   dd ZY nw zddlmZ W n eyh   dd ZY nw edZG dd deZd*ddZd+ddZzddlmZ W n ey   ddlmZ Y nw G dd deZeejZeej Z!e!dkrej"sd d! Zee_ne!dkrej"rne!d"krd#d! Zee_eeZ#d$d% Z$d&d' Z%d(d) Z&dS ),zT
Decorator module, see http://pypi.python.org/pypi/decorator
for the documentation.
    )print_functionNz4.4.2)   )getfullargspecc                 C   s   | j S N)__init__cls r	   +/usr/lib/python3/dist-packages/decorator.pyget_init0   s   r   FullArgSpeczAargs varargs varkw defaults kwonlyargs kwonlydefaults annotationsc                 C   s   t t| g di f S )z?A quick and dirty replacement for getfullargspec for Python 2.XN)r   _makeinspectZ
getargspecfr	   r	   r
   r   7   s   r   c                 C   s   | j jS r   )r   __func__r   r	   r	   r
   r   ;   s   c                 C      dS NFr	   r   r	   r	   r
   iscoroutinefunctionB      r   )isgeneratorfunctionc                 C   r   r   r	   )callerr	   r	   r
   r   H   r   r   z\s*def\s*([_\w][_\w\d]*)\s*\(c                   @   sb   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.
    r	   Nc                 C   s  || _ |r|j| _| jdkrd| _|j| _|j| _t|rt	|}t
|di | _dD ]}	t| |	t
||	 q+t| jD ]\}
}t| d|
 | q<t| j}t| j}| jrg|d| j  |d| j  n| jro|d | jD ]}	|d|	  |d|	|	f  qr| jr|d	| j  |d	| j  d
|| _d
|| _ |j | _|r|| _|d ur|| _|r|| _|r|| _|r|| _|r|| _t| dsJ t| dstd| d S )N<lambda>_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__moduler   
isfunctionr   getattrannotationssetattr	enumerater   listr   appendr    r   joinr%   __dict__copydictr   hasattr	TypeError)selffuncr$   r%   r   r)   r+   ZfuncdictZargspecaiargZallargsZallshortargsr	   r	   r
   r   ]   s\   






zFunctionMaker.__init__c                 K   s   | j |_t| dd|_t| di |_| j|_| jpd|_t| dd|_	zt
d}W n ty5   d}Y nw |jdd}t| d||_|j| dS )	z2Update the signature of func with the data in selfr)   Nr6   r.   r   ?r'   r+   )r$   r'   r-   r(   r4   r   __defaults__r!   __kwdefaults__r   sys	_getframeAttributeError	f_globalsgetr*   update)r9   r:   kwframeZcallermoduler	   r	   r
   rF      s   zFunctionMaker.updateFc                 K   s  |t |  }|p	i }t|}|du rtd| |d}t|gdd | jdD  }|D ]}	|	dv r>td|	|f q0|	d	sH|d	7 }d
t
| j }
zt||
d}t|| W n tyr   tdtjd t|tjd  w || }|r}||d< | j|fi | |S )zBMake a new function from a given template and update the signatureNz 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printrA   stderrrF   )r9   Z	src_templevaldict	addsourceattrssrcmor$   namesnfilenamecoder:   r	   r	   r
   make   s:   




zFunctionMaker.makeTc                 K   s   t |tr| dd\}	}
|
dd }d}nd}	d}|}| ||	||||}ddd | D }|d}|rGt|rGd	| d
d}nd| }|j	|||fi |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.
        (rI   NrQ   c                 s   s    | ]}d | V  qdS )z    Nr	   )rL   liner	   r	   r
   	<genexpr>       z'FunctionMaker.create.<locals>.<genexpr>rP   z#async def %(name)s(%(signature)s):
returnzreturn awaitzdef %(name)s(%(signature)s):
)

isinstancestrrJ   rY   r3   
splitlinesrE   r   replacerl   )r   objbodyrc   r   r)   r+   rd   re   r$   restr%   r:   r9   Zibodyr   r	   r	   r
   create   s    
	
zFunctionMaker.create)NNNNNNNr   )NNNT)r'   r*   __qualname__r(   	itertoolscountr]   r   r   r   r   r    r!   r   rF   rl   classmethodrz   r	   r	   r	   r
   r   P   s    
4
"r   r	   c           
      C   s   t || d}d}t|D ]\}}d| }|||< ||d 7 }qdtj  kr*dk r6n n
t|o4t| }nt|}|rHtj| d| || d}	ntj| d	| || d}	t| d
r\| j	|	_	|	S )z
    decorate(func, caller) decorates a function using a caller.
    If the caller is a generator function, the resulting function
    will be a generator function.
    )rP   rO    z_e%d_r#   z3.5z3.6z?for res in _call_(_func_, %s%%(shortsignature)s):
    yield res__wrapped__z,return _call_(_func_, %s%%(shortsignature)s)r{   )
r6   r0   rA   versionr   r   r   rz   r7   r{   )
r:   r   extrasrc   esr<   ZextraexZgeneratorcallerZfunr	   r	   r
   decorate   s6   
r   c           
      C   s  |dur	t || S d\}}t| r | j }d| j| jf }n?t| rU| jdkr-d}n| j}| j}| jj}t	| j
p<d}d| jj|| | }|rQ|d7 }| j
}n
| jj }| jj}t| t d	}tjd
||f d||f ||| j| d}	|rd| |	_
|	S )z=decorator(caller) converts a caller function into a decoratorN)r   r	   zHdecorator(%s) converts functions/generators into factories of %s objectsr   r   r	   r#   rN   )Z_callZ
_decorate_z%s(func, %s)zhif func is None: return lambda func:  _decorate_(func, _call, (%s))
return _decorate_(func, _call, (%s)))r)   r+   r   r   )r   r   Zisclassr'   lowerr,   r(   __code__co_argcountlenr?   r3   co_varnames	__class____call__r6   r   rz   r*   )
r   Z_funcZdefaultargsr   r$   r)   nargsZndefsrc   decr	   r	   r
   	decorator  s@   








r   )_GeneratorContextManager)GeneratorContextManagerc                   @   s   e Zd Zdd ZdS )ContextManagerc                 C   s   t j|dt| |d|dS )zContext manager decoratorz.with _self_: return _func_(%(shortsignature)s))Z_self_rO   r   )r   rz   r6   )r9   r:   r	   r	   r
   r   0  s   zContextManager.__call__N)r'   r*   r{   r   r	   r	   r	   r
   r   /  s    r      c                 O   s   t | ||i |S r   r   r   r9   gr;   kr	   r	   r
   r   :  s   r      c                 O   s   t | |||S r   r   r   r	   r	   r
   r   @  s   c                 C   s   t | S r   )_contextmanager)r:   r	   r	   r
   contextmanagerG  s   r   c                 C   sT   d}t |D ]\}}t|| rd} nt| |r| ||< d}q|r(||  dS dS )z_
    Append ``a`` to the list of the virtual ancestors, unless it is already
    included.
    TFN)r0   
issubclassr2   )r;   
vancestorsaddjvar	   r	   r
   r2   N  s   

r2   c                     sL   sJ 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#   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   r8   )Z	argumentsZwrongmsg)dispatch_argsr	   r
   checkh  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||r.|| vr.t|| qq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	   )rL   _r	   r	   r
   rM   }  s    zIdispatch_on.<locals>.gen_func_dec.<locals>.vancestors.<locals>.<listcomp>c                 S   s   g | ]}t |qS r	   )rX   )rL   rar	   r	   r
   rM     s    )ranger   zipr   mror2   )typesZrastypes_tZtype_r   )r   r   typemapr	   r
   r   x  s   
z5dispatch_on.<locals>.gen_func_dec.<locals>.vancestorsc                     s    |  g }t | |  D ]7\}}t|}|dkr!td||f |dkr7|\}td||fi  dd }n| }||dd  q|S )zG
            Get a list of virtual MROs, one for each type
            rI   zAmbiguous dispatch for %s: %sr   Nrn   )r   r   RuntimeErrortyper   r2   )r   Z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 r   )r'   )rL   r;   r	   r	   r
   rp     s    zKdispatch_on.<locals>.gen_func_dec.<locals>.dispatch_info.<locals>.<genexpr>)r|   productr2   tuple)r   Z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y   Y nw ||i |S tj |  }t| |D ]}|}|d urA||i |  S q-|i |S )Nc                 s   s    | ]}t |V  qd S r   )r   rK   r	   r	   r
   rp     rq   zGdispatch_on.<locals>.gen_func_dec.<locals>._dispatch.<locals>.<genexpr>)r   KeyErrorr|   r   r\   rE   )r   r   rG   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   defaultr   r   r   r   r   )rX   r   r   rZ   r   rz   r6   )r:   Zargsetr   r   r   r   r   Zdispatch_str)r   r:   r   r   r
   gen_func_decn  s   
z!dispatch_on.<locals>.gen_func_decdispatch_on)r3   r   ner'   )r   r   r	   r   r
   r   `  s   
Wr   )r	   r   )'r(   Z
__future__r   rerA   r   r   r|   collections__version__version_infor   r   
namedtupler   r   rC   r   ImportErrorr^   rT   objectr   r   r   
contextlibr   r   r   r   Zinitr   r   Zn_argsr   r   r   r2   r   r	   r	   r	   r
   <module>   sh   



 

"'

