from __future__ import annotations import os import shutil from pathlib import Path PROJECT_ROOT = Path(__file__).resolve().parent DEFAULT_PROFILE_DATA = '{\n "profiles": {},\n "active_profile": null\n}\n' def bundle_path(*parts: str) -> Path: base = Path(os.environ.get("MICE_PROJECT_ROOT", PROJECT_ROOT)).expanduser() return base.joinpath(*parts) def resolve_bundle_path(path_like: str | Path) -> Path: candidate = Path(path_like).expanduser() if candidate.is_absolute(): return candidate return bundle_path(*candidate.parts) def data_root() -> Path: override = os.environ.get("MICE_DATA_DIR") if override: root = Path(override).expanduser() else: xdg_data_home = Path(os.environ.get("XDG_DATA_HOME", Path.home() / ".local" / "share")) root = xdg_data_home / "mice" root.mkdir(parents=True, exist_ok=True) return root def persistent_data_path( path_like: str | Path, *, default_source: str | Path | None = None, default_text: str = "", ) -> Path: candidate = Path(path_like).expanduser() if candidate.is_absolute(): target = candidate else: target = data_root().joinpath(*candidate.parts) target.parent.mkdir(parents=True, exist_ok=True) if target.exists(): return target if default_source is not None: source = resolve_bundle_path(default_source) if source.exists() and source != target: shutil.copy2(source, target) return target target.write_text(default_text, encoding="utf-8") return target