refactor: read recipe snapshots in one place

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:02:44 +01:00
1 parent 437690d4a8
commit 3f33b1c247
2 files changed
+31 -14

No files matched your search

+26 -6
View File
@@ -4,8 +4,11 @@ from __future__ import annotations
import json
from collections import defaultdict
from collections.abc import Iterator
from pathlib import Path
from romrecipes.ledger import ARCHIVES_FILE
Recipe = list[tuple[str, str]]
@@ -31,15 +34,32 @@ def load_dat_recipes(path: str | Path) -> dict[str, dict[str, Recipe]]:
target = Path(path)
if target.is_dir():
merged: dict[str, dict[str, Recipe]] = {}
for snapshot_file in sorted(target.glob("*.json")):
merged = merge_recipes(merged, load_dat_recipes(snapshot_file))
for source, snapshot in dat_snapshots(target):
merged = merge_recipes(merged, _snapshot_recipes(source, snapshot))
return merged
snapshot_path = target
if not snapshot_path.is_file():
if not target.is_file():
return {}
with snapshot_path.open(encoding="utf-8") as handle:
return _snapshot_recipes(*_read_snapshot(target))
def _read_snapshot(path: Path) -> tuple[str, dict]:
with path.open(encoding="utf-8") as handle:
snapshot = json.load(handle)
source = snapshot.get("source", "dat")
return str(snapshot.get("source", path.stem)), snapshot
def dat_snapshots(recipes_dir: Path) -> Iterator[tuple[str, dict]]:
"""(source, snapshot) of every DAT snapshot under ``recipes_dir``.
archives.json sits beside them and records the collection's archives,
not a DAT's sets.
"""
for path in sorted(recipes_dir.glob("*.json")):
if path.name != ARCHIVES_FILE.name:
yield _read_snapshot(path)
def _snapshot_recipes(source: str, snapshot: dict) -> dict[str, dict[str, Recipe]]:
# A recipe shared by several versions is stored once under the earliest.
# The label therefore reads "unchanged since", not "is this version".
recipes: dict[str, dict[str, Recipe]] = defaultdict(dict)
+5 -8
View File
@@ -14,6 +14,7 @@ from collections.abc import Callable
from core.hashing import parse_hash_list
from model.collection import profiles_on_disk
from packs.crossref import FileConsumers
from romrecipes.romsets import dat_snapshots, usable_recipe
from sitegen import markup, wanted as sitewanted
from core.paths import REPO_ROOT
from sitegen.fields import search_excluded
@@ -310,9 +311,7 @@ def _release_name(source: str, label: str, versions: dict) -> str:
@functools.lru_cache(maxsize=1)
def _romset_snapshots() -> list[tuple[str, dict]]:
return [(str(snapshot.get("source", path.stem)), snapshot)
for path in sorted((REPO_ROOT / "recipes").glob("*.json"))
for snapshot in [json.loads(path.read_text(encoding="utf-8"))]]
return list(dat_snapshots(REPO_ROOT / "recipes"))
@functools.lru_cache(maxsize=1)
@@ -328,12 +327,10 @@ def romset_releases() -> dict[str, list[tuple[RomsetMembers, str, list[Release]]
versions = snapshot.get("dats") or {}
order = {label: index for index, label in enumerate(versions)}
for entry in snapshot.get("entries") or []:
members = tuple(sorted(
(str(m["name"]), str(m.get("crc32") or "").lower())
for m in entry.get("members") or [] if m.get("name") is not None
))
if not entry.get("name") or not members or not all(crc for _n, crc in members):
recipe = usable_recipe(entry.get("members") or [])
if not entry.get("name") or recipe is None:
continue
members = tuple(sorted(recipe))
agreed = [(order.get(label, -1), _release_name(source, str(label), versions))
for label in entry.get("dats") or [entry.get("dat")]]
releases.setdefault(str(entry["name"]), []).append((members, source, agreed))