Files
kennethreitz 551aa63f45 Extract shared JSON dir-merge loader; fix kjv.py encoding
Phase 3b. cross_references.py, topics.py, reading_plans.py and data/__init__.py
each hand-rolled the same 'merge every JSON file in a dir, else read a legacy
file' logic. Extracted it to utils/data_access.load_merged_json_dir(); each
loader keeps its own @lru_cache wrapper and just delegates the merge. Also added
the missing encoding='utf-8' to kjv.py's verse-data read.

939 tests pass; data counts unchanged (24900 cross-refs, 36 topics, 12 plans,
39 resources).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 11:27:47 -04:00

24 lines
898 B
Python

"""Shared helpers for loading JSON data files."""
import json
from pathlib import Path
def load_merged_json_dir(directory: Path, legacy_path: Path) -> dict:
"""Merge every per-item JSON object in `directory` into a single dict.
Files are read in sorted order and later keys win. Each file must contain a
JSON object (non-object files are skipped). If `directory` does not exist,
falls back to reading the single `legacy_path` file.
"""
aggregated: dict = {}
if directory.exists():
for path in sorted(directory.glob("*.json")):
with open(path, "r", encoding="utf-8") as f:
content = json.load(f)
if isinstance(content, dict):
aggregated.update(content)
elif legacy_path.exists():
with open(legacy_path, "r", encoding="utf-8") as f:
aggregated = json.load(f)
return aggregated