budijuarto commited on
Commit
593e08a
·
verified ·
1 Parent(s): c02cb32

Upload src/egg_damage/paths.py

Browse files
Files changed (1) hide show
  1. src/egg_damage/paths.py +36 -0
src/egg_damage/paths.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import Any
5
+
6
+
7
+ def project_root() -> Path:
8
+ return Path(__file__).resolve().parents[2]
9
+
10
+
11
+ def resolve_path(path_value: str | Path | None, base: Path | None = None) -> Path | None:
12
+ if path_value in (None, ""):
13
+ return None
14
+ path = Path(path_value).expanduser()
15
+ if path.is_absolute():
16
+ return path
17
+ return (base or project_root()).joinpath(path).resolve()
18
+
19
+
20
+ def ensure_dir(path: str | Path) -> Path:
21
+ resolved = Path(path)
22
+ resolved.mkdir(parents=True, exist_ok=True)
23
+ return resolved
24
+
25
+
26
+ def as_posix_str(path: str | Path) -> str:
27
+ return Path(path).resolve().as_posix()
28
+
29
+
30
+ def configured_path(config: dict[str, Any], key: str) -> Path:
31
+ value = config.get("paths", {}).get(key)
32
+ resolved = resolve_path(value, project_root())
33
+ if resolved is None:
34
+ raise ValueError(f"Missing required path config: paths.{key}")
35
+ return resolved
36
+