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 Exception('Not an decoratable function: %s' % origin.name)
\r
43 origin.decorate(fn, needs_origin)
\r