Package twisted :: Package python :: Module hook
[frames | no frames]

Module twisted.python.hook

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:

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.
Exceptions
HookError An error which will fire when an invariant is violated.

Function Summary
  _addHook(klass, name, phase, func)
(private) adds a hook to a method on a class
  _dehook(klass, name)
(private) causes a certain method name no longer to be hooked on a class
  _enhook(klass, name)
(private) causes a certain method name to be hooked on a class
  _removeHook(klass, name, phase, func)
(private) removes a hook from a method on a class
  _XXX(k, n, s)
string manipulation garbage
  addPost(klass, name, func)
hook.addPost(klass, name, func) -> None
  addPre(klass, name, func)
hook.addPre(klass, name, func) -> None
  ORIG(k, n)
(private) munging to turn a method name into an `original' identifier
  POST(k, n)
(private) munging to turn a method name into a post-hook-method-name
  PRE(k, n)
(private) munging to turn a method name into a pre-hook-method-name
  removePost(klass, name, func)
hook.removePre(klass, name, func) -> None
  removePre(klass, name, func)
hook.removePre(klass, name, func) -> None

Variable Summary
str _ORIG = '__hook_orig_%s_%s_%s__'
str _POST = '__hook_post_%s_%s_%s__'
str _PRE = '__hook_pre_%s_%s_%s__'
str hooked_func = '\n\nimport %(module)s\n\ndef %(name)s(*ar...

Function Details

_addHook(klass, name, phase, func)

(private) adds a hook to a method on a class

_dehook(klass, name)

(private) causes a certain method name no longer to be hooked on a class

_enhook(klass, name)

(private) causes a certain method name to be hooked on a class

_removeHook(klass, name, phase, func)

(private) removes a hook from a method on a class

_XXX(k, n, s)

string manipulation garbage

addPost(klass, name, func)

hook.addPost(klass, name, func) -> None

Add a function to be called before the method klass.name is invoked.

addPre(klass, name, func)

hook.addPre(klass, name, func) -> None

Add a function to be called before the method klass.name is invoked.

ORIG(k, n)

(private) munging to turn a method name into an `original' identifier

POST(k, n)

(private) munging to turn a method name into a post-hook-method-name

PRE(k, n)

(private) munging to turn a method name into a pre-hook-method-name

removePost(klass, name, func)

hook.removePre(klass, name, func) -> None

Remove a function (previously registered with addPost) so that it is no longer executed after klass.name.

removePre(klass, name, func)

hook.removePre(klass, name, func) -> None

Remove a function (previously registered with addPre) so that it is no longer executed before klass.name.

Variable Details

_ORIG

Type:
str
Value:
'__hook_orig_%s_%s_%s__'                                               

_POST

Type:
str
Value:
'__hook_post_%s_%s_%s__'                                               

_PRE

Type:
str
Value:
'__hook_pre_%s_%s_%s__'                                                

hooked_func

Type:
str
Value:
'''

import %(module)s

def %(name)s(*args, **kw):
    klazz = %(module)s.%(klass)s
    for preMethod in klazz.%(preName)s:
        preMethod(*args, **kw)
...                                                                    

Generated by Epydoc 2.0 on Sat May 15 20:08:47 2004 http://epydoc.sf.net