find_module docstring and search for init files instead of directories

This commit is contained in:
boukeversteegh 2020-06-14 22:54:03 +02:00
parent e2d672a422
commit fdbe0205f1
2 changed files with 13 additions and 3 deletions

View File

@ -91,7 +91,7 @@ def test_data(request):
if not plugin_module_entry_point:
raise Exception(
f"Test case {repr(test_case_name)} has no entry point. "
+ "Please add a proto message or service called Test and recompile."
"Please add a proto message or service called Test and recompile."
)
yield (

View File

@ -68,13 +68,23 @@ def get_test_case_json_data(test_case_name, json_file_name=None):
def find_module(
module: ModuleType, predicate: Callable[[ModuleType], bool]
) -> Optional[ModuleType]:
"""
Recursively search module tree for a module that matches the search predicate.
Assumes that the submodules are directories containing __init__.py.
Example:
# find module inside foo that contains Test
import foo
test_module = find_module(foo, lambda m: hasattr(m, 'Test'))
"""
if predicate(module):
return module
module_path = pathlib.Path(*module.__path__)
for sub in list(module_path.glob("**/")):
if not sub.is_dir() or sub == module_path:
for sub in list(sub.parent for sub in module_path.glob("**/__init__.py")):
if sub == module_path:
continue
sub_module_path = sub.relative_to(module_path)
sub_module_name = ".".join(sub_module_path.parts)