File size: 959 Bytes
593e08a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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