Your IP : 216.73.216.196


Current Path : /opt/alt/python27/lib/python2.7/site-packages/logilab/common/
Upload File :
Current File : //opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyc

�
�d{Tc@sldZddlmZdZddlZddlZddlmZmZddlm	Z	m
Z
ddlmZde
fd	��YZd
e
fd��YZdefd
��YZdefd��YZddd�Zde
fd��YZd�Zd�Zd�Zde
fd��YZde
fd��YZde
fd��YZd�Zd�Zdd�ZdS(s* A few useful function/method decorators. i����(tprint_functionsrestructuredtext enN(tclockttime(tisgeneratorfunctiont
getargspec(tmethod_typetcached_decoratorcBs#eZddd�Zdd�ZRS(cCs||_||_dS(N(t	cacheattrtkeyarg(tselfRR((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyt__init__"s	cCs�t|�std|��tt|�j�dksG|jdkr\t||j�}n6|jr�t||j|j�}nt	||j�}|j
�S(Ns#cannot cache generator function: %sii(RtAssertionErrortlenRtargsRt_SingleValueCacheRt_MultiValuesKeyArgCachet_MultiValuesCachetclosure(R	tcallableobjtcache((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyt__call__%s
*	N(t__name__t
__module__tNoneR
R(((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR!sRcBs/eZdd�Zd�Zd�Zd�ZRS(cCsJ||_|dkr(d|j|_n||jks=t�||_dS(Ns
_%s_cache_(tcallableRRRR(R	RR((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR
1s
	cGsPy|j|jSWn7tk
rK|j||�}t||j|�|SXdS(N(t__dict__RtKeyErrorRtsetattr(t_SingleValueCache__meR	R
tvalue((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR9s
csH�fd�}�|_y"�jj|_�jj|_WnnX|S(Ncs�j||�S(N(R(R
tkwargs(R	(sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pytwrappedBs(t	cache_objRt__doc__R(R	R((R	sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyRAs	cCs|jj|jd�dS(N(RtpopRR(R	tholder((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pytclearLsN(RRRR
RRR$(((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR0s		RcBseZd�Zd�ZRS(cCsEy|j|j}Wn*tk
r@i}t||j|�nX|S(N(RRRR(R	R#t_cache((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyt
_get_cacheQs
cOsN|j|�}y||SWn,tk
rI|j||�||<||SXdS(N(R&RR(t_MultiValuesCache__meR	R
RR%((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyRYs
(RRR&R(((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyRPs	RcBseZdd�Zd�ZRS(cCs&tt|�j||�||_dS(N(tsuperRR
R(R	RRR((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR
bscOsb|j|�}||jd}y||SWn/tk
r]|j|||�||<||SXdS(Ni(R&RRR(t_MultiValuesKeyArgCache__meR	R
RR%tkey((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyRfs
N(RRRR
R(((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyRascKs4||d<t|�}|dkr&|S||�SdS(s0Simple decorator to cache result of method call.RN(RR(RRRt	decorator((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pytcachedps

tcachedpropertycBs8eZdZdZd�Zed��Zdd�ZRS(s� 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
    RcCs<y|jWn!tk
r.td|��nX||_dS(Ns!%s must have a __name__ attribute(RtAttributeErrort	TypeErrorR(R	R((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR
�s
cCs-t|jdd�}d|r(d|ndS(NR!s+<wrapped by the cachedproperty decorator>%ss
%st(tgetattrRR(R	tdoc((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR!�scCs9|dkr|S|j|�}t||jj|�|S(N(RRRR(R	tinsttobjtypetval((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyt__get__�s
(RN(RRR!t	__slots__R
tpropertyRR6(((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR-zs

	cCs:|j}t||�}t|t�r3|j}n|jS(N(t	__class__R1t
isinstanceR8tfgetR (tobjtfuncnametclstmember((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pytget_cache_impl�s
	cCst||�j|�dS(s�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(R@R$(R<R=((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pytclear_cache�scCsEt||�j}yt|||j|�Wntk
r@nXdS(s/Copy cache for <funcname> from cacheobj to obj.N(R@RRRR(R<R=tcacheobjR((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyt
copy_cache�s

t	wpropertycBs)eZdZd�Zd�Zd�ZRS(s�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(Ns_%s(tsetfuncRtattrname(R	RE((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR
�s	cCs|j||�dS(N(RE(R	R<R((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyt__set__�scCs"|dk	st�t||j�S(N(RRR1RF(R	R<R>((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR6�s(RRR!R
RGR6(((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyRD�s		t
classpropertycBs eZdZd�Zd�ZRS(sCthis is a simple property-like class but for class attributes.
    cCs
||_dS(N(tget(R	RI((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR
�scCs
|j|�S(N(RI(R	R3R>((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR6�s(RRR!R
R6(((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyRH�s	ticlassmethodcBs)eZdZd�Zd�Zd�ZRS(s�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(tfunc(R	RK((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR
�scCs5|dkr"t|j||j�St|j||�S(N(RRRKR9(R	tinstanceR4((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR6�scCstd��dS(Nscan't set attribute(R.(R	RLR((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyRG�s(RRR!R
R6RG(((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyRJ�s		cs�fd�}|S(NcsMt�}t�}�||�}td�jt�|t�|f�|S(Ns%s clock: %.9f / time: %.9f(RRtprintR(R
Rtttctres(tf(sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pytwrap�s		((RQRR((RQsJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyttimed�scs��fd�}|S(s�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���fd�}|S(Ncs0�|�z�|||�SWd�|�XdS(N((R	R
R(tacquireRQtrelease(sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pytwrapper�s
((RQRV(RTRU(RQsJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR+�s((RTRUR+((RTRUsJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pytlocked�scs��fd�}|S(s}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
    csKy�p|j}Wn!tk
r6td|��nXt�||�|S(NsI%s has no __name__ attribute: you should provide an explicit `methodname`(RR.R(RKtname(tklasst
methodname(sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyR+s
((RYRZR+((RYRZsJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pytmonkeypatch�s	(R!t
__future__Rt
__docformat__tsysttypesRRtinspectRRtlogilab.common.compatRtobjectRRRRRR,R-R@RARCRDRHRJRSRWR[(((sJ/opt/alt/python27/lib/python2.7/site-packages/logilab/common/decorators.pyt<module>s, 
&