Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/decorators.cpython-37.pyc

B

�d{T�"�@sdZddlmZdZddlZddlZddlmZmZddlm	Z	m
Z
ddlmZGdd	�d	e
�ZGd
d�de
�ZGdd
�d
e�ZGdd�de�Zd&dd�ZGdd�de
�Zdd�Zdd�Zdd�ZGdd�de
�ZGdd�de
�ZGdd�de
�Zd d!�Zd"d#�Zd'd$d%�ZdS)(z* A few useful function/method decorators. �)�print_functionzrestructuredtext enN)�clock�time)�isgeneratorfunction�
getargspec)�method_typec@s eZdZddd�Zddd�ZdS)�cached_decoratorNcCs||_||_dS)N)�	cacheattr�keyarg)�selfr	r
�r�J/opt/alt/python37/lib/python3.7/site-packages/logilab/common/decorators.py�__init__"szcached_decorator.__init__cCsjt|�rtd|��tt|�j�dks0|jdkr>t||j�}n$|jrVt||j|j�}nt	||j�}|�
�S)Nz#cannot cache generator function: %s�r)r�AssertionError�lenr�argsr
�_SingleValueCacher	�_MultiValuesKeyArgCache�_MultiValuesCache�closure)r�callableobj�cacherrr
�__call__%s

zcached_decorator.__call__)NN)N)�__name__�
__module__�__qualname__rrrrrr
r!s
rc@s.eZdZd
dd�Zdd�Zdd�Zdd	�ZdS)rNcCs4||_|dkrd|j|_n||jks*t�||_dS)Nz
_%s_cache_)�callablerr	r)rrr	rrr
r1s
z_SingleValueCache.__init__cGsDy|j|jStk
r>|j|f|��}t||j|�|SXdS)N)�__dict__r	�KeyErrorr�setattr)Z_SingleValueCache__merr�valuerrr
r9sz_SingleValueCache.__call__cs<�fdd�}�|_y�jj|_�jj|_WnYnX|S)Ncs�j||�S)N)r)r�kwargs)rrr
�wrappedBsz*_SingleValueCache.closure.<locals>.wrapped)�	cache_objr�__doc__r)rr#r)rr
rAs
z_SingleValueCache.closurecCs|j�|jd�dS)N)r�popr	)r�holderrrr
�clearLsz_SingleValueCache.clear)N)rrrrrrr(rrrr
r0s
rc@seZdZdd�Zdd�ZdS)rcCs<y|j|j}Wn&tk
r6i}t||j|�YnX|S)N)rr	rr )rr'�_cacherrr
�
_get_cacheQsz_MultiValuesCache._get_cachecOsD|�|�}y||Stk
r>|j|f|��||<||SXdS)N)r*rr)Z_MultiValuesCache__merrr"r)rrr
rYs
z_MultiValuesCache.__call__N)rrrr*rrrrr
rPsrcs&eZdZd�fdd�	Zdd�Z�ZS)rNcstt|��||�||_dS)N)�superrrr
)rrr
r	)�	__class__rr
rbsz _MultiValuesKeyArgCache.__init__cOsT|�|�}||jd}y||Stk
rN|j|f|�|�||<||SXdS)Nr)r*r
rr)Z_MultiValuesKeyArgCache__merrr"r)�keyrrr
rfs
z _MultiValuesKeyArgCache.__call__)N)rrrrr�
__classcell__rr)r,r
rasrcKs*||d<tf|�}|dkr|S||�SdS)z0Simple decorator to cache result of method call.r
N)r)rr
r"�	decoratorrrr
�cachedps

r0c@s2eZdZdZdZdd�Zedd��Zd
dd	�ZdS)�cachedpropertya� Provides a cached property equivalent to the stacking of
    @cached and @property, but more efficient.

    After first usage, the <property_name> becomes part of the object's
    __dict__. Doing:

      del obj.<property_name> empties the cache.

    Idea taken from the pyramid_ framework and the mercurial_ project.

    .. _pyramid: http://pypi.python.org/pypi/pyramid
    .. _mercurial: http://pypi.python.org/pypi/Mercurial
    )r#cCs6y
|jWn tk
r*td|��YnX||_dS)Nz!%s must have a __name__ attribute)r�AttributeError�	TypeErrorr#)rr#rrr
r�s
zcachedproperty.__init__cCs"t|jdd�}d|rd|ndS)Nr%z+<wrapped by the cachedproperty decorator>%sz
%s�)�getattrr#)r�docrrr
r%�szcachedproperty.__doc__NcCs*|dkr|S|�|�}t||jj|�|S)N)r#r r)r�inst�objtype�valrrr
�__get__�s

