|
1 | 1 | import types
|
| 2 | +from pathlib import Path |
2 | 3 | from typing import Type, Optional
|
3 | 4 | from pygments.lexers.python import NumPyLexer
|
4 | 5 | from inspect import getmembers, isfunction, ismethod, ismodule, isclass
|
5 | 6 |
|
6 | 7 |
|
7 |
| -def get_pkg_funcs(pkg: types.ModuleType): |
8 |
| - # Get funcs/meths defined in pkg.__init__ |
9 |
| - funcs_meths = get_funcs(pkg) |
10 |
| - modules = getmembers(pkg, ismodule) |
11 |
| - # Get funcs/meths of each module in pkg |
12 |
| - for name, module in modules: |
13 |
| - funcs_meths += get_funcs(module) |
14 |
| - classes = getmembers(module, isclass) |
15 |
| - for class_name, _class in classes: |
16 |
| - funcs_meths += get_funcs(_class) |
17 |
| - # Set of all funcs/meths contained in modules used by package |
18 |
| - return set(funcs_meths) |
| 8 | +def get_pkg_funcs(pkg_module: types.ModuleType, funcs_meths=set(), processed_modules=set()): |
| 9 | + funcs_meths.update(get_funcs(pkg_module)) |
| 10 | + processed_modules.add(pkg_module) |
| 11 | + |
| 12 | + for class_name, _class in getmembers(pkg_module, isclass): |
| 13 | + funcs_meths.update(get_funcs(_class)) |
| 14 | + |
| 15 | + for mod_name, mod in getmembers(pkg_module, ismodule): |
| 16 | + if mod in processed_modules: |
| 17 | + continue |
| 18 | + |
| 19 | + try: |
| 20 | + is_pkg = Path(mod.__file__).name == '__init__.py' |
| 21 | + except AttributeError: # It's a built-in module |
| 22 | + is_pkg = False |
| 23 | + |
| 24 | + if not is_pkg: # If it's not a subpackage, get all funcs/meths defined in it |
| 25 | + funcs_meths.update(get_funcs(mod)) |
| 26 | + processed_modules.add(mod) |
| 27 | + |
| 28 | + else: # If it's a subpackage, call recursively to process all submodules |
| 29 | + get_pkg_funcs(mod, funcs_meths, processed_modules) |
| 30 | + |
| 31 | + return funcs_meths |
19 | 32 |
|
20 | 33 |
|
21 | 34 | def get_funcs(of):
|
|
0 commit comments