-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoupdate_plugins.py
53 lines (37 loc) · 1.56 KB
/
autoupdate_plugins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import difflib
import importlib
import inspect
from typing import Callable
def update_plugin(module_name: str) -> Callable:
"""Auto-update the hooks name.
This decorator allows to keep custom hooks up-to-date with the project.
As custom hooks need to have the **same name** of original hooks, this decorator looks for the most similar
methods names in a given module and update the custom hook name to be the same of the closest one.
Example:
The decorator should be chained and set as second with respect to the `@hook` decorator. For
example ::
@hook()
@update_plugin("module_name")
def custom_hook_with_old_name(args):
# cool stuff
return
Args:
module_name: string name of the module where the original hook is defined.
Returns:
function with the updated name.
"""
# Get Absolute Import
module_path = ".".join(["cat.mad_hatter.core_plugin.hooks", module_name])
# Import the module
module = importlib.import_module(module_path)
# Get all names of the members in hooks module
functions_list = [f[0] for f in inspect.getmembers(module)]
def change_name(func):
# Get the old function name
current_name = func.__name__
# Get the most similar name in the given module
closest_name = difflib.get_close_matches(current_name, functions_list)[0]
# Set the new name to the hook to be updated
setattr(func, '__name__', closest_name)
return func
return change_name