3 class DecoratableObject(object):
\r
4 def __init__(self, fn):
\r
7 def decorate(self, fn, needs_origin):
\r
8 origin = self._callable
\r
11 self._callable = lambda *args, **kwargs: fn(origin, *args, **kwargs)
\r
13 self._callable = lambda *args, **kwargs: fn(*args, **kwargs)
\r
15 def __call__(self, *args, **kwargs):
\r
16 return self._callable(*args, **kwargs)
\r
19 def decoratable(fn):
\r
20 return DecoratableObject(fn)
\r
22 def decoratable_method(fn):
\r
23 obj = DecoratableObject(fn)
\r
24 def decorated(self, *args, **kwargs):
\r
25 return obj(self, *args, **kwargs)
\r
27 decorated.__obj = obj
\r
30 decoratable.method = decoratable_method
\r
32 def decorate(origin, needs_origin=True):
\r
33 if not isinstance(origin, DecoratableObject):
\r
34 if hasattr(origin, '__obj'):
\r
36 origin.__obj.decorate(fn, needs_origin)
\r
40 raise TypeError('Not a decoratable function: %s' % origin.__name__)
\r
43 origin.decorate(fn, needs_origin)
\r
49 def decorate_all(module):
\r
50 [setattr(module, n, decoratable(f)) for n, f in
\r
51 [(n, getattr(module, n)) for n in dir(module)]
\r
52 if (callable(f)) and (not inspect.isclass(f)) and (f.__module__ == module.__name__)]
\r