3 class DecoratableObject(object):
\r
8 def __init__(self, fn, is_method=False):
\r
10 self.is_method = is_method
\r
12 self._params_decoration = None
\r
13 self._result_decoration = None
\r
15 def _decorate(self, fn, mode, **kwargs):
\r
16 if mode == self.MODE_OVERRIDE:
\r
17 self._decorate_full(fn, **kwargs)
\r
18 elif mode == self.MODE_PARAMS:
\r
19 self._decorate_params(fn)
\r
20 elif mode == self.MODE_RESULT:
\r
21 self._decorate_result(fn, **kwargs)
\r
23 def _decorate_full(self, fn, needs_origin=True):
\r
24 origin = self._callable
\r
28 self._callable = lambda inst, *args, **kwargs: fn(inst, origin, *args, **kwargs)
\r
30 self._callable = lambda *args, **kwargs: fn(origin, *args, **kwargs)
\r
34 def _decorate_params(self, fn):
\r
35 if not self._params_decoration:
\r
36 self._params_decoration = []
\r
38 self._params_decoration.append(fn)
\r
40 def _decorate_result(self, fn, needs_params=False):
\r
41 if not self._result_decoration:
\r
42 self._result_decoration = []
\r
44 fn._needs_params = needs_params
\r
45 self._result_decoration.append(fn)
\r
47 def __call__(self, *args, **kwargs):
\r
48 if self._params_decoration:
\r
49 for dec in self._params_decoration:
\r
50 args, kwargs = dec(*args, **kwargs)
\r
52 res = self._callable(*args, **kwargs)
\r
54 if self._result_decoration:
\r
55 for dec in self._result_decoration:
\r
56 if dec._needs_params:
\r
57 res = dec(res, *args, **kwargs)
\r
63 def _check_decoratable(origin, install=True):
\r
64 if not isinstance(origin, DecoratableObject):
\r
65 if inspect.ismethod(origin) and not hasattr(origin, '_decoratable_obj'):
\r
66 decoratable = DecoratableObject(origin)
\r
68 def decoratable_method(self, *args, **kwargs):
\r
69 return decoratable(self, *args, **kwargs)
\r
71 decoratable_method._decoratable_obj = decoratable
\r
73 def decoratable_decorate(fn, mode, **kwargs):
\r
74 decoratable._decorate(fn, mode, **kwargs)
\r
76 decoratable_method._decorate = decoratable_decorate
\r
79 setattr(origin.im_class, origin.__name__, decoratable_method)
\r
81 return decoratable_method
\r
83 elif inspect.isfunction(origin):
\r
84 decoratable = DecoratableObject(origin)
\r
87 setattr(inspect.getmodule(origin), origin.__name__, decoratable)
\r
94 def decorate(origin, needs_origin=True):
\r
95 origin = _check_decoratable(origin)
\r
98 origin._decorate(fn, DecoratableObject.MODE_OVERRIDE, needs_origin=needs_origin)
\r
103 def _decorate_params(origin):
\r
104 origin = _check_decoratable(origin)
\r
107 origin._decorate(fn, DecoratableObject.MODE_PARAMS)
\r
111 decorate.params = _decorate_params
\r
113 def _decorate_result(origin, needs_params=False):
\r
114 origin = _check_decoratable(origin)
\r
117 origin._decorate(fn, DecoratableObject.MODE_RESULT, needs_params=needs_params)
\r
121 decorate.result = _decorate_result
\r
123 def _decorate_with(fn):
\r
124 def decorator(origin):
\r
125 origin = _check_decoratable(origin)
\r
126 origin._decorate(fn, DecoratableObject.MODE_OVERRIDE, needs_origin=True)
\r
130 decorate.withfn = _decorate_with
\r
132 def _decorate_result_with(fn, needs_params=False):
\r
133 def decorator(origin):
\r
134 origin = _check_decoratable(origin)
\r
135 origin._decorate(fn, DecoratableObject.MODE_RESULT, needs_params=needs_params)
\r
139 decorate.result.withfn = _decorate_result_with
\r
141 def _decorate_params_with(fn):
\r
142 def decorator(origin):
\r
143 origin = _check_decoratable(origin)
\r
144 origin._decorate(fn, DecoratableObject.MODE_PARAMS)
\r
148 decorate.params.withfn = _decorate_params_with