routing.py 965 B

12345678910111213141516171819202122232425262728293031323334353637
  1. """Project path routing: relative paths are resolved from the repository root (this file's directory).
  2. 中文「路由」在此指:把 ``input/screenshot.png`` 等相对路径统一映射到仓库根下的绝对路径。
  3. Example::
  4. from routing import REPO_ROOT, route, INPUT_DIR, OUTPUT_DIR
  5. shot = route(\"input/screenshot.png\")
  6. same = INPUT_DIR / \"screenshot.png\"
  7. assert shot.resolve() == same.resolve()
  8. """
  9. from __future__ import annotations
  10. from pathlib import Path
  11. REPO_ROOT: Path = Path(__file__).resolve().parent
  12. INPUT_DIR: Path = REPO_ROOT / "input"
  13. OUTPUT_DIR: Path = REPO_ROOT / "output"
  14. def route(path: str | Path) -> Path:
  15. """Resolve ``path`` under ``REPO_ROOT`` when relative; absolute paths stay absolute."""
  16. p = Path(path)
  17. return p.resolve() if p.is_absolute() else (REPO_ROOT / p).resolve()
  18. repo_path = route
  19. __all__ = [
  20. "REPO_ROOT",
  21. "INPUT_DIR",
  22. "OUTPUT_DIR",
  23. "route",
  24. "repo_path",
  25. ]