3 class DecoratableObject(object):
8 def __init__(self, fn, is_method=False):
10 self.is_method = is_method
12 self._params_decoration = None
13 self._result_decoration = None
15 def _decorate(self, fn, mode, **kwargs):
16 if mode == self.MODE_OVERRIDE:
17 self._decorate_full(fn, **kwargs)
18 elif mode == self.MODE_PARAMS:
19 self._decorate_params(fn)
20 elif mode == self.MODE_RESULT:
21 self._decorate_result(fn, **kwargs)
23 def _decorate_full(self, fn, needs_origin=True):
24 origin = self._callable
28 self._callable = lambda inst, *args, **kwargs: fn(inst, origin, *args, **kwargs)
30 self._callable = lambda *args, **kwargs: fn(origin, *args, **kwargs)
34 def _decorate_params(self, fn):
35 if not self._params_decoration:
36 self._params_decoration = []
38 self._params_decoration.append(fn)
40 def _decorate_result(self, fn, needs_params=False):
41 if not self._result_decoration:
42 self._result_decoration = []
44 fn._needs_params = needs_params
45 self._result_decoration.append(fn)
47 def __call__(self, *args, **kwargs):
48 if self._params_decoration:
49 for dec in self._params_decoration:
51 args, kwargs = dec(*args, **kwargs)
52 except ReturnImediatelyException, e:
55 res = self._callable(*args, **kwargs)
57 if self._result_decoration:
58 for dec in self._result_decoration:
60 res = dec(res, *args, **kwargs)
66 class ReturnImediatelyException(Exception):
67 def __init__(self, ret):
68 super(Exception, self).__init__()
71 def _check_decoratable(origin, install=True):
72 if not hasattr(origin, '_decoratable_obj'):
73 if inspect.ismethod(origin) and not hasattr(origin, '_decoratable_obj'):
74 decoratable = DecoratableObject(origin)
76 def decoratable_method(self, *args, **kwargs):
77 return decoratable(self, *args, **kwargs)
79 decoratable_method._decoratable_obj = decoratable
81 def decoratable_decorate(fn, mode, **kwargs):
82 decoratable._decorate(fn, mode, **kwargs)
84 decoratable_method._decorate = decoratable_decorate
87 setattr(origin.im_class, origin.__name__, decoratable_method)
89 return decoratable_method
91 elif inspect.isfunction(origin):
92 decoratable = DecoratableObject(origin)
94 def decorated(*args, **kwargs):
95 return decoratable(*args, **kwargs)
97 decorated._decoratable_obj = decoratable
100 setattr(inspect.getmodule(origin), origin.__name__, decorated)
102 decorated.__name__ = origin.__name__
103 decorated.__module__ = origin.__module__
110 def decorate(origin, needs_origin=True):
111 origin = _check_decoratable(origin)
114 origin._decoratable_obj._decorate(fn, DecoratableObject.MODE_OVERRIDE, needs_origin=needs_origin)
119 def _decorate_params(origin):
120 origin = _check_decoratable(origin)
123 origin._decoratable_obj._decorate(fn, DecoratableObject.MODE_PARAMS)
127 decorate.params = _decorate_params
129 def _decorate_result(origin, needs_params=False):
130 origin = _check_decoratable(origin)
133 origin._decoratable_obj._decorate(fn, DecoratableObject.MODE_RESULT, needs_params=needs_params)
137 decorate.result = _decorate_result
139 def _decorate_with(fn):
140 def decorator(origin):
141 origin = _check_decoratable(origin)
142 origin._decoratable_obj._decorate(fn, DecoratableObject.MODE_OVERRIDE, needs_origin=True)
146 decorate.withfn = _decorate_with
148 def _decorate_result_with(fn, needs_params=False):
149 def decorator(origin):
150 origin = _check_decoratable(origin)
151 origin._decoratable_obj._decorate(fn, DecoratableObject.MODE_RESULT, needs_params=needs_params)
155 decorate.result.withfn = _decorate_result_with
157 def _decorate_params_with(fn):
158 def decorator(origin):
159 origin = _check_decoratable(origin)
160 origin._decoratable_obj._decorate(fn, DecoratableObject.MODE_PARAMS)
164 decorate.params.withfn = _decorate_params_with