Files
romm/backend/handler/metadata/base_handler.py
Sam Dornan 4542998510 fix(backend): make type ignores honest and replace mypy trunk-ignores with fixes (#4711)
* fix(backend): make type ignores match what mypy reports

Third in the staged series clearing the errors #4655 made visible.
Whole-project mypy: 91 errors in 26 files -> 64 in 23.

Twenty `# type: ignore[assignment]` comments in
endpoints/responses/rom.py never suppressed anything. The request-scoped
values `from_orm_with_request` attaches to a Rom (rom_user, has_notes,
user_saves and the rest) are not declared on the model, so mypy reports
`attr-defined`; it does so under 1.19 as well as 2.3, so the code was
wrong when the comments were written rather than drifting since. They
now name the error they were meant to silence.

Seventy-nine ignores across 24 files suppressed nothing at all under
`--warn-unused-ignores`, and are removed. One of them,
`utils/zip_cache.py`, keeps its `attr-defined` code for the private
`zipfile._get_compressor` and drops only `assignment`. The unused set is
identical under mypy 2.3.0 and 2.3.1, and removing them leaves the error
count unchanged under both, so none was quietly needed.

The two `filter` helpers typed as `[QueryT: Select | Update | Delete]`
bound the TypeVar to the union, so `query.filter(...)` returned the union
and could not be assigned back to `QueryT`. Constraining it to the three
types lets mypy check each one separately, and the callers are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SPpv6W2ygP9tQ61fTQxdYp

* fix(backend): replace mypy and ASYNC240 trunk-ignores with real fixes

A `trunk-ignore` only suppresses when Trunk runs the linter. Plain mypy
does not read them, so the 13 on mypy findings were all still errors to
`mypy .` and would fail a whole-project check. Each now has a fix.

endpoints/tasks.py: `common_data` was a plain dict literal unpacked into
eight TypedDict constructors, so mypy saw `dict[str, Any]` and could not
match its keys. It holds exactly the fields of BaseTaskStatusResponse and
is now annotated as one, so the unpacking is checked.

handler/metadata/gamelist_handler.py: both media maps were inferred as
`dict[str, str]`, so indexing the TypedDict with their keys was
unprovable. Their keys are now typed `GamelistMediaKey`, the Literal of
the TypedDict's fields. A misspelt map key is now an error at the entry;
before, only the index site could have noticed, and it was suppressed.

handler/metadata/playmatch_handler.py: `attr` was a bare `str`. A TypeIs
guard narrows it to the five lookup attributes, and the frozenset is
derived from that Literal so the names live in one place. mypy now checks
that each name is a real PlaymatchRomMatch key. It does not check that
each takes an int, since mypy is lenient about value types on a union key;
that still rests on which five names are listed.

endpoints/search.py: the per-source tuples mixed ROM-list and handler
types, which mypy joined to `object`. The dict is now annotated with what
the loop uses: Mapping rows and the MetadataHandler base.

tests/handler/metadata/test_launchbox_handler.py: `Path.stat()` inside an
async test (ASYNC240) now goes through `anyio.Path`, as the rest of the
codebase already does.

Whole-project mypy: 64 errors in 23 files -> 51 in 20, identical under
2.3.0 and 2.3.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SPpv6W2ygP9tQ61fTQxdYp

* fix(backend): keep gamelist media keys in one place

Review pointed out that `GamelistMediaKey` repeated all fourteen fields
of GamelistMetadataMedia. Python has no `keyof`, so any Literal of a
TypedDict's keys is a second declaration of them, and a field added to
the TypedDict would be left out of it without any error.

The three index sites go back to suppressing `literal-required`, now as
`# type: ignore[literal-required]` so plain mypy reads it as well as
Trunk. The TypedDict stays the only declaration of the key set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SPpv6W2ygP9tQ61fTQxdYp

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-22 16:02:39 -05:00

401 lines
13 KiB
Python

import abc
import json
import re
import unicodedata
from functools import lru_cache
from pathlib import Path
from typing import TYPE_CHECKING, Final, Mapping, NotRequired, TypedDict
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
from fastapi import HTTPException, status
from strsimpy.jaro_winkler import JaroWinkler
from handler.dump_cache import hget_json
from handler.redis_handler import async_cache
from logger.logger import log
from tasks.scheduled.update_switch_titledb import (
SWITCH_PRODUCT_ID_KEY,
SWITCH_TITLEDB_INDEX_KEY,
SWITCH_TITLEDB_STORE,
)
from utils.cache import is_cache_store_ready
from utils.context import ctx_httpx_client
from utils.switch import derive_base_title_id
if TYPE_CHECKING:
from models.rom import Rom
jarowinkler = JaroWinkler()
METADATA_FIXTURES_DIR: Final = Path(__file__).parent / "fixtures"
# Providers are third parties; a response is read only this far before it is dropped.
MAX_RESPONSE_BYTES: Final[int] = 1_000_000
REQUEST_TIMEOUT: Final[int] = 25
# These are loaded in cache in update_switch_titledb_task
SWITCH_TITLEDB_REGEX: Final = re.compile(r"(70[0-9]{12})")
SWITCH_PRODUCT_ID_REGEX: Final = re.compile(r"(0100[0-9A-F]{12})")
# No regex needed for MAME
MAME_XML_KEY: Final = "romm:mame_xml"
# ScummVM
SCUMMVM_INDEX_KEY: Final = "romm:scummvm_index"
# PS2 OPL
PS2_OPL_REGEX: Final = re.compile(r"^([A-Z]{4}_\d{3}\.\d{2})\..*$")
PS2_OPL_KEY: Final = "romm:ps2_opl_index"
# Sony serial codes for PS1, PS2, PS3 and PSP
SONY_SERIAL_REGEX: Final = re.compile(r".*([a-zA-Z]{4}-\d{5}).*$")
PS1_SERIAL_INDEX_KEY: Final = "romm:ps1_serial_index"
PS2_SERIAL_INDEX_KEY: Final = "romm:ps2_serial_index"
PSP_SERIAL_INDEX_KEY: Final = "romm:psp_serial_index"
LEADING_ARTICLE_PATTERN = re.compile(r"^(a|an|the)\b", re.IGNORECASE)
COMMA_ARTICLE_PATTERN = re.compile(r",\s(a|an|the)\b(?=\s*[^\w\s]|$)", re.IGNORECASE)
NON_WORD_SPACE_PATTERN = re.compile(r"[^\w\s]")
MULTIPLE_SPACE_PATTERN = re.compile(r"\s+")
def unavailable(provider: str) -> HTTPException:
"""The error a provider raises when it can't be reached."""
return HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Can't connect to {provider}, check your internet connection",
)
class BaseRom(TypedDict):
name: NotRequired[str]
name_sort_key: NotRequired[str | None]
summary: NotRequired[str]
url_cover: NotRequired[str]
url_screenshots: NotRequired[list[str]]
url_manual: NotRequired[str]
class CoverResource(TypedDict):
"""One piece of artwork the manual cover search offers, in the shape
SteamGridDB grids have, so every provider fills the same picker."""
thumb: str
url: str
type: str
width: int
height: int
style: str
author: str
score: int
nsfw: bool
humor: bool
epilepsy: bool
class CoverResult(TypedDict):
name: str
resources: list[CoverResource]
SENSITIVE_KEYS = {
"Authorization",
"Client-ID",
"Client-Secret",
"client_id",
"client_secret",
"api_key",
"ssid",
"sspassword",
"devid",
"devpassword",
"y",
}
SENSITIVE_KEYS_REGEX = re.compile(
rf"({'|'.join(re.escape(k) for k in SENSITIVE_KEYS)})=[^&\s\"]*",
re.IGNORECASE,
)
# This caches results to avoid repeated normalization of the same search term
@lru_cache(maxsize=1024)
def _normalize_search_term(
name: str, remove_articles: bool = True, remove_punctuation: bool = True
) -> str:
# Lower and replace underscores with spaces
name = name.lower().replace("_", " ")
# Remove articles (combined if possible)
if remove_articles:
name = LEADING_ARTICLE_PATTERN.sub("", name)
name = COMMA_ARTICLE_PATTERN.sub("", name)
# Remove punctuation and normalize spaces in one step
if remove_punctuation:
name = NON_WORD_SPACE_PATTERN.sub(" ", name)
name = MULTIPLE_SPACE_PATTERN.sub(" ", name)
# Unicode normalization and accent removal
if any(ord(c) > 127 for c in name): # Only if non-ASCII chars present
normalized = unicodedata.normalize("NFD", name)
name = "".join(c for c in normalized if not unicodedata.combining(c))
return name.strip()
def strip_sensitive_query_params(
url: str, sensitive_keys: set[str] = SENSITIVE_KEYS
) -> str:
"""Remove sensitive query parameters from a URL."""
parsed = urlparse(url)
qsl = parse_qsl(parsed.query, keep_blank_values=True)
keys_lower = {k.lower() for k in sensitive_keys}
keep = [(k, v) for k, v in qsl if k.lower() not in keys_lower]
new_query = urlencode(keep, doseq=True)
return urlunparse(parsed._replace(query=new_query))
def restore_sensitive_query_params(url: str, params: dict[str, str]) -> str:
"""Add back key/value pairs previously stripped by strip_sensitive_query_params."""
parsed = urlparse(url)
qsl = parse_qsl(parsed.query, keep_blank_values=True)
existing = {k.lower() for k in params}
filtered = [(k, v) for k, v in qsl if k.lower() not in existing]
new_query = urlencode(filtered + list(params.items()))
return urlunparse(parsed._replace(query=new_query))
class MetadataHandler(abc.ABC):
SEARCH_TERM_SPLIT_PATTERN = re.compile(r"[\:\-\/]")
SEARCH_TERM_NORMALIZER = re.compile(r"\s*[:-]\s+")
@classmethod
@abc.abstractmethod
def is_enabled(cls) -> bool:
"""Return whether this metadata handler is enabled."""
async def _fetch_capped(
self, url: str, *, headers: Mapping[str, str]
) -> bytes | None:
"""Stream a body, returning None rather than reading past the cap."""
httpx_client = ctx_httpx_client.get()
body = bytearray()
async with httpx_client.stream(
"GET", url, headers=dict(headers), timeout=REQUEST_TIMEOUT
) as res:
res.raise_for_status()
async for chunk in res.aiter_bytes():
body += chunk
if len(body) > MAX_RESPONSE_BYTES:
log.warning(
"Response from %s exceeds %s bytes", url, MAX_RESPONSE_BYTES
)
return None
return bytes(body)
def normalize_cover_url(self, url: str) -> str:
return url if not url else f"https:{url.replace('https:', '')}"
def normalize_search_term(
self, name: str, remove_articles: bool = True, remove_punctuation: bool = True
) -> str:
return _normalize_search_term(name, remove_articles, remove_punctuation)
def find_best_match(
self,
search_term: str,
game_names: list[str],
min_similarity_score: float = 0.75,
split_game_name: bool = False,
) -> tuple[str | None, float]:
"""
Find the best matching game name from a list of candidates.
Args:
search_term: The search term to match
game_names: List of game names to check against
min_similarity_score: Minimum similarity score to consider a match
Returns:
Tuple of (best_match_name, similarity_score) or (None, 0.0) if no good match
"""
if not game_names:
return None, 0.0
best_match = None
best_score = 0.0
search_term_normalized = self.normalize_search_term(search_term)
for game_name in game_names:
game_name_normalized = self.normalize_search_term(game_name)
# If the game name is split, normalize the last term
if split_game_name and re.search(self.SEARCH_TERM_SPLIT_PATTERN, game_name):
game_name_normalized = self.normalize_search_term(
re.split(self.SEARCH_TERM_SPLIT_PATTERN, game_name)[-1]
)
score = jarowinkler.similarity(search_term_normalized, game_name_normalized)
if score > best_score:
best_score = score
best_match = game_name
# Early exit for perfect match
if score == 1.0:
break
if best_score >= min_similarity_score:
return best_match, best_score
return None, 0.0
async def _ps2_opl_format(self, match: re.Match[str], search_term: str) -> str:
serial_code = match.group(1)
index_entry = await async_cache.hget(PS2_OPL_KEY, serial_code)
if index_entry:
index_entry = json.loads(index_entry)
search_term = index_entry["Name"]
return search_term
async def _sony_serial_format(self, index_key: str, serial_code: str) -> str | None:
index_entry = await async_cache.hget(index_key, serial_code.upper())
if index_entry:
index_entry = json.loads(index_entry)
return index_entry["title"]
return None
async def _ps1_serial_format(self, match: re.Match[str], search_term: str) -> str:
serial_code = match.group(1)
return (
await self._sony_serial_format(PS1_SERIAL_INDEX_KEY, serial_code)
or search_term
)
async def _ps2_serial_format(self, match: re.Match[str], search_term: str) -> str:
serial_code = match.group(1)
return (
await self._sony_serial_format(PS2_SERIAL_INDEX_KEY, serial_code)
or search_term
)
async def _psp_serial_format(self, match: re.Match[str], search_term: str) -> str:
serial_code = match.group(1)
return (
await self._sony_serial_format(PSP_SERIAL_INDEX_KEY, serial_code)
or search_term
)
async def _switch_titledb_format(
self, match: re.Match[str], search_term: str
) -> tuple[str, dict | None]:
title_id = match.group(1)
if not await self._is_switch_titledb_current():
log.error("Could not find a current Switch titleID index in cache")
return search_term, None
index_entry = await self._switch_titledb_entry(title_id)
if index_entry:
return index_entry["name"], index_entry
return search_term, None
async def _switch_productid_format(
self, rom: "Rom", fs_name: str, search_term: str
) -> tuple[str, dict | None]:
"""Match by Switch product id, preferring the one the scan read out of
the binary over one scraped from the filename."""
if rom.title_id and SWITCH_PRODUCT_ID_REGEX.fullmatch(rom.title_id.upper()):
product_id = rom.title_id.upper()
else:
match = SWITCH_PRODUCT_ID_REGEX.search(fs_name)
if not match:
return search_term, None
product_id = match.group(1)
# Updates and DLC share the base application's product ID, off by the
# low 12 bits, and only the base has a titledb entry.
product_id = derive_base_title_id(product_id) or product_id
if not await self._is_switch_titledb_current():
log.error("Could not find a current Switch productID index in cache")
return search_term, None
index_entry = await self._switch_product_id_entry(product_id)
if index_entry:
return index_entry["name"], index_entry
return search_term, None
@staticmethod
async def _is_switch_titledb_current() -> bool:
"""Whether the titleID index was written by the current import."""
return await is_cache_store_ready(
async_cache, SWITCH_TITLEDB_STORE, SWITCH_TITLEDB_INDEX_KEY
)
@staticmethod
async def _switch_titledb_entry(title_id: str) -> dict | None:
return await hget_json(SWITCH_TITLEDB_INDEX_KEY, title_id)
@classmethod
async def _switch_product_id_entry(cls, product_id: str) -> dict | None:
"""Resolve a Switch product id to the titleID entry its index points at."""
title_id = await hget_json(SWITCH_PRODUCT_ID_KEY, product_id)
if not title_id:
return None
return await cls._switch_titledb_entry(title_id)
async def _mame_format(self, search_term: str) -> str:
from handler.filesystem import fs_rom_handler
index_entry = await async_cache.hget(MAME_XML_KEY, search_term)
if index_entry:
index_entry = json.loads(index_entry)
search_term = fs_rom_handler.get_file_name_with_no_tags(
index_entry.get("description", search_term)
)
return search_term
async def _scummvm_format(self, search_term: str) -> str:
from handler.filesystem import fs_rom_handler
search_term = fs_rom_handler.get_file_name_with_no_extension(search_term)
index_entry = await async_cache.hget(SCUMMVM_INDEX_KEY, search_term)
if index_entry:
index_entry = json.loads(index_entry)
search_term = index_entry["name"]
return search_term
def _mask_sensitive_values(
self, values: Mapping[str, str | None]
) -> dict[str, str]:
"""
Mask sensitive values (headers or params), leaving only the first 2 and last 2 characters of the token.
"""
masked_keys: dict[str, str] = {}
for key, val in values.items():
if val is None:
masked_keys[key] = ""
continue
if key == "Authorization" and val.startswith("Bearer "):
token = val.split(" ", 1)[1]
masked_keys[key] = f"Bearer {token[:2]}***{token[-2:]}"
elif key in SENSITIVE_KEYS:
masked_keys[key] = f"{val[:2]}***{val[-2:]}"
else:
masked_keys[key] = val
return masked_keys