-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
31 lines (22 loc) · 892 Bytes
/
config.py
File metadata and controls
31 lines (22 loc) · 892 Bytes
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
from __future__ import annotations
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
import yaml
@dataclass(slots=True)
class LoadedConfig:
raw: dict[str, Any]
path: Path
def load_config(path: str | Path) -> LoadedConfig:
config_path = Path(path)
with config_path.open("r", encoding="utf-8") as handle:
raw = yaml.safe_load(handle) or {}
if not isinstance(raw, dict):
raise ValueError(f"Config at {config_path} must be a mapping.")
return LoadedConfig(raw=raw, path=config_path)
def ensure_directory(path: str | Path, *, allow_existing: bool = True) -> Path:
directory = Path(path)
if directory.exists() and not allow_existing:
raise FileExistsError(f"Refusing to overwrite existing directory: {directory}")
directory.mkdir(parents=True, exist_ok=allow_existing)
return directory