Not all __init__.py modules are generated by python-tools.sh, some are needed early to make jw-pkg useful without generation, notably in jw.pkg.cmds.
Add detect_modules() to unify that detection, and place it into a minimal module lib.init to not increase startup time cost.
Signed-off-by: Jan Lindemann <jan@janware.com>
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
|