???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3
5*[Z@                 @   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rdddlmZ dd Znejd	d
Zdd Zdd Zy
ejZW n ek
r   dd ZY nX ejddZdd ZejdZG dd deZf fddZd*ddZyddlmZ W n" ek
r   ddlmZ Y nX G dd deZeejZe ej!Z"e"dkrhej# rhd d! Zee_n,e"dkr|ej#r|n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.2.13)getfullargspecc             C   s   | j S )N)__init__)cls r   /usr/lib/python3.6/decorator.pyget_init0   s    r	   FullArgSpecz5args varargs varkw defaults kwonlyargs kwonlydefaultsc             C   s   t jtj| g df S )z?A quick and dirty replacement for getfullargspec for Python 2.XN)r
   _makeinspect
getargspec)fr   r   r   r   7   s    r   c             C   s   | j jS )N)r   __func__)r   r   r   r   r	   ;   s    c             C   s   dS )NFr   )r   r   r   r   iscoroutinefunctionB   s    r   ArgSpeczargs varargs varkw defaultsc             C   s   t | }t|j|j|j|jS )z$A replacement for inspect.getargspec)r   r   argsvarargsvarkwdefaults)r   specr   r   r   r   J   s    r   z\s*def\s*([_\w][_\w\d]*)\s*\(c               @   sZ   e Zd ZdZej Zf  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  || _ |rf|j| _| jdkr$d| _|j| _|j| _tj|rft	|}t
|di | _xdD ]}	t| |	t
||	 q\W x&t| jD ]\}
}t| d
|
 | qW t| j}t| j}| jr|jd| j  |jd| j  n| jr|jd x.| jD ]$}	|jd|	  |jd|	|	f  qW | jrB|jd| j  |jd| j  dj|| _dj|| _ |jj | _|rr|| _|d k	r|| _|r|| _|r|| _|r|| _|r|| _t| dstd| d S )Nz<lambda>_lambda___annotations__r   r   r   r   
kwonlyargskwonlydefaultszarg%d*z%s=Nonez%s=%sz**z, 	signaturez%You are decorating a non function: %s)r   r   r   r   r   r   )shortsignature__name__name__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   a   sZ    




zFunctionMaker.__init__c             K   s   | j |_t| dd|_t| di |_| j|_| jp4d|_t| dd|_	yt
jd}W n tk
rl   d}Y nX |jjdd}t| d||_|jj| dS )	z2Update the signature of func with the data in selfr"   Nr/   r'      ?r   r$   )r    r   r&   r!   r-   r   __defaults__r   __kwdefaults__r   sys	_getframeAttributeError	f_globalsgetr#   update)r2   r3   kwframeZcallermoduler   r   r   r@      s    
zFunctionMaker.updateFc          
   K   s  |t |  }|pi }tj|}|dkr2td| |jd}t|gdd | jjdD  }x$|D ]}	|	dkrbtd	|	|f qbW |j	d
s|d
7 }dt
| jf }
yt||
d}t|| W n*   tdtjd t|tjd  Y nX || }|r ||d< | j|f| |S )zBMake a new function from a given template and update the signatureNz not a valid function template
%s   c             S   s   g | ]}|j d qS )z *)strip).0r6   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__)rH   rI   )varsDEFsearchSyntaxErrorgroupsetr   split	NameErrorendswithnext_compile_countcompileexecprintr;   stderrr@   )r2   Z	src_templevaldict	addsourceattrssrcmor    namesnfilenamecoder3   r   r   r   make   s4    



zFunctionMaker.makeTc             K   s   t |tr0|j jdd\}	}
|
dd }d}nd}	d}|}| ||	||||}djdd |j D }|jd}|rt|rd| j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.
        (rC   NrJ   c             s   s   | ]}d | V  qdS )z    Nr   )rE   liner   r   r   	<genexpr>   s    z'FunctionMaker.create.<locals>.<genexpr>rI   z#async def %(name)s(%(signature)s):
returnzreturn awaitzdef %(name)s(%(signature)s):
)

isinstancestrrD   rR   r,   
splitlinesr?   r   replacerd   )r   objZbodyr[   r   r"   r$   r\   r]   r    restr   r3   r2   Zibodycallerr   r   r   create   s    	


zFunctionMaker.create)NNNNNNN)NF)NNNT)r   r#   __qualname__r!   	itertoolscountrV   r   r   r   r   r   r   r   r@   rd   classmethodrq   r   r   r   r   r   T   s    
3
" r   c       	      C   sn   t || d}d}x0t|D ]$\}}d| }|||< ||d 7 }qW tj| d| || d}t| drj| j|_|S )zE
    decorate(func, caller) decorates a function using a caller.
    )rI   rH    z_e%d_z, z,return _call_(_func_, %s%%(shortsignature)s))__wrapped__rr   )r/   r)   r   rq   r0   rr   )	r3   rp   Zextrasr[   Zesr5   ZextraZexZfunr   r   r   decorate   s    

rx   c       
      C   s  |dk	rt || S df  }}tj| rB| jj }d| j| jf }n~tj| r| jdkr\d}n| j}| j}| jj}t	| j
