find_module docstring and search for init files instead of directories
This commit is contained in:
parent
e2d672a422
commit
fdbe0205f1
@ -91,7 +91,7 @@ def test_data(request):
|
|||||||
if not plugin_module_entry_point:
|
if not plugin_module_entry_point:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
f"Test case {repr(test_case_name)} has no entry point. "
|
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 (
|
yield (
|
||||||
|
@ -68,13 +68,23 @@ def get_test_case_json_data(test_case_name, json_file_name=None):
|
|||||||
def find_module(
|
def find_module(
|
||||||
module: ModuleType, predicate: Callable[[ModuleType], bool]
|
module: ModuleType, predicate: Callable[[ModuleType], bool]
|
||||||
) -> Optional[ModuleType]:
|
) -> 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):
|
if predicate(module):
|
||||||
return module
|
return module
|
||||||
|
|
||||||
module_path = pathlib.Path(*module.__path__)
|
module_path = pathlib.Path(*module.__path__)
|
||||||
|
|
||||||
for sub in list(module_path.glob("**/")):
|
for sub in list(sub.parent for sub in module_path.glob("**/__init__.py")):
|
||||||
if not sub.is_dir() or sub == module_path:
|
if sub == module_path:
|
||||||
continue
|
continue
|
||||||
sub_module_path = sub.relative_to(module_path)
|
sub_module_path = sub.relative_to(module_path)
|
||||||
sub_module_name = ".".join(sub_module_path.parts)
|
sub_module_name = ".".join(sub_module_path.parts)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user