mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
refactor(scan): simplify kept hash-source tags after review
- A kept tag only fills an empty region or language slot: the row's value may come from a source the scan didn't ask either (a gamelist), and a hash source's old answer mustn't replace it. Fresh answers still work as before. - A rehash that drops the Hasheous match now does so before the tags are resolved, so the ROM doesn't take tags from the match it's losing. - One reader for this scan's blob and the stored one, every tag field recorded (empty lists included), and one ordered list of claims instead of a dict plus a list. - The applied sources and the hash sources are ordered in one pass, since each pass rereads config.yml, instead of adding a read per ROM. - Tests: one fixture per lookup, the blob assertion folded into the translated-dump test, and new ones for the gamelist case and the dropped Hasheous match. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -43,6 +43,7 @@ class HasheousMetadata(TypedDict):
|
||||
# What the dump our hashes matched says of itself, kept by the scan.
|
||||
dump_regions: NotRequired[list[str]]
|
||||
dump_languages: NotRequired[list[str]]
|
||||
dump_tags: NotRequired[list[str]]
|
||||
|
||||
|
||||
class HasheousPlatform(TypedDict):
|
||||
|
||||
@@ -145,28 +145,30 @@ DUMP_TAG_KEYS = {field: f"dump_{field}" for field in PROVIDER_ALL_TAG_FIELDS}
|
||||
|
||||
def hash_source_tags(
|
||||
fields: dict[str, Any], rom_attrs: dict[str, Any]
|
||||
) -> dict[str, list[str]] | None:
|
||||
"""The tags a hash-matched source vouches for: its answer now, else its last.
|
||||
) -> tuple[dict[str, list[str]], bool]:
|
||||
"""The tags a hash-matched source vouches for, and whether it said so this scan.
|
||||
|
||||
Args:
|
||||
fields: The source's entry in the scan's handler table.
|
||||
fields: The source's entry in the scan's handler table, its blob already
|
||||
holding this scan's dump tags.
|
||||
rom_attrs: The ROM's attributes, still holding its stored ids and blobs.
|
||||
|
||||
Returns:
|
||||
Each tag field's values, or None when the source doesn't cover the ROM.
|
||||
Each tag field's values, empty when the source doesn't cover the ROM, and
|
||||
whether they are this scan's answer rather than the stored one.
|
||||
"""
|
||||
handler = fields["handler"]
|
||||
if handler.get(fields["id_field"]):
|
||||
return {
|
||||
field: list(handler.get(field) or []) for field in PROVIDER_ALL_TAG_FIELDS
|
||||
}
|
||||
if rom_attrs.get(fields["id_field"]):
|
||||
stored = rom_attrs.get(fields["metadata_field"]) or {}
|
||||
return {
|
||||
field: list(stored.get(DUMP_TAG_KEYS[field]) or [])
|
||||
for field in PROVIDER_ALL_TAG_FIELDS
|
||||
}
|
||||
return None
|
||||
answered = bool(handler.get(fields["id_field"]))
|
||||
if answered:
|
||||
blob = handler.get(fields["metadata_field"]) or {}
|
||||
elif rom_attrs.get(fields["id_field"]):
|
||||
blob = rom_attrs.get(fields["metadata_field"]) or {}
|
||||
else:
|
||||
blob = {}
|
||||
claims = {
|
||||
field: blob.get(DUMP_TAG_KEYS[field]) or [] for field in PROVIDER_ALL_TAG_FIELDS
|
||||
}
|
||||
return claims, answered
|
||||
|
||||
|
||||
def scene_apply_sources(
|
||||
@@ -1444,6 +1446,18 @@ async def scan_rom(
|
||||
extra=LOGGER_MODULE_NAME,
|
||||
)
|
||||
|
||||
# A rehash that no longer matches must drop the previous Hasheous match, or
|
||||
# the ROM keeps showing verification flags earned by hashes it no longer has.
|
||||
# Only a conclusive lookup clears them, so an unreachable Hasheous can't
|
||||
# silently de-verify a library.
|
||||
if (
|
||||
scan_type == ScanType.HASHES
|
||||
and hasheous_lookup_conclusive
|
||||
and not hasheous_hash_match.get("hasheous_id")
|
||||
):
|
||||
rom_attrs["hasheous_id"] = None
|
||||
rom_attrs["hasheous_metadata"] = {}
|
||||
|
||||
# A skipped source's tags can't be told apart on the row, so each hash source
|
||||
# keeps the ones its dump gave in its own blob.
|
||||
for source_name in HASH_MATCHED_TAG_SOURCES:
|
||||
@@ -1451,24 +1465,25 @@ async def scan_rom(
|
||||
handler = fields["handler"]
|
||||
if handler.get(fields["id_field"]):
|
||||
dump_tags = {
|
||||
DUMP_TAG_KEYS[field]: list(handler[field] or [])
|
||||
DUMP_TAG_KEYS[field]: list(handler.get(field) or [])
|
||||
for field in PROVIDER_ALL_TAG_FIELDS
|
||||
if field in handler
|
||||
}
|
||||
blob = {**(handler.get(fields["metadata_field"]) or {}), **dump_tags}
|
||||
fields["handler"] = {**handler, fields["metadata_field"]: blob}
|
||||
hash_claims = {
|
||||
source_name: claims
|
||||
for source_name in HASH_MATCHED_TAG_SOURCES
|
||||
if (claims := hash_source_tags(metadata_handlers[source_name], rom_attrs))
|
||||
is not None
|
||||
}
|
||||
hash_sources = get_priority_ordered_metadata_sources(
|
||||
scene_apply_sources(list(hash_claims), scene_locked=scene_locked), "metadata"
|
||||
|
||||
# One ordering pass for both lists, since each one rereads config.yml.
|
||||
hash_sources = [] if scene_locked else list(HASH_MATCHED_TAG_SOURCES)
|
||||
ordered = get_priority_ordered_metadata_sources(
|
||||
list(dict.fromkeys([*apply_sources, *hash_sources])), "metadata"
|
||||
)
|
||||
priority_ordered = [source for source in ordered if source in apply_sources]
|
||||
hash_claims = [
|
||||
hash_source_tags(metadata_handlers[source], rom_attrs)
|
||||
for source in ordered
|
||||
if source in hash_sources
|
||||
]
|
||||
|
||||
# Apply metadata priority order
|
||||
priority_ordered = get_priority_ordered_metadata_sources(apply_sources, "metadata")
|
||||
# Reverse priority order to apply highest priority last
|
||||
for source_name in reversed(priority_ordered):
|
||||
handler_data = metadata_handlers[source_name]["handler"]
|
||||
@@ -1497,10 +1512,11 @@ async def scan_rom(
|
||||
if source_name not in HASH_MATCHED_TAG_SOURCES
|
||||
):
|
||||
continue
|
||||
for source_name in hash_sources:
|
||||
field_value = hash_claims[source_name][field]
|
||||
if field_value:
|
||||
rom_attrs[field] = field_value
|
||||
for claims, answered in hash_claims:
|
||||
# What a source said on an earlier scan fills only an empty slot: the
|
||||
# row's value may come from a source this scan didn't ask either.
|
||||
if claims[field] and (answered or not rom_attrs.get(field)):
|
||||
rom_attrs[field] = claims[field]
|
||||
break
|
||||
|
||||
for field in PROVIDER_MERGED_TAG_FIELDS:
|
||||
@@ -1514,8 +1530,8 @@ async def scan_rom(
|
||||
if claimed:
|
||||
merged_tags = list(claimed)
|
||||
break
|
||||
for source_name in hash_sources:
|
||||
for tag in hash_claims[source_name][field]:
|
||||
for claims, _ in hash_claims:
|
||||
for tag in claims[field]:
|
||||
if tag not in merged_tags:
|
||||
merged_tags.append(tag)
|
||||
rom_attrs[field] = merged_tags
|
||||
@@ -1568,18 +1584,6 @@ async def scan_rom(
|
||||
}
|
||||
)
|
||||
|
||||
# A rehash that no longer matches must drop the previous Hasheous match, or
|
||||
# the ROM keeps showing verification flags earned by hashes it no longer has.
|
||||
# Only a conclusive lookup clears them, so an unreachable Hasheous can't
|
||||
# silently de-verify a library.
|
||||
if (
|
||||
scan_type == ScanType.HASHES
|
||||
and hasheous_lookup_conclusive
|
||||
and not hasheous_hash_match.get("hasheous_id")
|
||||
):
|
||||
rom_attrs["hasheous_id"] = None
|
||||
rom_attrs["hasheous_metadata"] = {}
|
||||
|
||||
# Use PICO-8 cartridge PNG as cover art if no cover is set.
|
||||
# PICO-8 .p8.png files are valid PNG images whose visual content is the
|
||||
# cartridge label, so the ROM file itself serves as the cover art.
|
||||
|
||||
@@ -78,6 +78,37 @@ def ss_translated_by_id() -> Iterator[AsyncMock]:
|
||||
yield by_id
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ss_match_by_id() -> Iterator[AsyncMock]:
|
||||
"""Patch the id refetch with an answer that finds the game but not our dump."""
|
||||
with patch(
|
||||
"handler.scan_handler.meta_ss_handler.get_rom_by_id",
|
||||
new=AsyncMock(return_value=SS_MATCH),
|
||||
) as by_id:
|
||||
yield by_id
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hasheous_no_match() -> Iterator[AsyncMock]:
|
||||
"""Patch a Hasheous hash lookup that answers and finds nothing."""
|
||||
no_match = HasheousRom(hasheous_id=None, igdb_id=None, tgdb_id=None, ra_id=None)
|
||||
with (
|
||||
patch(
|
||||
"handler.scan_handler.meta_hasheous_handler.lookup_rom",
|
||||
new=AsyncMock(return_value=(no_match, True)),
|
||||
) as lookup,
|
||||
patch(
|
||||
"handler.scan_handler.meta_hasheous_handler.get_igdb_game",
|
||||
new=AsyncMock(return_value=no_match),
|
||||
),
|
||||
patch(
|
||||
"handler.scan_handler.meta_hasheous_handler.get_ra_game",
|
||||
new=AsyncMock(return_value=no_match),
|
||||
),
|
||||
):
|
||||
yield lookup
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ss_name_search() -> Iterator[AsyncMock]:
|
||||
"""Patch the ScreenScraper name search, which identifies a title, not a dump."""
|
||||
@@ -163,6 +194,8 @@ async def test_screenscraper_tags_a_translated_dump(ss_translated: AsyncMock):
|
||||
result = await _scan(MetadataSource.SS, tags=[])
|
||||
|
||||
assert result.tags == ["Translation"]
|
||||
assert result.ss_metadata is not None
|
||||
assert result.ss_metadata["dump_tags"] == ["Translation"]
|
||||
|
||||
|
||||
async def test_a_dump_tag_joins_the_filename_tags(ss_translated: AsyncMock):
|
||||
@@ -214,13 +247,6 @@ async def test_a_filename_tag_survives_a_provider_that_reports_none(
|
||||
assert result.tags == ["Beta"]
|
||||
|
||||
|
||||
async def test_a_dump_tag_is_kept_in_its_sources_blob(ss_translated: AsyncMock):
|
||||
result = await _scan(MetadataSource.SS)
|
||||
|
||||
assert result.ss_metadata is not None
|
||||
assert result.ss_metadata["dump_tags"] == ["Translation"]
|
||||
|
||||
|
||||
async def test_an_update_that_skips_screenscraper_keeps_what_its_dump_said():
|
||||
"""The row's tags are re-read from the filename first, as a selected rescan does."""
|
||||
result = await _scan(
|
||||
@@ -249,23 +275,47 @@ async def test_a_hashes_scan_keeps_the_dump_tag_screenscraper_gave():
|
||||
assert result.tags == ["Translation"]
|
||||
|
||||
|
||||
async def test_a_fresh_answer_replaces_what_the_blob_kept():
|
||||
async def test_a_fresh_answer_replaces_what_the_blob_kept(ss_match_by_id: AsyncMock):
|
||||
"""ScreenScraper answering without our dump outranks the dump tags it kept."""
|
||||
with patch(
|
||||
"handler.scan_handler.meta_ss_handler.get_rom_by_id",
|
||||
new=AsyncMock(return_value=SS_MATCH),
|
||||
):
|
||||
result = await _scan(
|
||||
MetadataSource.SS,
|
||||
scan_type=ScanType.UPDATE,
|
||||
ss_id=42,
|
||||
ss_metadata={"dump_tags": ["Translation"]},
|
||||
tags=[],
|
||||
)
|
||||
result = await _scan(
|
||||
MetadataSource.SS,
|
||||
scan_type=ScanType.UPDATE,
|
||||
ss_id=42,
|
||||
ss_metadata={"dump_tags": ["Translation"]},
|
||||
tags=[],
|
||||
)
|
||||
|
||||
assert result.tags == []
|
||||
assert result.ss_metadata is not None
|
||||
assert "dump_tags" not in result.ss_metadata
|
||||
assert result.ss_metadata["dump_tags"] == []
|
||||
|
||||
|
||||
async def test_a_kept_region_only_fills_an_empty_slot():
|
||||
"""The row's region may be a gamelist's, which this scan didn't ask either."""
|
||||
result = await _scan(
|
||||
MetadataSource.SS,
|
||||
scan_type=ScanType.HASHES,
|
||||
ss_id=42,
|
||||
ss_metadata={"dump_regions": ["Europe"]},
|
||||
regions=["USA"],
|
||||
)
|
||||
|
||||
assert result.regions == ["USA"]
|
||||
|
||||
|
||||
async def test_a_rehash_that_drops_hasheous_drops_its_tags(
|
||||
hasheous_no_match: AsyncMock,
|
||||
):
|
||||
result = await _scan(
|
||||
MetadataSource.HASHEOUS,
|
||||
scan_type=ScanType.HASHES,
|
||||
hasheous_id=1,
|
||||
hasheous_metadata={"dump_regions": ["Japan"]},
|
||||
regions=[],
|
||||
)
|
||||
|
||||
assert result.hasheous_id is None
|
||||
assert result.regions == []
|
||||
|
||||
|
||||
async def test_a_complete_rescan_without_screenscraper_drops_its_tags(
|
||||
|
||||
@@ -15,5 +15,6 @@ export type RomHasheousMetadata = {
|
||||
puredos_match?: boolean;
|
||||
dump_regions?: Array<string>;
|
||||
dump_languages?: Array<string>;
|
||||
dump_tags?: Array<string>;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user