File size: 748 Bytes
d3ddf55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
"""Utility helpers: logging setup, path handling, JSON read/write."""

from pathlib import Path
import json
import logging

ROOT = Path(__file__).resolve().parents[1]
LOGS_DIR = ROOT / "logs"
LOGS_DIR.mkdir(exist_ok=True)

logging.basicConfig(
    filename=LOGS_DIR / "drs.log",
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(message)s",
)
logger = logging.getLogger("drs")


def ensure_dir(path: Path):
    """Create directory if absent."""
    path.mkdir(parents=True, exist_ok=True)


def read_json(fp: Path):
    with open(fp, "r", encoding="utf-8") as f:
        return json.load(f)


def write_json(obj, fp: Path):
    ensure_dir(fp.parent)
    with open(fp, "w", encoding="utf-8") as f:
        json.dump(obj, f, indent=2)