3 class DecoratableObject(object):
\r
8 def __init__(self, fn):
\r
9 a = inspect.getargspec(fn)
\r
11 self._params_decoration = None
\r
12 self._result_decoration = None
\r
14 def _decorate(self, fn, needs_origin, method=False):
\r
15 origin = self._callable
\r
19 self._callable = lambda inst, *args, **kwargs: fn(inst, origin, *args, **kwargs)
\r
21 self._callable = lambda *args, **kwargs: fn(origin, *args, **kwargs)
\r
25 def _decorate_params(self, fn):
\r
26 if not self._params_decoration:
\r
27 self._params_decoration = []
\r
29 self._params_decoration.append(fn)
\r
31 def _decorate_result(self, fn):
\r
32 if not self._result_decoration:
\r
33 self._result_decoration = []
\r
35 self._result_decoration.append(fn)
\r
37 def __call__(self, *args, **kwargs):
\r
38 if self._params_decoration:
\r
39 for dec in self._params_decoration:
\r
40 args, kwargs = dec(*args, **kwargs)
\r
42 res = self._callable(*args, **kwargs)
\r
44 if self._result_decoration:
\r
45 for dec in self._result_decoration:
\r
51 def _create_decorator(origin, needs_origin, mode, method=False):
\r
53 if mode == DecoratableObject.MODE_OVERRIDE:
\r
54 origin._decorate(fn, needs_origin, method=method)
\r
55 elif mode == DecoratableObject.MODE_PARAMS:
\r
56 origin._decorate_params(fn)
\r
57 elif mode == DecoratableObject.MODE_RESULT:
\r
58 origin._decorate_result(fn)
\r
65 def _decorate_method(origin, needs_origin, mode):
\r
66 if not hasattr(origin, '_decoratable_obj'):
\r
67 name = origin.__name__
\r
68 cls = origin.im_class
\r
70 decoratable = DecoratableObject(origin)
\r
72 def decoratable_method(self, *args, **kwargs):
\r
73 return decoratable(self, *args, **kwargs)
\r
75 decoratable_method._decoratable_obj = decoratable
\r
76 setattr(cls, name, decoratable_method)
\r
78 decoratable = origin._decoratable_obj
\r
80 return _create_decorator(decoratable, needs_origin, mode, method=True)
\r
82 def _decorate_function(origin, needs_origin, mode):
\r
83 if not isinstance(origin, DecoratableObject):
\r
84 mod = inspect.getmodule(origin)
\r
86 name = origin.__name__
\r
87 origin = DecoratableObject(origin)
\r
88 setattr(mod, name, origin)
\r
90 return _create_decorator(origin, needs_origin, mode)
\r
93 def decorate(origin, needs_origin=True, mode=DecoratableObject.MODE_OVERRIDE):
\r
94 if inspect.ismethod(origin):
\r
95 return _decorate_method(origin, needs_origin, mode)
\r
97 if inspect.isfunction(origin) or isinstance(origin, DecoratableObject):
\r
98 return _decorate_function(origin, needs_origin, mode)
\r
104 def _decorate_params(origin):
\r
105 return decorate(origin, mode=DecoratableObject.MODE_PARAMS)
\r
107 decorate.params = _decorate_params
\r
109 def _decorate_result(origin):
\r
110 return decorate(origin, mode=DecoratableObject.MODE_RESULT)
\r
112 decorate.result = _decorate_result
\r