Created
June 19, 2017 21:17
-
-
Save disconnect3d/53418bf94fbdf52d55bdfbf1a89557f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import inspect | |
class Bar: | |
def changelist_view(self, request, extra_content): | |
print("Bar.changelist_view(request={}, extra_content={})".format(request, extra_content)) | |
def change_view(self, request, object_id, form_url, extra_content): | |
print("Bar.change_view(request={}, object_id={}, form_url={}, extra_content={})" | |
.format(request, object_id, form_url, extra_content)) | |
def update_global_ctf(self, ctf): | |
print("Bar.update_global_ctf(ctf={})".format(ctf)) | |
# Intercepted calls are changed so that they execute: | |
# self.update_global_ctf(ctf) | |
# return super().<method>(<rest_args>) | |
def intercepted(method): | |
method.intercepted = True | |
return method | |
class Foo(Bar): | |
@intercepted | |
def changelist_view(self, request, ctf, extra_content=None): | |
pass | |
@intercepted | |
def change_view(self, request, ctf, object_id, form_url='', extra_context=None): | |
pass | |
def not_intercepted(self): | |
return 5 | |
def decorate(cls, method): | |
@functools.wraps(method) | |
def _decorated(self, request, ctf, *a, **kw): | |
self.update_global_ctf(ctf) | |
return getattr(super(cls, self), method.__name__)(request, *a, **kw) | |
return _decorated | |
methods = inspect.getmembers(Foo, predicate=inspect.isfunction) | |
for method_name, method in filter(lambda m: getattr(m[1], 'intercepted', False), methods): | |
#print(method_name, method) | |
decorated = decorate(Foo, method) | |
setattr(Foo, method_name, decorated) | |
Foo().changelist_view(1, 'ctf1', 3) | |
Foo().change_view(1, 'ctf2', 3, 4, 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment