| 1 |
"""Centralized JSON file type detection and loading for duct.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
import json |
| 5 |
from typing import Any |
| 6 |
from con_duct._constants import SUFFIXES |
| 7 |
|
| 8 |
|
| 9 |
JSONL_SUFFIXES = (SUFFIXES["usage"], SUFFIXES["usage_legacy"]) |
| 10 |
|
| 11 |
|
| 12 |
def is_jsonl_file(path: str) -> bool: |
| 13 |
"""Check if path is a JSON Lines file.""" |
| 14 |
if any(path.endswith(s) for s in JSONL_SUFFIXES): |
| 15 |
return True |
| 16 |
return path.endswith(".jsonl") |
| 17 |
|
| 18 |
|
| 19 |
def is_info_file(path: str) -> bool: |
| 20 |
"""Check if path is a duct info file (standard JSON).""" |
| 21 |
return path.endswith(SUFFIXES["info"]) |
| 22 |
|
| 23 |
|
| 24 |
def load_usage_file(path: str) -> list[dict[str, Any]]: |
| 25 |
"""Load a duct usage file (JSON Lines format).""" |
| 26 |
with open(path, "r") as f: |
| 27 |
return [json.loads(line) for line in f if line.strip()] |
| 28 |
|
| 29 |
|
| 30 |
def load_info_file(path: str) -> dict[str, Any]: |
| 31 |
"""Load a duct info file (standard JSON).""" |
| 32 |
with open(path, "r") as f: |
| 33 |
result: dict[str, Any] = json.load(f) |
| 34 |
return result |