a
    ö£BepA  ã                   @   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 u�rv|| _|�r‚|| _|�rŽ|| _|�rš|| _|�r¦|| _t| dƒ�s¶J ‚t| dƒ�sÎt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Ú	TypeError)ÚselfÚfuncr   r   r   r   r   ZfuncdictZargspecÚaÚiÚargZallargsZallshortargsr   r   úL/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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yj   d}Y n0 |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,   r7   t   s    
zFunctionMaker.updateFc                 K   s  |t | ƒ }|pi }t |¡}|du r2td| ƒ‚| d¡}t|gdd„ | j d¡D ƒ ƒ}|D ]}	|	dv r`td|	|f ƒ‚q`| 	d	¡s�|d	7 }d
t
| jƒ }
zt||
dƒ}t||ƒ W n0 tyè   tdtjd� t|tjd� ‚ Y n0 || }|rþ||d< | j|fi |¤Ž |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>‘   ó    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Úprintr2   Ústderrr7   )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¡}|rŽt|ƒrŽd	|  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.
        ú(r:   NéÿÿÿÿrC   c                 s   s   | ]}d | V  qdS )z    Nr   )r=   Úliner   r   r,   Ú	<genexpr>½   r?   z'FunctionMaker.create.<locals>.<genexpr>rB   z#async def %(name)s(%(signature)s):
Úreturnzreturn awaitzdef %(name)s(%(signature)s):
)
Ú
isinstanceÚstrr;   rL   r!   Ú
splitlinesr6   r   Úreplacer_   )ÚclsÚobjÚbodyrV   r   r   r   rW   rX   r   Úrestr   r(   r'   ZibodyÚcallerr   r   r,   Úcreate«   s     	

ÿzFunctionMaker.create)NNNNNNN)NF)NNNT)r   r   Ú__qualname__r   Ú	itertoolsÚcountrP   r	   r
   r   r   r   r   r-   r7   r_   Úclassmethodrn   r   r   r   r,   r   3   s     ÿ
4
$  ÿr   c                 C   s$   |j | i |¤Ž}| ¡  |j|jfS )zA
    Fix args and kwargs to be consistent with the signature
    )ÚbindÚapply_defaultsr	   Úkwargs)r	   ru   ÚsigÚbar   r   r,   ÚfixÇ   s    rx   r   Fc                    s*  t  ˆ¡‰tˆ ƒr(‡ ‡‡‡‡fdd„}n2tˆ ƒrF‡ ‡‡‡‡fdd„}n‡ ‡‡‡‡fdd„}ˆj|_ˆj|_ˆ|_ˆ|_ˆj|_zˆj	|_	W n t
yœ   Y n0 zˆj|_W n t
y¼   Y n0 zˆj|_W n t
yÜ   Y n0 zˆj|_W n t
yü   Y n0 z|j ˆj¡ W n t
�y$   Y n0 |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                  Ÿ   s4   ˆst | |ˆƒ\} }ˆ ˆgˆ|  ¢R i |¤ŽI d H S ©N©rx   ©r	   r8   ©rm   Úextrasr(   Úkwsyntaxrv   r   r,   ÚfunÚ   s    zdecorate.<locals>.func                  ?   s>   ˆst | |ˆƒ\} }ˆ ˆgˆ|  ¢R i |¤ŽD ]
}|V  q.d S ry   rz   )r	   r8   Úresr|   r   r,   r   ß   s    c                     s.   ˆst | |ˆƒ\} }ˆ ˆgˆ|  ¢R i |¤ŽS ry   rz   r{   r|   r   r,   r   å   s    )r   r   r   r   r   r   Ú__wrapped__Ú__signature__ro   r0   r4   r1   r   r   r"   r7   )r(   rm   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�   ro   )r   rn   r$   ro   ©r(   ©rm   r   r,   Údec  s    
üzdecoratorx.<locals>.decr   )rm   r†   r   r…   r,   Ú
decoratorx  s    r‡   c                    s’   |du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u r|‘qS r   )ÚkindÚPOS©r=   Úpr   r   r,   r>     r?   zdecorator.<locals>.<listcomp>c                    sX   t |ƒd }|t‡fdd„ˆ|d … D ƒƒ ‰ | d u rF‡‡ ‡fdd„S t| ˆˆ ˆƒS d S )Nr:   c                 3   s(   | ] }|j turˆ  |j|j ¡V  qd S ry   )ÚdefaultÚEMPTYr6   r   rŠ   )r8   r   r,   rc   "  s   
þz)decorator.<locals>.dec.<locals>.<genexpr>c                    s   t | ˆ ˆˆƒS ry   ©rƒ   r„   )rm   r}   r~   r   r,   Ú<lambda>&  r?   z(decorator.<locals>.dec.<locals>.<lambda>)ÚlenÚtuplerƒ   )r(   r	   r8   Úna©rm   Z
dec_paramsr~   )r}   r8   r,   r†      s    
ÿ
zdecorator.<locals>.dec)Ú
parametersr1   )N)rƒ   r   r   r”   Úvaluesrh   r‚   r   r   r�   ro   r   r1   r"   r7   )rm   Z_funcr~   rv   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 ry   )r   r-   )r'   Úgr)   Úkr   r   r,   r-   7  s    zContextManager.__init__c                    s   ‡ fdd„}t ||ƒS )Nc                    sP   ˆ j ˆ jgˆ j¢R i ˆ j¤Ž� | |i |¤ŽW  d   ƒ S 1 sB0    Y  d S ry   )Ú	__class__r(   r	   Úkwds)Úfr)   r™   ©r'   r   r,   rm   ;  s     z'ContextManager.__call__.<locals>.callerrŽ   )r'   r(   rm   r   r�   r,   Ú__call__:  s    zContextManager.__call__N)r   r   ro   r-   rž   r   r   r   r,   r—   6  s   r—   c                 C   s   t | ƒS ry   )Ú_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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   Ú 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|| ¡ v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  r?   zIdispatch_on.<locals>.gen_func_dec.<locals>.vancestors.<locals>.<listcomp>c                 S   s   g | ]}t |ƒ‘qS r   )rK   )r=   Úrar   r   r,   r>     r?   )Ú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²   Nra   )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 ry   )r   )r=   r)   r   r   r,   rc   §  r?   zKdispatch_on.<locals>.gen_func_dec.<locals>.dispatch_info.<locals>.<genexpr>)rp   Ú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y0   Y n0 ||i |¤ŽS tjˆ |Ž Ž }t|ƒ |D ](}ˆ |¡}|d urZ||i |¤Ž  S qZˆ|i |¤ŽS )Nc                 s   s   | ]}t |ƒV  qd S ry   )r¶   r<   r   r   r,   rc   «  r?   zGdispatch_on.<locals>.gen_func_dec.<locals>._dispatch.<locals>.<genexpr>)r‘   ÚKeyErrorrp   r¼   rO   r6   )r©   r	   r8   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�   )rK   r   r	   rM   r   rn   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¹   Úner   )r©   rÃ   r   rÂ   r,   rÄ   ]  s    W
rÄ   )r   F)NF)r   Úrer2   r   r¹   rp   Ú
contextlibr   r   r   r   Ú__version__rQ   rG   Ú	ParameterÚPOSITIONAL_OR_KEYWORDr‰   Úemptyr�   Úobjectr   rx   rƒ   r‡   r–   r—   rŸ   r    r    rÄ   r   r   r   r,   Ú<module>   s,   
 	
6
!