Afgangsbevis-generator / services /data_loader.py
danielhjerresen's picture
Upload 13 files
e37e177 verified
import json
from pathlib import Path
from typing import Any
def load_json(path: str) -> Any:
file_path = Path(path)
if not file_path.exists():
raise FileNotFoundError(f"Filen findes ikke: {path}")
with open(file_path, "r", encoding="utf-8") as f:
return json.load(f)
def load_text(path: str) -> str:
file_path = Path(path)
if not file_path.exists():
raise FileNotFoundError(f"Filen findes ikke: {path}")
return file_path.read_text(encoding="utf-8")
def save_generated_output(path: str, item: dict) -> None:
file_path = Path(path)
file_path.parent.mkdir(parents=True, exist_ok=True)
if file_path.exists():
try:
with open(file_path, "r", encoding="utf-8") as f:
data = json.load(f)
except json.JSONDecodeError:
data = []
else:
data = []
data.append(item)
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)