44 lines
1.0 KiB
Python
44 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
from pathlib import Path
|
|
from typing import Any, Dict
|
|
|
|
import yaml
|
|
|
|
|
|
def setup_logging(verbose: bool = False) -> None:
|
|
logging.basicConfig(
|
|
level=logging.DEBUG if verbose else logging.INFO,
|
|
format="%(asctime)s | %(levelname)-8s | %(message)s",
|
|
datefmt="%H:%M:%S",
|
|
)
|
|
|
|
|
|
def load_yaml(path: str | Path) -> Dict[str, Any]:
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
return yaml.safe_load(f) or {}
|
|
|
|
|
|
def ensure_dir(path: str | Path) -> Path:
|
|
p = Path(path)
|
|
p.mkdir(parents=True, exist_ok=True)
|
|
return p
|
|
|
|
|
|
def write_json(path: str | Path, data: Any) -> None:
|
|
p = Path(path)
|
|
ensure_dir(p.parent)
|
|
with open(p, "w", encoding="utf-8") as f:
|
|
json.dump(data, f, ensure_ascii=False, indent=2)
|
|
|
|
|
|
def read_json(path: str | Path) -> Any:
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
|
|
|
|
def safe_stem(name: str) -> str:
|
|
return Path(name).stem.replace("/", "_").replace("\\", "_")
|