pzf }dj| jj|| | }|r|d7 }| j
}n| jjj }| jj}t| t d}tjd	||f d
||f ||| j| d}	|rd| |	_
|	S )z=decorator(caller) converts a caller function into a decoratorNrv   zHdecorator(%s) converts functions/generators into factories of %s objectsz<lambda>r   z, rG   )_callZ
_decorate_z%s(func, %s)zhif func is None: return lambda func:  _decorate_(func, _call, (%s))
return _decorate_(func, _call, (%s)))r"   r$   rw   )N)rx   r   Zisclassr   lowerr%   r!   __code__co_argcountlenr9   r,   co_varnames	__class____call__r/   r   rq   r#   )
rp   Z_funcZdefaultargsr   r    r"   nargsZndefsr[   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_rH   )rw   )r   rq   r/   )r2   r3   r   r   r   r   #  s    zContextManager.__call__N)r   r#   rr   r   r   r   r   r   r   "  s   r      c             O   s   t j| |||S )N)r   r   )r2   gr4   kr   r   r   r   -  s    r      c             O   s   t j| |||S )N)r   r   )r2   r   r4   r   r   r   r   r   3  s    c             C   s   t | S )N)_contextmanager)r3   r   r   r   contextmanager:  s    r   c             C   sR   d}x:t |D ].\}}t|| r&d}P t| |r| ||< d}qW |rN|j|  dS )z_
    Append ``a`` to the list of the virtual ancestors, unless it is already
    included.
    TFN)r)   
issubclassr+   )r4   
vancestorsaddjvar   r   r   r+   A  s    

r+   c                 s@   ddj  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(%s,)z, rv   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}   r1   )Z	argumentsZwrongmsg)dispatch_argsr   r   check[  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                 sv    |  dd t tD }xHD ]@}x:t| ||D ]*\}}}t||r6||j kr6t|| q6W q$W 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   )rE   _r   r   r   rF   p  s    zIdispatch_on.<locals>.gen_func_dec.<locals>.vancestors.<locals>.<listcomp>c             S   s   g | ]}t |qS r   )rQ   )rE   rar   r   r   rF   u  s    )ranger}   zipr   mror+   )typesZrastypes_tZtype_r   )r   r   typemapr   r   r   k  s    
z5dispatch_on.<locals>.gen_func_dec.<locals>.vancestorsc                 s    |  g }xt | |  D ]p\}}t|}|dkrFtd||f n4|dkrr|\}td||fi j dd }n|j }|j|dd  qW |S )zG
            Get a list of virtual MROs, one for each type
            rC   zAmbiguous dispatch for %s: %sr   Nri   )r   r}   RuntimeErrortyper   r+   )r   Zlistsr   ZvasZn_vasr   r   )r   r   r   r   	ancestorsw  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 }x.t j |   D ]}|jtdd |D  qW |S )zI
            An utility to introspect the dispatch algorithm
            c             s   s   | ]}|j V  qd S )N)r   )rE   r4   r   r   r   rg     s    zKdispatch_on.<locals>.gen_func_dec.<locals>.dispatch_info.<locals>.<genexpr>)rs   productr+   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 }y| }W n tk
r2   Y nX |||S tj |  }t| x(|D ] }j|}|d k	rZ|||S qZW ||S )Nc             s   s   | ]}t |V  qd S )N)r   )rE   r6   r   r   r   rg     s    zGdispatch_on.<locals>.gen_func_dec.<locals>._dispatch.<locals>.<genexpr>)r   KeyErrorrs   r   rU   r?   )r   r   rA   r   r   combinationsr   )r   r3   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   rw   )rQ   r   r   rS   r   rq   r/   )r3   Zargsetr   r   r   )r   r   dispatch_str)r   r3   r   r   r   gen_func_deca  s    
z!dispatch_on.<locals>.gen_func_decdispatch_on)r,   r   ner   )r   r   r   )r   r   r   r   r   S  s
    W
r   )N)(r!   Z
__future__r   rer;   r   r   rs   collections__version__versionr   r	   
namedtupler
   r   r=   r   r   rW   rM   objectr   rx   r   
contextlibr   ImportErrorr   r   r   Zinitr}   r   Zn_argsr   r   r   r+   r   r   r   r   r   <module>!   s\   



 
&


