Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/python/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/python/hook.pyc


[XMc@sdZddlZdefdYZdZdZdZdZd	Zd
Z	dZ
dZd
ZdZ
dZdZdZdZdZdZdS(s
I define support for hookable instance methods.

These are methods which you can register pre-call and post-call external
functions to augment their functionality.  People familiar with more esoteric
languages may think of these as "method combinations".

This could be used to add optional preconditions, user-extensible callbacks
(a-la emacs) or a thread-safety mechanism.

The four exported calls are:

   - L{addPre}
   - L{addPost}
   - L{removePre}
   - L{removePost}

All have the signature (class, methodName, callable), and the callable they
take must always have the signature (instance, *args, **kw) unless the
particular signature of the method they hook is known.

Hooks should typically not throw exceptions, however, no effort will be made by
this module to prevent them from doing so.  Pre-hooks will always be called,
but post-hooks will only be called if the pre-hooks do not raise any exceptions
(they will still be called if the main method raises an exception).  The return
values and exception status of the main method will be propogated (assuming
none of the hooks raise an exception).  Hooks will be executed in the order in
which they are added.

iNt	HookErrorcBseZdZRS(s7An error which will fire when an invariant is violated.(t__name__t
__module__t__doc__(((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyR+scCst||t|dS(suhook.addPre(klass, name, func) -> None

    Add a function to be called before the method klass.name is invoked.
    N(t_addHooktPRE(tklasstnametfunc((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pytaddPre.scCst||t|dS(suhook.addPost(klass, name, func) -> None

    Add a function to be called after the method klass.name is invoked.
    N(RtPOST(RRR((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pytaddPost6scCst||t|dS(shook.removePre(klass, name, func) -> None

    Remove a function (previously registered with addPre) so that it
    is no longer executed before klass.name.
    N(t_removeHookR(RRR((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyt	removePre=scCst||t|dS(shook.removePre(klass, name, func) -> None

    Remove a function (previously registered with addPost) so that it
    is no longer executed after klass.name.
    N(RR
(RRR((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyt
removePostFss3

import %(module)s

def %(name)s(*args, **kw):
    klazz = %(module)s.%(klass)s
    for preMethod in klazz.%(preName)s:
        preMethod(*args, **kw)
    try:
        return klazz.%(originalName)s(*args, **kw)
    finally:
        for postMethod in klazz.%(postName)s:
            postMethod(*args, **kw)
s__hook_pre_%s_%s_%s__s__hook_post_%s_%s_%s__s__hook_orig_%s_%s_%s__cCs,|tj|jdd|j|f}|S(sstring manipulation garbaget.t_(tstringtreplaceRR(tktntstx((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyt_XXXds(cCst||tS(sC(private) munging to turn a method name into a pre-hook-method-name(Rt_PRE(RR((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyRiscCst||tS(sD(private) munging to turn a method name into a post-hook-method-name(Rt_POST(RR((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyR
mscCst||tS(sE(private) munging to turn a method name into an `original' identifier(Rt_ORIG(RR((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pytORIGqscCsjt||t||||sAt||||gnt||||}|j|dS(s,(private) adds a hook to a method on a classN(t_enhookthasattrtsetattrtgetattrtappend(RRtphaseRt	phaselist((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyRvs

cCs|||}t|t||s6tdnt||||}y|j|Wn'tk
rtd||fnXt|t||rt|t||rt||ndS(s1(private) removes a hook from a method on a classsno hooks present!s(hook %s not found in removal list for %sN(	RRRRtremovet
ValueErrorRR
t_dehook(RRR!Rt
phaselistnameR"((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyRs
2csttrdSfd}y
|_Wntk
rNnXtj}tt|ttgttgt|dS(s>(private) causes a certain method name to be hooked on a classNcsx-ttD]}|||qWz#tt||SWdx-ttD]}|||qoWXdS(N(RRRR
(targstkwt	preMethodt
postMethod(RR(s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pytnewfuncs#(	RRt	func_namet	TypeErrorRtim_funcRRR
(RRR+toldfunc((RRs7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyRs

cCst|t||s'tdnt||t|t||t|t||t|t||t|t||dS(sH(private) causes a certain method name no longer to be hooked on a classsCannot unhook!N(RRRRRtdelattrRR
(RR((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyR%s"(RRt	ExceptionRR	RR
Rthooked_funcRRRRRR
RRRRR%(((s7/usr/lib/python2.7/dist-packages/twisted/python/hook.pyt<module>$s$