budijuarto's picture
Upload src/egg_damage/paths.py
593e08a verified
raw
history blame contribute delete
959 Bytes
from __future__ import annotations
from pathlib import Path
from typing import Any
def project_root() -> Path:
return Path(__file__).resolve().parents[2]
def resolve_path(path_value: str | Path | None, base: Path | None = None) -> Path | None:
if path_value in (None, ""):
return None
path = Path(path_value).expanduser()
if path.is_absolute():
return path
return (base or project_root()).joinpath(path).resolve()
def ensure_dir(path: str | Path) -> Path:
resolved = Path(path)
resolved.mkdir(parents=True, exist_ok=True)
return resolved
def as_posix_str(path: str | Path) -> str:
return Path(path).resolve().as_posix()
def configured_path(config: dict[str, Any], key: str) -> Path:
value = config.get("paths", {}).get(key)
resolved = resolve_path(value, project_root())
if resolved is None:
raise ValueError(f"Missing required path config: paths.{key}")
return resolved