36 lines
851 B
Python
36 lines
851 B
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import TYPE_CHECKING
|
||
|
|
|
||
|
|
if TYPE_CHECKING:
|
||
|
|
from collections.abc import MutableMapping
|
||
|
|
from typing import Any, Iterable
|
||
|
|
|
||
|
|
def detect_modules(
|
||
|
|
package_name: str,
|
||
|
|
package_path: Iterable[str],
|
||
|
|
namespace: MutableMapping[str, Any],
|
||
|
|
prefix: str,
|
||
|
|
skip: set[str] | None = None,
|
||
|
|
) -> list[str]:
|
||
|
|
|
||
|
|
import importlib
|
||
|
|
import pkgutil
|
||
|
|
|
||
|
|
ret: list[str] = []
|
||
|
|
skip = skip or set()
|
||
|
|
|
||
|
|
for _finder, module_name, _ispkg in pkgutil.iter_modules(package_path):
|
||
|
|
if not module_name.startswith(prefix):
|
||
|
|
continue
|
||
|
|
|
||
|
|
if module_name in skip:
|
||
|
|
continue
|
||
|
|
|
||
|
|
module = importlib.import_module(f'.{module_name}', package_name)
|
||
|
|
cls = getattr(module, module_name)
|
||
|
|
|
||
|
|
namespace[module_name] = cls
|
||
|
|
ret.append(module_name)
|
||
|
|
|
||
|
|
return ret
|