zcachedproperty.__get__)N)rrrr%�	__slots__r�propertyr:rrrr
r1zs

r1cCs&|j}t||�}t|t�r |j}|jS)N)r,r5�
isinstancer<�fgetr$)�obj�funcname�cls�memberrrr
�get_cache_impl�s


rCcCst||��|�dS)z�Clear a cache handled by the :func:`cached` decorator. If 'x' class has
    @cached on its method `foo`, type

    >>> clear_cache(x, 'foo')

    to purge this method's cache on the instance.
    N)rCr()r?r@rrr
�clear_cache�srDcCs<t||�j}yt|||j|�Wntk
r6YnXdS)z/Copy cache for <funcname> from cacheobj to obj.N)rCr	r rr)r?r@Zcacheobjr	rrr
�
copy_cache�s
rEc@s(eZdZdZdd�Zdd�Zdd�ZdS)	�	wpropertyz�Simple descriptor expecting to take a modifier function as first argument
    and looking for a _<function name> to retrieve the attribute.
    cCs||_d|j|_dS)Nz_%s)�setfuncr�attrname)rrGrrr
r�szwproperty.__init__cCs|�||�dS)N)rG)rr?r!rrr
�__set__�szwproperty.__set__cCs|dk	st�t||j�S)N)rr5rH)rr?rArrr
r:�szwproperty.__get__N)rrrr%rrIr:rrrr
rF�srFc@s eZdZdZdd�Zdd�ZdS)�
classpropertyzCthis is a simple property-like class but for class attributes.
    cCs
||_dS)N)�get)rrKrrr
r�szclassproperty.__init__cCs
|�|�S)N)rK)rr7rArrr
r:�szclassproperty.__get__N)rrrr%rr:rrrr
rJ�srJc@s(eZdZdZdd�Zdd�Zdd�ZdS)	�iclassmethodz�Descriptor for method which should be available as class method if called
    on the class or instance method if called on an instance.
    cCs
||_dS)N)�func)rrMrrr
r�sziclassmethod.__init__cCs&|dkrt|j||j�St|j||�S)N)rrMr,)r�instancer8rrr
r:�sziclassmethod.__get__cCstd��dS)Nzcan't set attribute)r2)rrNr!rrr
rI�sziclassmethod.__set__N)rrrr%rr:rIrrrr
rL�srLcs�fdd�}|S)Ncs:t�}t�}�||�}td�jt�|t�|f�|S)Nz%s clock: %.9f / time: %.9f)rr�printr)rr"�t�c�res)�frr
�wrap�s
ztimed.<locals>.wrapr)rSrTr)rSr
�timed�srUcs��fdd�}|S)z�Decorator taking two methods to acquire/release a lock as argument,
    returning a decorator function which will call the inner method after
    having called acquire(self) et will call release(self) afterwards.
    cs���fdd�}|S)Ncs(�|�z�|f|�|�S�|�XdS)Nr)rrr")�acquirerS�releaserr
�wrapper�sz*locked.<locals>.decorator.<locals>.wrapperr)rSrX)rVrW)rSr
r/�szlocked.<locals>.decoratorr)rVrWr/r)rVrWr
�locked�srYcs��fdd�}|S)a}Decorator extending class with the decorated callable. This is basically
    a syntactic sugar vs class assignment.

    >>> class A:
    ...     pass
    >>> @monkeypatch(A)
    ... def meth(self):
    ...     return 12
    ...
    >>> a = A()
    >>> a.meth()
    12
    >>> @monkeypatch(A, 'foo')
    ... def meth(self):
    ...     return 12
    ...
    >>> a.foo()
    12
    cs@y�p
|j}Wn tk
r.td|��YnXt�||�|S)NzI%s has no __name__ attribute: you should provide an explicit `methodname`)rr2r )rM�name)�klass�
methodnamerr
r/szmonkeypatch.<locals>.decoratorr)r[r\r/r)r[r\r
�monkeypatch�s	r])NN)N)r%�
__future__r�
__docformat__�sys�typesrr�inspectrrZlogilab.common.compatr�objectrrrrr0r1rCrDrErFrJrLrUrYr]rrrr
�<module>s, 

&