-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgenerate_module_shortcuts.py
More file actions
executable file
·59 lines (42 loc) · 1.77 KB
/
generate_module_shortcuts.py
File metadata and controls
executable file
·59 lines (42 loc) · 1.77 KB
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
54
55
56
57
58
59
#!/usr/bin/env python3
from __future__ import annotations
import importlib
import inspect
import json
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from types import ModuleType
def get_module_shortcuts(module: ModuleType, parent_classes: list | None = None) -> dict:
"""Traverse a module and its submodules to identify and register shortcuts for classes."""
shortcuts = {}
if parent_classes is None:
parent_classes = []
parent_module_name = '.'.join(module.__name__.split('.')[:-1])
module_classes = []
for classname, cls in inspect.getmembers(module, inspect.isclass):
module_classes.append(cls)
if cls in parent_classes:
shortcuts[f'{module.__name__}.{classname}'] = f'{parent_module_name}.{classname}'
for _, submodule in inspect.getmembers(module, inspect.ismodule):
if submodule.__name__.startswith('apify'):
shortcuts.update(get_module_shortcuts(submodule, module_classes))
return shortcuts
def resolve_shortcuts(shortcuts: dict) -> None:
"""Resolve linked shortcuts.
For example, if there are shortcuts A -> B and B -> C, resolve them to A -> C.
"""
for source, target in shortcuts.items():
while target in shortcuts:
shortcuts[source] = shortcuts[target]
target = shortcuts[target] # noqa: PLW2901
shortcuts = {}
for module_name in ['apify', 'apify_client']:
try:
module = importlib.import_module(module_name)
module_shortcuts = get_module_shortcuts(module)
shortcuts.update(module_shortcuts)
except ModuleNotFoundError:
pass
resolve_shortcuts(shortcuts)
with open('module_shortcuts.json', 'w', encoding='utf-8') as shortcuts_file:
json.dump(shortcuts, shortcuts_file, indent=4, sort_keys=True)