| from __future__ import annotations |
|
|
| import json |
| import os |
| from pathlib import Path |
| from typing import Any |
|
|
|
|
| def ensure_dir(path: str | Path) -> Path: |
| p = Path(path) |
| p.mkdir(parents=True, exist_ok=True) |
| return p |
|
|
|
|
| def read_jsonl(path: str | Path) -> list[dict[str, Any]]: |
| rows: list[dict[str, Any]] = [] |
| with open(path, encoding="utf-8") as f: |
| for line in f: |
| line = line.strip() |
| if not line: |
| continue |
| rows.append(json.loads(line)) |
| return rows |
|
|
|
|
| def write_jsonl(path: str | Path, rows: list[dict[str, Any]]) -> None: |
| ensure_dir(Path(path).parent) |
| with open(path, "w", encoding="utf-8") as f: |
| for row in rows: |
| f.write(json.dumps(row, ensure_ascii=False) + "\n") |
|
|
|
|
| def getenv_bool(name: str, default: bool = False) -> bool: |
| v = os.getenv(name) |
| if v is None: |
| return default |
| return v.strip().lower() in {"1", "true", "yes", "y", "on"} |
|
|