a
     ˜ëjS   ã                   @   s˜   d Z dZg d¢ZddlZejdd… Zdd„ ejD ƒe_ddlZee_dd„ Zd	d
„ Zddd„Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdS )a  
Decorator module by Michele Simionato <michelesimionato@libero.it>
Copyright Michele Simionato, distributed under the terms of the BSD License (see below).
http://www.phyast.pitt.edu/~micheles/python/documentation.html

Included in NLTK for its support of a nice memoization decorator.
zrestructuredtext en)Ú	decoratorÚnew_wrapperÚgetinfoé    Nc                 C   s    g | ]}|rd t |ƒvr|‘qS )Znltk)Ústr)Ú.0Úp© r   úN/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/decorators.pyÚ
<listcomp>   ó    r
   c                 C   sh   t | ƒdd…  d¡}t|ƒD ]>\}}| d¡dkrP|d| d¡…  ¡ ||< q| ¡ ||< qd |¡S )z×
    For retrocompatibility reasons, we don't use a standard Signature.
    Instead, we use the string generated by this method.
    Basically, from a Signature we create a string and remove the default values.
    é   éÿÿÿÿú,ú=r   z, )r   ÚsplitÚ	enumerateÚcountÚindexÚstripÚjoin)Ú	signatureZlistsignatureÚcounterÚparamr   r   r	   Ú__legacysignature   s    r   c           
      C   s´   t  | ¡st  | ¡sJ ‚t  | ¡}|dd… \}}}t|ƒ}|rJ| |¡ |rX| |¡ t  | ¡}t|ƒ}t| dƒr‚| j	}| j
}	n| j}| j}	t| j|||| j| j| j| j|	|d�
S )aé  
    Returns an info dictionary containing:
    - name (the name of the function : str)
    - argnames (the names of the arguments : list)
    - defaults (the values of the default arguments : tuple)
    - signature (the signature : str)
    - fullsignature (the full signature : Signature)
    - doc (the docstring : str)
    - module (the module name : str)
    - dict (the function __dict__ : str)

    >>> def f(self, x=1, y=2, *args, **kw): pass

    >>> info = getinfo(f)

    >>> info["name"]
    'f'
    >>> info["argnames"]
    ['self', 'x', 'y', 'args', 'kw']

    >>> info["defaults"]
    (1, 2)

    >>> info["signature"]
    'self, x, y, *args, **kw'

    >>> info["fullsignature"]
    <Signature (self, x=1, y=2, *args, **kw)>
    Né   Ú__closure__)
ÚnameÚargnamesr   ÚfullsignatureÚdefaultsÚdocÚmoduleÚdictÚglobalsÚclosure)ÚinspectÚismethodÚ
isfunctionÚgetfullargspecÚlistÚappendr   r   Úhasattrr   Ú__globals__Zfunc_closureZfunc_globalsr"   Ú__name__Ú__defaults__Ú__doc__Ú
__module__Ú__dict__)
ÚfuncZargspecZregargsÚvarargsZ	varkwargsr   r   r   Z_closureZ_globalsr   r   r	   r   ,   s6    




ör   c                 C   sN   |p
t |ƒ}|d | _|d | _|d | _| j |d ¡ |d | _|| _| S )z akin to functools.update_wrapperr   r    r!   r"   r   )r   r-   r/   r0   r1   Úupdater.   Zundecorated)ÚwrapperÚmodelÚinfodictr   r   r	   Úupdate_wrapperl   s    



r8   c                 C   sP   t |tƒr|}nt|ƒ}d|d vs,J dƒ‚d| }t|t| d�ƒ}t|||ƒS )aA  
    An improvement over functools.update_wrapper. The wrapper is a generic
    callable object. It works by generating a copy of the wrapper with the
    right signature and by updating the copy, not the original.
    Moreovoer, 'model' can be a dictionary with keys 'name', 'doc', 'module',
    'dict', 'defaults'.
    Ú	_wrapper_r   z("_wrapper_" is a reserved argument name!z.lambda %(signature)s: _wrapper_(%(signature)s))r9   )Ú
isinstancer"   r   Úevalr8   )r5   r6   r7   ÚsrcZfuncopyr   r   r	   r   x   s    

ÿþr   c                    s   t ‡ ‡fdd„ˆ ƒS )Nc                     s   ˆj ˆ g| ¢R i |¤ŽS )N)Úcall)ÚaÚk©r2   Úselfr   r	   Ú<lambda>Ž   r   z__call__.<locals>.<lambda>)r   )rA   r2   r   r@   r	   Ú__call__�   s    rC   c                 C   s6   t t| ƒƒ}d|v rtdƒ‚d|vr,tdƒ‚t| _| S )zí
    Take a class with a ``.caller`` method and return a callable decorator
    object. It works by adding a suitable __call__ method to the class;
    it raises a TypeError if the class already has a nontrivial __call__
    method.
    rC   z=You cannot decorate a class with a nontrivial __call__ methodr=   z2You cannot decorate a class without a .call method)ÚsetÚdirÚ	TypeErrorrC   )ÚclsÚattrsr   r   r	   Údecorator_factory‘   s    ÿrI   c                    s(   t  ˆ ¡rtˆ ƒS ‡ fdd„}t|ˆ ƒS )aò  
    General purpose decorator factory: takes a caller function as
    input and returns a decorator with the same attributes.
    A caller function is any function like this::

     def caller(func, *args, **kw):
         # do something
         return func(*args, **kw)

    Here is an example of usage:

    >>> @decorator
    ... def chatty(f, *args, **kw):
    ...     print("Calling %r" % f.__name__)
    ...     return f(*args, **kw)

    >>> chatty.__name__
    'chatty'

    >>> @chatty
    ... def f(): pass
    ...
    >>> f()
    Calling 'f'

    decorator can also take in input a class with a .caller method; in this
    case it converts the class into a factory of callable decorator objects.
    See the documentation for an example.
    c                    sN   t | ƒ}|d }d|v s d|v r(J dƒ‚d| }t|t| ˆ d�ƒ}t|| |ƒS )Nr   Ú_call_Ú_func_z2You cannot use _call_ or _func_ as argument names!z3lambda %(signature)s: _call_(_func_, %(signature)s))rK   rJ   )r   r;   r"   r8   )r2   r7   r   r<   Zdec_func©Úcallerr   r	   Ú
_decoratorÄ   s    ÿÿþzdecorator.<locals>._decorator)r%   ÚisclassrI   r8   )rM   rN   r   rL   r	   r   £   s    
r   c                 C   s:   zt | |ƒW S  ty4   |ƒ }t| ||ƒ | Y S 0 dS )z'Similar to .setdefault in dictionaries.N)ÚgetattrÚAttributeErrorÚsetattr)Úobjr   Zdefault_thunkÚdefaultr   r   r	   Úgetattr_Ò   s    rU   c                 G   s0   t | dtƒ}||v r|| S | |Ž }|||< |S )NZmemoize_dic)rU   r"   )r2   ÚargsZdicÚresultr   r   r	   ÚmemoizeÜ   s    rX   )N)r/   Z__docformat__Ú__all__ÚsysÚpathZOLD_SYS_PATHr%   r   r   r8   r   rC   rI   r   rU   rX   r   r   r   r	   Ú<module>   s"   @
/
