B
    fFC                 @   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d	d
Zdd Zdd Zy
ejZW n ek
r   dd ZY nX yddlmZ W n ek
r   dd ZY nX edZG dd deZd*ddZd+ddZyddlmZ W n" ek
r6   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r~ej"s~d 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   L/home/ankuromar296_gmail_com/.local/lib/python3.7/site-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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   )isgeneratorfunctionc             C   s   dS )NFr   )callerr   r   r   r   H   s    r   z\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.
    r   Nc             C   s  || _ |rf|j| _| jdkr$d| _|j| _|j| _t|rft	|}t
|di | _xdD ]}	t| |	t
||	 q\W x&t| jD ]\}
}t| d|
 | qW t| j}t| j}| jr|d| j  |d| j  n| jr|d x.| jD ]$}	|d|	  |d|	|	f  qW | jrB|d	| j  |d	| j  d
|| _d
|| _ |j | _|rr|| _|d k	r|| _|r|| _|r|| _|r|| _|r|| _t| dstt| dstd| d S )Nz<lambda>_lambda___annotations__)argsvarargsvarkwdefaults
kwonlyargskwonlydefaultszarg%d*z%s=Nonez%s=%sz**z, 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AssertionError	TypeError)selffuncr   r   r   r"   r$   ZfuncdictZargspecaiargZallargsZallshortargsr   r   r   r   ]   sZ    





zFunctionMaker.__init__c             K   s   | j |_t| dd|_t| di |_| j|_| jp4d|_t| dd|_	yt
d}W n tk
rl   d}Y nX |jdd}t| d||_|j| dS )	z2Update the signature of func with the data in selfr"   Nr/   r'   r   ?r    r$   )r   r    r&   r!   r-   r   __defaults__r   __kwdefaults__r   sys	_getframeAttributeError	f_globalsgetr#   update)r3   r4   kwframeZcallermoduler   r   r   r@      s    
zFunctionMaker.updateFc             K   s  |t |  }|pi }t|}|dkr2td| |d}t|gdd | jdD  }x$|D ]}	|	dkrbtd|	|f qbW |	d	s|d	7 }d
t
| j }
y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 )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).0r7   r   r   r   
<listcomp>   s    z&FunctionMaker.make.<locals>.<listcomp>,)_func__call_z%s is overridden in
%s
z<decorator-gen-%d>singlezError in generated code:)fileZ
__source__)varsDEFsearchSyntaxErrorgroupsetr   split	NameErrorendswithnext_compile_countcompileexec	Exceptionprintr;   stderrr@   )r3   Z	src_templevaldict	addsourceattrssrcmor   namesnfilenamecoder4   r   r   r   make   s4    



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.
        (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   rS   r,   
splitlinesr?   r   replacerf   )r   objbodyr]   r   r"   r$   r^   r_   r   restr   r4   r3   Zibodyr   r   r   r   create   s    	


zFunctionMaker.create)NNNNNNN)NF)NNNT)r    r#   __qualname__r!   	itertoolscountrW   r   r   r   r   r   r   r   r@   rf   classmethodrs   r   r   r   r   r   P   s    
3
" r   r   c       
      C   s   t || d}d}x0t|D ]$\}}d| }|||< ||d 7 }qW dtj  krXdk rpn nt|olt| }nt|}|rt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.
    )rI   rH    z_e%d_z, z3.5z3.6z?for res in _call_(_func_, %s%%(shortsignature)s):
    yield res)__wrapped__z,return _call_(_func_, %s%%(shortsignature)s)rt   )
r/   r)   r;   versionr   r   r   rs   r0   rt   )
r4   r   extrasr]   esr6   extraexZgeneratorcallerZfunr   r   r   decorate   s(    

r   c       
      C   s  |dk	rt || S d\}}t| r@| j }d| j| jf }n~t| r| jdkrZd}n| j}| j}| jj}t	| j
pxd}d| jj|| | }|r|d7 }| j
}n| 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 decoratorN)rx   r   zHdecorator(%s) converts functions/generators into factories of %s objectsz<lambda>r   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$   ry   )N)r   r   isclassr    lowerr%   r!   __code__co_argcountlenr9   r,   co_varnames	__class____call__r/   r   rs   r#   )
r   _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   )ry   )r   rs   r/   )r3   r4   r   r   r   r   0  s    zContextManager.__call__N)r    r#   rt   r   r   r   r   r   r   /  s   r      c             O   s   t | |||S )N)r   r   )r3   gr5   kr   r   r   r   :  s    r      c             O   s   t | |||S )N)r   r   )r3   r   r5   r   r   r   r   r   @  s    c             C   s   t | S )N)_contextmanager)r4   r   r   r   contextmanagerG  s    r   c             C   sR   d}x:t |D ].\}}t|| r&d}P t| |r| ||< d}qW |rN||  dS )z_
    Append ``a`` to the list of the virtual ancestors, unless it is already
    included.
    TFN)r)   
issubclassr+   )r5   
vancestorsaddjvar   r   r   r+   N  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,)z, rx   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   r2   )	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                 sv    |  dd t tD }xHD ]@}x:t| ||D ]*\}}}t||r6|| 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   }  s    zIdispatch_on.<locals>.gen_func_dec.<locals>.vancestors.<locals>.<listcomp>c             S   s   g | ]}t |qS r   )rR   )rE   rar   r   r   rF     s    )ranger   zipr   mror+   )typesZrastypes_ttype_r   )r   r   typemapr   r   r   x  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  dd }n| }||dd  qW |S )zG
            Get a list of virtual MROs, one for each type
            rC   zAmbiguous dispatch for %s: %sr   Nrh   )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 }x.t j |   D ]}|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   r5   r   r   r   rj     s    zKdispatch_on.<locals>.gen_func_dec.<locals>.dispatch_info.<locals>.<genexpr>)ru   productr+   tuple)r   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 ] }|}|d k	rZ|||S qZW ||S )Nc             s   s   | ]}t |V  qd S )N)r   )rE   r7   r   r   r   rj     s    zGdispatch_on.<locals>.gen_func_dec.<locals>._dispatch.<locals>.<genexpr>)r   KeyErrorru   r   rV   r?   )r   r   rA   r   r   combinationsr   )r   r4   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   ry   )rR   r   r   rT   r   rs   r/   )r4   Zargsetr   r   r   )r   r   dispatch_str)r   r4   r   r   r   gen_func_decn  s    
z!dispatch_on.<locals>.gen_func_decdispatch_on)r1   r,   r   ner    )r   r   r   )r   r   r   r   r   `  s    W
r   )r   )N)'r!   
__future__r   rer;   r   r   ru   collections__version__version_infor   r	   
namedtupler
   r   r=   r   ImportErrorrX   rN   objectr   r   r   
contextlibr   r   r   r   initr   r   Zn_argsr   r   r   r+   r   r   r   r   r   <module>!   s^   



 
"
'


