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
51 args, kwargs = dec(*args, **kwargs)
\r
52 except ReturnImediatelyException, e:
\r
55 res = self._callable(*args, **kwargs)
\r
57 if self._result_decoration:
\r
58 for dec in self._result_decoration:
\r
59 if dec._needs_params:
\r
60 res = dec(res, *args, **kwargs)
\r
66 class ReturnImediatelyException(Exception):
\r
67 def __init__(self, ret):
\r
68 super(Exception, self).__init__()
\r
71 def _check_decoratable(origin, install=True):
\r
72 if not isinstance(origin, DecoratableObject):
\r
73 if inspect.ismethod(origin) and not hasattr(origin, '_decoratable_obj'):
\r
74 decoratable = DecoratableObject(origin)
\r
76 def decoratable_method(self, *args, **kwargs):
\r
77 return decoratable(self, *args, **kwargs)
\r
79 decoratable_method._decoratable_obj = decoratable
\r
81 def decoratable_decorate(fn, mode, **kwargs):
\r
82 decoratable._decorate(fn, mode, **kwargs)
\r
84 decoratable_method._decorate = decoratable_decorate
\r
87 setattr(origin.im_class, origin.__name__, decoratable_method)
\r
89 return decoratable_method
\r
91 elif inspect.isfunction(origin):
\r
92 decoratable = DecoratableObject(origin)
\r
95 setattr(inspect.getmodule(origin), origin.__name__, decoratable)
\r
102 def decorate(origin, needs_origin=True):
\r
103 origin = _check_decoratable(origin)
\r
106 origin._decorate(fn, DecoratableObject.MODE_OVERRIDE, needs_origin=needs_origin)
\r
111 def _decorate_params(origin):
\r
112 origin = _check_decoratable(origin)
\r
115 origin._decorate(fn, DecoratableObject.MODE_PARAMS)
\r
119 decorate.params = _decorate_params
\r
121 def _decorate_result(origin, needs_params=False):
\r
122 origin = _check_decoratable(origin)
\r
125 origin._decorate(fn, DecoratableObject.MODE_RESULT, needs_params=needs_params)
\r
129 decorate.result = _decorate_result
\r
131 def _decorate_with(fn):
\r
132 def decorator(origin):
\r
133 origin = _check_decoratable(origin)
\r
134 origin._decorate(fn, DecoratableObject.MODE_OVERRIDE, needs_origin=True)
\r
138 decorate.withfn = _decorate_with
\r
140 def _decorate_result_with(fn, needs_params=False):
\r
141 def decorator(origin):
\r
142 origin = _check_decoratable(origin)
\r
143 origin._decorate(fn, DecoratableObject.MODE_RESULT, needs_params=needs_params)
\r
147 decorate.result.withfn = _decorate_result_with
\r
149 def _decorate_params_with(fn):
\r
150 def decorator(origin):
\r
151 origin = _check_decoratable(origin)
\r
152 origin._decorate(fn, DecoratableObject.MODE_PARAMS)
\r
156 decorate.params.withfn = _decorate_params_with