2026-05-30 12:44:55 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-06-05 07:22:54 +02:00
|
|
|
import pkgutil
|
|
|
|
|
from importlib import import_module
|
2026-05-30 12:44:55 +02:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from collections.abc import MutableMapping
|
2026-06-05 07:22:54 +02:00
|
|
|
from typing import Any
|
2026-05-30 12:44:55 +02:00
|
|
|
|
|
|
|
|
def detect_modules(
|
|
|
|
|
namespace: MutableMapping[str, Any],
|
2026-06-05 07:22:54 +02:00
|
|
|
prefix: str | None = None,
|
2026-05-30 12:44:55 +02:00
|
|
|
skip: set[str] | None = None,
|
2026-06-05 07:22:54 +02:00
|
|
|
*,
|
|
|
|
|
extend_namespace: bool = True,
|
2026-05-30 12:44:55 +02:00
|
|
|
) -> list[str]:
|
|
|
|
|
|
2026-06-05 07:22:54 +02:00
|
|
|
package_name = namespace.get("__name__")
|
|
|
|
|
package_path = namespace.get("__path__")
|
|
|
|
|
|
|
|
|
|
if not isinstance(package_name, str):
|
|
|
|
|
raise TypeError("namespace must contain string __name__")
|
|
|
|
|
|
|
|
|
|
if package_path is None:
|
|
|
|
|
raise TypeError("namespace must be a package namespace with __path__")
|
|
|
|
|
|
|
|
|
|
if extend_namespace:
|
|
|
|
|
package_path = pkgutil.extend_path(package_path, package_name)
|
|
|
|
|
namespace["__path__"] = package_path
|
2026-05-30 12:44:55 +02:00
|
|
|
|
|
|
|
|
ret: list[str] = []
|
|
|
|
|
skip = skip or set()
|
|
|
|
|
|
|
|
|
|
for _finder, module_name, _ispkg in pkgutil.iter_modules(package_path):
|
2026-06-05 07:22:54 +02:00
|
|
|
if prefix is not None and not module_name.startswith(prefix):
|
2026-05-30 12:44:55 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if module_name in skip:
|
|
|
|
|
continue
|
|
|
|
|
|
2026-06-05 07:22:54 +02:00
|
|
|
module = import_module(f".{module_name}", package_name)
|
2026-05-30 12:44:55 +02:00
|
|
|
cls = getattr(module, module_name)
|
|
|
|
|
|
|
|
|
|
namespace[module_name] = cls
|
|
|
|
|
ret.append(module_name)
|
|
|
|
|
|
|
|
|
|
return ret
|