1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
from __future__ import unicode_literals
import logging
import time
import types
from django.utils.importlib import import_module
def post_dispatch(func):
def wrapped(callback):
register_hook(func, 'after', callback)
return callback
return wrapped
def pre_dispatch(func):
def wrapped(callback):
register_hook(func, 'before', callback)
return callback
return wrapped
def replace_call(func):
def inner(callback):
def wrapped(*args, **kwargs):
return callback(func, *args, **kwargs)
actual = getattr(func, '__wrapped__', func)
wrapped.__wrapped__ = actual
wrapped.__doc__ = getattr(actual, '__doc__', None)
wrapped.__name__ = actual.__name__
_replace_function(func, wrapped)
return wrapped
return inner
def fire_hook(hook, sender, **kwargs):
try:
for callback in callbacks[hook].get(id(sender), []):
callback(sender=sender, **kwargs)
except Exception as e:
# Log the exception, dont mess w/ the underlying function
logging.exception(e)
def _replace_function(func, wrapped):
if isinstance(func, types.FunctionType):
if func.__module__ == '__builtin__':
# oh shit
__builtins__[func] = wrapped
else:
module = import_module(func.__module__)
setattr(module, func.__name__, wrapped)
elif getattr(func, '__self__', None):
setattr(func.__self__, func.__name__, classmethod(wrapped))
elif hasattr(func, 'im_class'):
# for unbound methods - Python 2 only
setattr(func.im_class, func.__name__, wrapped)
else:
raise NotImplementedError
callbacks = {
'before': {},
'after': {},
}
def register_hook(func, hook, callback):
"""
def myhook(sender, args, kwargs):
print func, "executed
print "args:", args
print "kwargs:", kwargs
register_hook(BaseDatabaseWrapper.cursor, 'before', myhook)
"""
assert hook in ('before', 'after')
def wrapped(*args, **kwargs):
start = time.time()
fire_hook('before', sender=wrapped.__wrapped__, args=args, kwargs=kwargs,
start=start)
result = wrapped.__wrapped__(*args, **kwargs)
stop = time.time()
fire_hook('after', sender=wrapped.__wrapped__, args=args, kwargs=kwargs,
result=result, start=start, stop=stop)
actual = getattr(func, '__wrapped__', func)
wrapped.__wrapped__ = actual
wrapped.__doc__ = getattr(actual, '__doc__', None)
wrapped.__name__ = actual.__name__
id_ = id(actual)
if id_ not in callbacks[hook]:
callbacks[hook][id_] = []
callbacks[hook][id_].append(callback)
_replace_function(func, wrapped)
|