refactor: read declared hashes with one helper

core.hashing.declared_hash_lists gives an entry's declared values per
field in the order written, and declared_hashes builds its sets on it.
The gap page, the doubled-destination report and the file lookup used
their own copies of that reader. The wanted page keeps its filters on
top of it. sitegen.lookup.hash_pairs replaces sorted_hash_values for
the lookup, the data exports and the structured data. The one-field
aliases parse_md5_list and reconcile.hash_values are gone, and
model.profiles reads HASH_FIELDS instead of a fourth tuple.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 10:29:37 +01:00
1 parent ea68e85c06
commit 02969dda06
30 files changed
+176 -195

No files matched your search

+4 -3
View File
@@ -10,7 +10,8 @@ from model.database import sha1_at
from model.systemnames import system_name
from model.profiles import is_emulator
from sitegen.config import SITE_URL
from sitegen.lookup import sorted_hash_values, site_path
from core.hashing import parse_hash_list
from sitegen.lookup import site_path
from sitegen.render import card_anchor, console_href
@@ -45,8 +46,8 @@ def system_index_items(db: dict, coverages: dict, profiles: dict) -> list[dict]:
ids = ([str(entry["system"])] if entry.get("system")
else [str(s) for s in entry.get("systems") or []] or
(own if len(own) == 1 else []))
found = {v for v in sorted_hash_values(entry.get("sha1")) if v in files}
for md5 in sorted_hash_values(entry.get("md5")):
found = {v for v in parse_hash_list(entry.get("sha1")) if v in files}
for md5 in parse_hash_list(entry.get("md5")):
match = by_md5.get(md5)
found.update([match] if isinstance(match, str) else match or [])
for sys_id in ids:
+20 -21
View File
@@ -28,7 +28,7 @@ from core.paths import REPO_ROOT
from sitegen.install import release_pack_platforms
from sitegen.lookup import (
HASH_ALGORITHMS,
sorted_hash_values,
hash_pairs,
data_directory_members,
file_location_items,
file_lookup_records,
@@ -48,23 +48,23 @@ def _accepted_hash_rows(platform_files: list[dict], emulator_items: list[dict])
"""One row per accepted hash value, for both layers, lookup-ready."""
rows: list[dict] = []
for row in platform_files:
for algorithm in HASH_ALGORITHMS:
rows.extend({
"layer": "platform", "owner": row["platform_id"],
"system": row["system"], "name": row["name"],
"path": row["destination"], "algorithm": algorithm, "value": value,
} for value in sorted_hash_values(row.get(f"{algorithm}_raw", row.get(algorithm))))
# A table row keeps the field as declared under <algorithm>_raw.
declared = {algo: row.get(f"{algo}_raw", row.get(algo)) for algo in HASH_ALGORITHMS}
rows.extend({
"layer": "platform", "owner": row["platform_id"],
"system": row["system"], "name": row["name"],
"path": row["destination"], "algorithm": algorithm, "value": value,
} for algorithm, value in hash_pairs(declared))
for item in emulator_items:
for entry in item["profile"].get("files", []) or []:
if not isinstance(entry, dict):
continue
for algorithm in HASH_ALGORITHMS:
rows.extend({
"layer": "emulator", "owner": item["id"],
"system": _json_text(entry.get("system")),
"name": entry.get("name", ""), "path": entry.get("path", "") or "",
"algorithm": algorithm, "value": value,
} for value in sorted_hash_values(entry.get(algorithm)))
rows.extend({
"layer": "emulator", "owner": item["id"],
"system": _json_text(entry.get("system")),
"name": entry.get("name", ""), "path": entry.get("path", "") or "",
"algorithm": algorithm, "value": value,
} for algorithm, value in hash_pairs(entry))
# Members of an archive: MAME and FBNeo hash the ROMs inside the
# set, never the set. Left out, a loose nmk004.bin (crc32
# 83b6f611) found no row while the site search found it.
@@ -72,13 +72,12 @@ def _accepted_hash_rows(platform_files: list[dict], emulator_items: list[dict])
for member in entry.get("contents") or []:
if not isinstance(member, dict) or not member.get("name"):
continue
for algorithm in HASH_ALGORITHMS:
rows.extend({
"layer": "emulator", "owner": item["id"],
"system": _json_text(entry.get("system")),
"name": str(member["name"]), "path": f"{archive}/{member['name']}",
"algorithm": algorithm, "value": value,
} for value in sorted_hash_values(member.get(algorithm)))
rows.extend({
"layer": "emulator", "owner": item["id"],
"system": _json_text(entry.get("system")),
"name": str(member["name"]), "path": f"{archive}/{member['name']}",
"algorithm": algorithm, "value": value,
} for algorithm, value in hash_pairs(member))
rows.sort(key=lambda r: (r["layer"], r["owner"], r["name"], r["algorithm"], r["value"]))
return rows
+5 -19
View File
@@ -7,6 +7,7 @@ import html
import re
from dataclasses import dataclass
from core.hashing import declared_hash_lists
from core.text import series
from model.platforms import native_layer
from model.profiles import is_emulator
@@ -51,24 +52,6 @@ def _held_accepted(discrepancy: str, db: dict | None) -> str:
return ""
_PROBLEM_HASHES = ("sha1", "md5", "sha256", "crc32")
def _declared_hashes(entry: dict) -> dict[str, list[str]]:
"""The hashes the platform declares for a reported file, by algorithm.
The untested table said "declared hash contradicted by the local file"
and named neither: the reader could not look for the dump RetroDECK asks
for, nor report the difference.
"""
from core.hashing import parse_hash_list
return {
algo: parse_hash_list(entry.get(algo))
for algo in _PROBLEM_HASHES if parse_hash_list(entry.get(algo))
}
def _declarations(cov: dict) -> dict[int, dict]:
"""Each verify detail of a platform, by id, with the declaration it
scores (wanted.paired_declarations): AmberELEC declares
@@ -422,7 +405,10 @@ def _problem_record(pname: str, cov: dict, d: dict, entry: dict) -> dict:
"reason": d.get("reason", ""),
"discrepancy": d.get("discrepancy", ""),
"system": d.get("system", ""),
"declared": _declared_hashes(entry),
# The untested table said "declared hash contradicted by the local
# file" and named neither: the reader could not look for the dump
# RetroDECK asks for, nor report the difference.
"declared": declared_hash_lists(entry),
"destination": str(entry.get("destination") or d.get("name", "")),
"path": d.get("path") or "",
}
+35 -39
View File
@@ -9,7 +9,7 @@ import urllib.parse
import zipfile
from pathlib import Path
from core.hashing import parse_hash_list
from core.hashing import declared_hash_lists
from model import siteinputs
from model.collection import manufacturer_slug
from model.registry import load_versions
@@ -26,13 +26,15 @@ from sitegen.write import write_if_changed
HASH_ALGORITHMS = ("sha1", "sha256", "md5", "crc32")
def sorted_hash_values(value: object) -> list[str]:
"""Every accepted value of one hash field, lowercase.
def hash_pairs(entry: dict, fields: tuple[str, ...] = HASH_ALGORITHMS) -> list[tuple[str, str]]:
"""(algorithm, value) for every value an entry declares, lowercase, the
algorithms in the order given and the values sorted within each.
A field holds one value, a list of accepted revisions, or (Recalbox) a
comma-joined string: an exact lookup missed all but the first form.
"""
return sorted(parse_hash_list(value))
return [(algo, value) for algo, values in declared_hash_lists(entry, fields).items()
for value in sorted(values)]
def site_path(markdown_target: str) -> str:
@@ -307,11 +309,10 @@ def retired_records(retired: dict[str, dict], files: dict, locations: list[dict]
else:
# The page its card was on: the reader lands among its neighbours.
url = site_path(f"systems/{card_page(str(record.get('path', '')), sha1)}.md")
for algorithm in HASH_ALGORITHMS:
value = sha1 if algorithm == "sha1" else record.get(algorithm)
records.extend({"value": one, "algorithm": algorithm, "layer": "retired",
"name": str(record.get("name", "")), "sha1": sha1,
"url": url, **extra} for one in sorted_hash_values(value))
records.extend({"value": one, "algorithm": algorithm, "layer": "retired",
"name": str(record.get("name", "")), "sha1": sha1,
"url": url, **extra}
for algorithm, one in hash_pairs({**record, "sha1": sha1}))
return records
@@ -365,12 +366,11 @@ def _collection_lookup_records(
continue
parts = str(entry.get("path", "")).split("/")
held_by = named.get((manufacturer_slug(parts[1]), parts[2])) if len(parts) > 3 else None
for algorithm in HASH_ALGORITHMS:
records.extend({
"value": value, "algorithm": algorithm, "layer": "collection",
"name": str(entry.get("name", "")), "sha1": sha1, "url": item["url"],
**({"system_name": held_by} if held_by else {}),
} for value in sorted_hash_values(entry.get(algorithm)))
records.extend({
"value": value, "algorithm": algorithm, "layer": "collection",
"name": str(entry.get("name", "")), "sha1": sha1, "url": item["url"],
**({"system_name": held_by} if held_by else {}),
} for algorithm, value in hash_pairs(entry))
records += _archive_member_records(entry, sha1, item["url"])
return records
@@ -388,13 +388,12 @@ def _platform_lookup_records(coverages: dict, consumers: FileConsumers | None) -
# no collection file by itself: the archive holding it is the one.
archive = (consumers.sha1_of(entry)
if consumers is not None and entry.get("zipped_file") else None)
for algorithm in HASH_ALGORITHMS:
records.extend({
"value": value, "algorithm": algorithm, "layer": "platform",
"owner": key, "name": str(entry.get("name", "")), "url": url,
**({"system_name": system_label} if system_label else {}),
**({"sha1": archive} if archive else {}),
} for value in sorted_hash_values(entry.get(algorithm)))
records.extend({
"value": value, "algorithm": algorithm, "layer": "platform",
"owner": key, "name": str(entry.get("name", "")), "url": url,
**({"system_name": system_label} if system_label else {}),
**({"sha1": archive} if archive else {}),
} for algorithm, value in hash_pairs(entry))
return records
@@ -416,12 +415,11 @@ def _emulator_lookup_records(profiles: dict) -> list[dict]:
if isinstance(member, dict) and member.get("name")
]
for name, holder in holders:
for algorithm in HASH_ALGORITHMS:
records.extend({
"value": value, "algorithm": algorithm, "layer": "emulator",
"owner": key, "name": name, "url": url,
**({"system_name": entry_label} if entry_label else {}),
} for value in sorted_hash_values(holder.get(algorithm)))
records.extend({
"value": value, "algorithm": algorithm, "layer": "emulator",
"owner": key, "name": name, "url": url,
**({"system_name": entry_label} if entry_label else {}),
} for algorithm, value in hash_pairs(holder))
return records
@@ -503,12 +501,11 @@ def _read_by_name(records: list[dict], files: dict, consumers: FileConsumers) ->
anchor = emulator_card_anchor(emulator, entries, index)
url = site_path(f"emulators/{emulator_card_page(emulator, anchor)}.md#{anchor}")
name = str(entries[index].get("name", ""))
for algorithm in HASH_ALGORITHMS:
added.extend({
"value": value, "algorithm": algorithm, "layer": "emulator",
"owner": emulator, "name": name, "url": url, "sha1": sha1,
"declared": "name",
} for value in sorted_hash_values(entry.get(algorithm)))
added.extend({
"value": value, "algorithm": algorithm, "layer": "emulator",
"owner": emulator, "name": name, "url": url, "sha1": sha1,
"declared": "name",
} for algorithm, value in hash_pairs(entry))
return added
@@ -539,11 +536,10 @@ def _held_under_every_hash(records: list[dict], files: dict, indexes: dict) -> l
if sha1 not in files:
continue
record["sha1"] = sha1
for algorithm in HASH_ALGORITHMS:
copies.extend({**record, "value": value, "algorithm": algorithm,
"declared": record["algorithm"]}
for value in sorted_hash_values(files[sha1].get(algorithm))
if value != record["value"])
copies.extend({**record, "value": value, "algorithm": algorithm,
"declared": record["algorithm"]}
for algorithm, value in hash_pairs(files[sha1])
if value != record["value"])
return copies
+4 -11
View File
@@ -16,7 +16,7 @@ from core.paths import REPO_ROOT
from sitegen.descriptions import EMULATOR_SNIPPETS, NOTHING_TO_FETCH, PAGE_DATA, TITLE_NOUNS, title_noun
from sitegen.emulatorcards import snippet_noun
from sitegen.install import comes_with_app
from sitegen.lookup import data_member_digests, sorted_hash_values
from sitegen.lookup import data_member_digests, hash_pairs
from sitegen.render import card_anchor, system_page_map
from sitegen.write import write_if_changed
@@ -197,8 +197,7 @@ def platform_structured_data(name: str, coverage: dict) -> dict:
}
identifiers = [
f"{field}:{value}"
for field in ("sha1", "md5", "crc32", "sha256")
for value in sorted_hash_values(entry.get(field))
for field, value in hash_pairs(entry, ("sha1", "md5", "crc32", "sha256"))
]
if identifiers:
part["identifier"] = identifiers
@@ -369,11 +368,7 @@ def emulator_structured_data(profile: dict) -> dict:
continue
# One identifier per accepted value: "sha256:a,b,c,d" matched no
# exact search (mesence's BS-X.bin).
identifiers = [
f"{field}:{value}"
for field in ("sha1", "sha256", "md5", "crc32")
for value in sorted_hash_values(f.get(field))
]
identifiers = [f"{field}:{value}" for field, value in hash_pairs(f)]
part: dict[str, object] = {
"@type": "CreativeWork",
"name": str(f.get("path") or f["name"]),
@@ -389,9 +384,7 @@ def emulator_structured_data(profile: dict) -> dict:
"@type": "CreativeWork",
"name": str(member.get("name", "")),
"identifier": [
f"{field}:{value}"
for field in ("sha1", "crc32")
for value in sorted_hash_values(member.get(field))
f"{field}:{value}" for field, value in hash_pairs(member, ("sha1", "crc32"))
],
}
for member in f.get("contents") or []
+3 -2
View File
@@ -26,7 +26,7 @@ from resolve.layout import runs_standalone
from resolve.standalone import standalone_set
from model.profiles import unique_emulator_profiles
from packs.crossref import first_hash
from core.hashing import cut_md5_owner, parse_hash_list
from core.hashing import cut_md5_owner, declared_hash_lists, parse_hash_list
from resolve.review import LARGE_FILE_ISSUE_URL, Placement, placement
from resolve.validation import deliverable, filter_files_by_mode, installed_with_emulator
from sitegen.config import UPLOAD_ROOT
@@ -53,7 +53,8 @@ def declared_hashes(entry: dict) -> dict[str, list[str]]:
counts only where no stronger hash is declared, as in
resolve_local_file.
"""
hashes = {algo: parse_hash_list(entry.get(algo)) for algo in ALGORITHMS}
declared = declared_hash_lists(entry, ALGORITHMS)
hashes = {algo: declared.get(algo, []) for algo in ALGORITHMS}
if entry.get("zipped_file"):
hashes["md5"] = []
hashes["md5"] = [v for v in hashes["md5"] if 0 < len(v) <= 32]