Spaces:
Sleeping
Sleeping
Update utils/io_utils.py
Browse files- utils/io_utils.py +32 -0
utils/io_utils.py
CHANGED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Utility helpers: logging setup, path handling, JSON read/write."""
|
2 |
+
|
3 |
+
from pathlib import Path
|
4 |
+
import json
|
5 |
+
import logging
|
6 |
+
|
7 |
+
ROOT = Path(__file__).resolve().parents[1]
|
8 |
+
LOGS_DIR = ROOT / "logs"
|
9 |
+
LOGS_DIR.mkdir(exist_ok=True)
|
10 |
+
|
11 |
+
logging.basicConfig(
|
12 |
+
filename=LOGS_DIR / "drs.log",
|
13 |
+
level=logging.INFO,
|
14 |
+
format="%(asctime)s [%(levelname)s] %(message)s",
|
15 |
+
)
|
16 |
+
logger = logging.getLogger("drs")
|
17 |
+
|
18 |
+
|
19 |
+
def ensure_dir(path: Path):
|
20 |
+
"""Create directory if absent."""
|
21 |
+
path.mkdir(parents=True, exist_ok=True)
|
22 |
+
|
23 |
+
|
24 |
+
def read_json(fp: Path):
|
25 |
+
with open(fp, "r", encoding="utf-8") as f:
|
26 |
+
return json.load(f)
|
27 |
+
|
28 |
+
|
29 |
+
def write_json(obj, fp: Path):
|
30 |
+
ensure_dir(fp.parent)
|
31 |
+
with open(fp, "w", encoding="utf-8") as f:
|
32 |
+
json.dump(obj, f, indent=2)
|