Hook every sym named something
unknown
python
3 years ago
1.1 kB
15
Indexable
def pre_hook_func(func, hook):
@functools.wraps(func)
def run(*args, **kwargs):
hook(*args, **kwargs)
return func(*args, **kwargs)
return run
def prehook_sym_func_creator(sym_name, hook):
def func(mod, callables):
if sym_name in callables.keys():
setattr(mod, sym_name, pre_hook_func(getattr(mod, sym_name), hook))
return func
def recurse_mod(mod, apply_func, checked=None):
if checked == None:
checked = []
submodules = [cls for _, cls in mod.__dict__.items() if isinstance(cls, mod.__class__) and cls.__name__ not in checked]
callables = dict([(name, cls) for name, cls in mod.__dict__.items() if callable(cls)])
apply_func(mod, callables)
checked += [sm.__name__ for sm in submodules]
for sm in submodules:
if mod == sm:
continue
recurse_mod(sm, apply_func, checked)
def some_funk_new_func():
print("caca boudin")
import module_to_modify
recurse_mod(module_to_modify, prehook_sym_func_creator("sym_to_modify", some_funk_new_func))
Editor is loading...