|
5 | 5 | from inspect import getmembers, isfunction, ismethod, ismodule, isclass
|
6 | 6 |
|
7 | 7 |
|
| 8 | +def is_module_in_package(object): |
| 9 | + """Predicate to determine if an object is a module within the project's top level package""" |
| 10 | + return ismodule(object) and object.__name__.startswith(TDKMethLexer.TOP_LEVEL) |
| 11 | + |
| 12 | + |
| 13 | +def is_class_in_package(object): |
| 14 | + """Predicate to determine if an object is a class within the project's top level package""" |
| 15 | + return isclass(object) and object.__module__.startswith(TDKMethLexer.TOP_LEVEL) |
| 16 | + |
| 17 | + |
8 | 18 | def get_pkg_funcs(pkg_module: types.ModuleType, funcs_meths=set(), processed_modules=set()):
|
9 | 19 | funcs_meths.update(get_funcs(pkg_module))
|
10 | 20 | processed_modules.add(pkg_module)
|
11 | 21 |
|
12 |
| - for class_name, _class in getmembers(pkg_module, isclass): |
| 22 | + for class_name, _class in getmembers(pkg_module, is_class_in_package): |
13 | 23 | funcs_meths.update(get_funcs(_class))
|
14 | 24 |
|
15 |
| - for mod_name, mod in getmembers(pkg_module, ismodule): |
| 25 | + for mod_name, mod in getmembers(pkg_module, is_module_in_package): |
16 | 26 | if mod in processed_modules:
|
17 | 27 | continue
|
18 | 28 |
|
19 | 29 | try:
|
20 |
| - is_pkg = Path(mod.__file__).name == '__init__.py' |
21 |
| - except AttributeError: # It's a built-in module |
22 |
| - is_pkg = False |
| 30 | + mod_path = Path(mod.__file__) |
| 31 | + except AttributeError: |
| 32 | + continue # It's a built-in module |
23 | 33 |
|
24 |
| - if not is_pkg: # If it's not a subpackage, get all funcs/meths defined in it |
| 34 | + if mod_path.name == '__init__.py': # If it's a subpackage, call recursively to process all submodules |
| 35 | + get_pkg_funcs(mod, funcs_meths, processed_modules) |
| 36 | + |
| 37 | + else: # If it's not a subpackage, get all funcs/meths defined in it |
25 | 38 | funcs_meths.update(get_funcs(mod))
|
26 | 39 | processed_modules.add(mod)
|
27 | 40 |
|
28 |
| - else: # If it's a subpackage, call recursively to process all submodules |
29 |
| - get_pkg_funcs(mod, funcs_meths, processed_modules) |
30 |
| - |
31 | 41 | return funcs_meths
|
32 | 42 |
|
33 | 43 |
|
|
0 commit comments