ó
[³XMc           @   s°   d  Z  d d l Z d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d	 Z d
 Z	 d Z
 d Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   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.

iÿÿÿÿNt	   HookErrorc           B   s   e  Z d  Z RS(   s7   An 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    +   s   c         C   s   t  |  | t | ƒ d S(   su   hook.addPre(klass, name, func) -> None

    Add a function to be called before the method klass.name is invoked.
    N(   t   _addHookt   PRE(   t   klasst   namet   func(    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyt   addPre.   s    c         C   s   t  |  | t | ƒ d S(   su   hook.addPost(klass, name, func) -> None

    Add a function to be called after the method klass.name is invoked.
    N(   R   t   POST(   R   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyt   addPost6   s    c         C   s   t  |  | t | ƒ d S(   s¡   hook.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   (   R   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyt	   removePre=   s    c         C   s   t  |  | t | ƒ d S(   s¡   hook.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
   (   R   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyt
   removePostF   s    s3  

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__c         C   s,   | t  j |  j d d ƒ |  j | f } | S(   s   string manipulation garbaget   .t   _(   t   stringt   replaceR   R   (   t   kt   nt   st   x(    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyt   _XXXd   s    (c         C   s   t  |  | t ƒ S(   sC   (private) munging to turn a method name into a pre-hook-method-name(   R   t   _PRE(   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyR   i   s    c         C   s   t  |  | t ƒ S(   sD   (private) munging to turn a method name into a post-hook-method-name(   R   t   _POST(   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyR
   m   s    c         C   s   t  |  | t ƒ S(   sE   (private) munging to turn a method name into an `original' identifier(   R   t   _ORIG(   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyt   ORIGq   s    c         C   sj   t  |  | ƒ t |  | |  | ƒ ƒ sA t |  | |  | ƒ g  ƒ n  t |  | |  | ƒ ƒ } | j | ƒ d S(   s,   (private) adds a hook to a method on a classN(   t   _enhookt   hasattrt   setattrt   getattrt   append(   R   R   t   phaseR   t	   phaselist(    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyR   v   s
    c         C   sÏ   | |  | ƒ } t  |  t |  | ƒ ƒ s6 t d ƒ ‚ n  t |  | |  | ƒ ƒ } y | j | ƒ Wn' t k
 rˆ t d | |  f ƒ ‚ n Xt |  t |  | ƒ ƒ rË t |  t |  | ƒ ƒ rË t |  | ƒ n  d S(   s1   (private) removes a hook from a method on a classs   no hooks present!s(   hook %s not found in removal list for %sN(	   R   R   R    R   t   removet
   ValueErrorR   R
   t   _dehook(   R   R   R!   R   t   phaselistnameR"   (    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyR      s     2c            sÀ   t  ˆ t ˆ ˆ  ƒ ƒ r d S‡  ‡ f d †  } y ˆ  | _ Wn t k
 rN n Xt ˆ ˆ  ƒ j } t ˆ t ˆ ˆ  ƒ | ƒ t ˆ t ˆ ˆ  ƒ g  ƒ t ˆ t ˆ ˆ  ƒ g  ƒ t ˆ ˆ  | ƒ d S(   s>   (private) causes a certain method name to be hooked on a classNc             s‹   x- t  ˆ t ˆ ˆ  ƒ ƒ D] } | |  | Ž  q Wz# t  ˆ t ˆ ˆ  ƒ ƒ |  | Ž  SWd  x- t  ˆ t ˆ ˆ  ƒ ƒ D] } | |  | Ž  qo WXd  S(   N(   R   R   R   R
   (   t   argst   kwt	   preMethodt
   postMethod(   R   R   (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyt   newfunc•   s    #(	   R   R   t	   func_namet	   TypeErrorR   t   im_funcR   R   R
   (   R   R   R+   t   oldfunc(    (   R   R   s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyR      s    c         C   s   t  |  t |  | ƒ ƒ s' t d ƒ ‚ n  t |  | t |  t |  | ƒ ƒ ƒ t |  t |  | ƒ ƒ t |  t |  | ƒ ƒ t |  t |  | ƒ ƒ d S(   sH   (private) causes a certain method name no longer to be hooked on a classs   Cannot unhook!N(   R   R   R    R   R   t   delattrR   R
   (   R   R   (    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyR%   ©   s    "(   R   R   t	   ExceptionR    R	   R   R   R   t   hooked_funcR   R   R   R   R   R
   R   R   R   R   R%   (    (    (    s7   /usr/lib/python2.7/dist-packages/twisted/python/hook.pyt   <module>$   s$   												