Files
Medios-Macina/medeia_entry.py

62 lines
2.0 KiB
Python
Raw Normal View History

2025-12-24 02:13:21 -08:00
"""Entry point wrapper for Medeia-Macina CLI.
This file is intentionally backwards-compatible. When installed from the
packaged distribution the preferred entry is `medeia_macina.cli_entry.main`.
When running from the repository (or in legacy installs) the module will
attempt to import `MedeiaCLI` from the top-level `CLI` module.
"""
2025-12-29 17:05:03 -08:00
2025-11-25 20:09:33 -08:00
import sys
from pathlib import Path
2025-12-24 02:13:21 -08:00
def _run_packaged_entry(argv=None) -> int:
"""Try to delegate to the packaged entry (`medeia_macina.cli_entry:main`)."""
try:
from medeia_macina.cli_entry import main as _main
return int(_main(argv) or 0)
except Exception:
return -1
def _run_legacy_entry() -> None:
"""Legacy behaviour: make repo root importable and run CLI.
This supports running directly from the source tree where `CLI.py` is
available as a top-level module.
"""
root_dir = Path(__file__).resolve().parent
if str(root_dir) not in sys.path:
sys.path.insert(0, str(root_dir))
try:
from CLI import MedeiaCLI
except Exception as exc: # pragma: no cover - user environment issues
raise ImportError(
"Could not import 'MedeiaCLI' from top-level 'CLI'. "
"If you installed the package into a virtualenv, activate it and run: \n"
" pip install -e .\n"
"or re-run the project bootstrap to ensure an up-to-date install."
) from exc
if __name__ == "__main__":
MedeiaCLI().run()
# Backward-compatibility: try to expose `MedeiaCLI` at import-time when the
# project is being used from a development checkout (so modules that import
# the top-level `medeia_entry` can still access the CLI class).
try:
from CLI import MedeiaCLI as MedeiaCLI # type: ignore
except Exception:
# It's okay if the legacy top-level CLI isn't importable in installed packages.
pass
2025-11-25 20:09:33 -08:00
if __name__ == "__main__":
2025-12-24 02:13:21 -08:00
rc = _run_packaged_entry(sys.argv[1:])
if rc >= 0:
raise SystemExit(rc)
# Fall back to legacy import when packaged entry couldn't be invoked.
_run_legacy_entry()