Files
romm/backend/handler/metadata/gamelist_handler.py
Georges-Antoine Assi dd7f46b52c fix: make metadata exports and matching follow the library structure
Testing the custom library structure against a live library turned up four
defects, all of them a path being reduced to a bare file name.

Both exporters wrote a rom's `fs_name` alone, so every game a template nests
was exported as if it sat in the platform folder: ES-DE and Pegasus could not
find any of them. They now write the rom's path relative to the platform
folder, the folder the metadata files themselves live in, and copy exported
media into the matching subfolder so two identically named roms keep their own
artwork.

Merging into an existing gamelist.xml / metadata.pegasus.txt keyed entries by
file name too, so identically named roms in different folders both merged into
the first entry and produced duplicates. Entries are now keyed by path, with a
Pegasus block matched by the folder its listed files sit in.

Reading a gamelist.xml had the same collision: two roms sharing a file name
both took the metadata of whichever entry was parsed last. A rom's own path now
wins, and a bare file name stays a fallback key for the tools that write those,
dropped when two entries share it.

Default discovery surfaced hidden folders as multi-file games, unlike
structured discovery which skips them. exclude_multi_roms now owns that rule
for both.

Also validate `exclude`'s exclusion_type: an unknown value reached
`Config.__getattribute__` and returned a 500 rather than a 422.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-08 07:31:05 -04:00

572 lines
21 KiB
Python

import glob
import os
import re
import uuid
from collections.abc import Iterator
from pathlib import Path
from typing import Final, NotRequired, TypedDict
from xml.etree.ElementTree import Element # trunk-ignore(bandit/B405)
import pydash
from defusedxml import ElementTree as ET
from config.config_manager import PLATFORM_MEDIA_DIRS, MetadataMediaType
from config.config_manager import config_manager as cm
from handler.filesystem import fs_platform_handler, fs_resource_handler
from logger.logger import log
from models.platform import Platform
from models.rom import Rom, compute_name_sort_key
from utils.filesystem import join_rel_path, rel_platform_folder
from .base_handler import BaseRom, MetadataHandler
# https://github.com/Aloshi/EmulationStation/blob/master/GAMELISTS.md#reference
# ES-DE writes a top-level <alternativeEmulator> sibling to <gameList>, which produces
# invalid multi-root XML. These patterns strip both self-closing and paired forms so the
# remaining document can be parsed.
ALTERNATIVE_EMULATOR_SELF_CLOSING_RE: Final = re.compile(
r"<alternativeEmulator\b[^>]*/>"
)
ALTERNATIVE_EMULATOR_PAIRED_RE: Final = re.compile(
r"<alternativeEmulator\b[^>]*>.*?</alternativeEmulator>",
re.DOTALL,
)
def get_preferred_media_types() -> list[MetadataMediaType]:
"""Get preferred media types from config"""
config = cm.get_config()
return [MetadataMediaType(media) for media in config.SCAN_MEDIA]
class GamelistMetadataMedia(TypedDict):
box2d_url: str | None
box2d_back_url: str | None
box3d_url: str | None
fanart_url: str | None
image_url: str | None
manual_url: str | None
marquee_url: str | None
miximage_url: str | None
miximage_v2_url: str | None
physical_url: str | None
screenshot_url: str | None
thumbnail_url: str | None
title_screen_url: str | None
video_url: str | None
class GamelistMetadata(GamelistMetadataMedia):
rating: float | None
first_release_date: str | None
sort_name: str | None
companies: list[str] | None
publishers: list[str] | None
developers: list[str] | None
franchises: list[str] | None
genres: list[str] | None
player_count: str | None
md5_hash: str | None
box2d_back_path: str | None
box3d_path: str | None
fanart_path: str | None
miximage_path: str | None
miximage_v2_path: str | None
physical_path: str | None
marquee_path: str | None
title_screen_path: str | None
video_path: str | None
class GamelistRom(BaseRom):
gamelist_id: str | None
regions: NotRequired[list[str]]
languages: NotRequired[list[str]]
gamelist_metadata: NotRequired[GamelistMetadata]
ESDE_MEDIA_MAP: Final = {
"image_url": PLATFORM_MEDIA_DIRS["image"],
"box2d_url": PLATFORM_MEDIA_DIRS["box2d"],
"box2d_back_url": PLATFORM_MEDIA_DIRS["box2d_back"],
"box3d_url": PLATFORM_MEDIA_DIRS["box3d"],
"fanart_url": PLATFORM_MEDIA_DIRS["fanart"],
"manual_url": PLATFORM_MEDIA_DIRS["manual"],
"marquee_url": PLATFORM_MEDIA_DIRS["marquee"],
"miximage_url": PLATFORM_MEDIA_DIRS["miximage"],
"miximage_v2_url": PLATFORM_MEDIA_DIRS["miximage_v2"],
"physical_url": PLATFORM_MEDIA_DIRS["physical"],
"screenshot_url": PLATFORM_MEDIA_DIRS["screenshot"],
"title_screen_url": PLATFORM_MEDIA_DIRS["title_screen"],
"thumbnail_url": PLATFORM_MEDIA_DIRS["thumbnail"],
"video_url": PLATFORM_MEDIA_DIRS["video"],
}
XML_TAG_MAP: Final = {
"image_url": "image",
"box2d_url": "cover",
"box2d_back_url": "backcover",
"box3d_url": "box3d",
"fanart_url": "fanart",
"manual_url": "manual",
"marquee_url": "marquee",
"miximage_url": "miximage",
"miximage_v2_url": "miximage_v2",
"physical_url": "physicalmedia",
"screenshot_url": "screenshot",
"title_screen_url": "title_screen",
"thumbnail_url": "thumbnail",
"video_url": "video",
}
def gamelist_path_to_filename(raw_path: str) -> str:
"""Return the filename a gamelist <path> refers to, without the `./` prefix."""
return os.path.basename(raw_path.removeprefix("./"))
def gamelist_path_to_rel_path(raw_path: str) -> str:
"""Return a gamelist <path> as a path relative to the gamelist file itself.
Unlike the filename alone, this tells two identically named roms in different
folders apart, which a custom library structure allows.
"""
return raw_path.removeprefix("./").strip("/")
def _make_file_uri(platform_dir: str, raw_text: str) -> str:
cleaned_text = raw_text.replace("./", "")
joined_path = Path(platform_dir, cleaned_text)
fs_platform_handler.validate_path(str(joined_path))
return f"file://{joined_path.as_posix()}"
def _split_comma_separated_values(value: str | None) -> list[str]:
"""Split comma separated values into clean list"""
if not value:
return []
split_values = value.split(",")
return pydash.compact([item.strip() for item in split_values])
def extract_media_from_gamelist_rom(
game: Element, platform: Platform
) -> GamelistMetadataMedia:
platform_dir = fs_platform_handler.get_platform_fs_structure(platform.fs_slug)
gamelist_media = GamelistMetadataMedia(
box2d_url=None,
box2d_back_url=None,
box3d_url=None,
fanart_url=None,
image_url=None,
manual_url=None,
marquee_url=None,
miximage_url=None,
miximage_v2_url=None,
physical_url=None,
screenshot_url=None,
title_screen_url=None,
thumbnail_url=None,
video_url=None,
)
# Check explicit XML elements defined in gamelist.xml
for media_key, xml_tag in XML_TAG_MAP.items():
elem = game.find(xml_tag)
if elem is not None and elem.text:
# trunk-ignore(mypy/literal-required)
gamelist_media[media_key] = _make_file_uri(platform_dir, elem.text)
# Fallback to searching media folders by ROM basename
path_elem = game.find("path")
if path_elem is not None and path_elem.text:
rom_stem = os.path.splitext(os.path.basename(path_elem.text))[0]
for media_key, folder_name in ESDE_MEDIA_MAP.items():
# trunk-ignore(mypy/literal-required)
if gamelist_media[media_key]:
continue
search_pattern = os.path.join(platform_dir, folder_name, f"{rom_stem}.*")
search_path = fs_platform_handler.validate_path(search_pattern)
found_files = glob.glob(str(search_path))
if found_files:
# trunk-ignore(mypy/literal-required)
gamelist_media[media_key] = (
f"file://{str(Path(found_files[0]).relative_to(fs_platform_handler.base_path))}"
)
return gamelist_media
def extract_metadata_from_gamelist_rom(
game: Element, platform: Platform
) -> GamelistMetadata:
rating_elem = game.find("rating")
releasedate_elem = game.find("releasedate")
sortname_elem = game.find("sortname")
developer_elem = game.find("developer")
publisher_elem = game.find("publisher")
family_elem = game.find("family")
genre_elem = game.find("genre")
players_elem = game.find("players")
md5_elem = game.find("md5")
rating = (
float(rating_elem.text)
if rating_elem is not None and rating_elem.text
else None
)
first_release_date = (
releasedate_elem.text
if releasedate_elem is not None and releasedate_elem.text
else None
)
sort_name = (
sortname_elem.text if sortname_elem is not None and sortname_elem.text else None
)
developer = (
developer_elem.text
if developer_elem is not None and developer_elem.text
else None
)
publisher = (
publisher_elem.text
if publisher_elem is not None and publisher_elem.text
else None
)
family = family_elem.text if family_elem is not None and family_elem.text else None
genre = genre_elem.text if genre_elem is not None and genre_elem.text else None
players = (
players_elem.text if players_elem is not None and players_elem.text else None
)
md5 = md5_elem.text if md5_elem is not None and md5_elem.text else None
publishers = _split_comma_separated_values(publisher)
developers = _split_comma_separated_values(developer)
return GamelistMetadata(
rating=rating,
first_release_date=first_release_date,
sort_name=sort_name,
companies=list(dict.fromkeys([*developers, *publishers])),
publishers=publishers,
developers=developers,
franchises=_split_comma_separated_values(family),
genres=_split_comma_separated_values(genre),
player_count=players,
md5_hash=md5,
box2d_back_path=None,
box3d_path=None,
fanart_path=None,
miximage_path=None,
miximage_v2_path=None,
physical_path=None,
marquee_path=None,
title_screen_path=None,
video_path=None,
**extract_media_from_gamelist_rom(game, platform),
)
def populate_rom_specific_paths(
rom_metadata: GamelistMetadata, rom: Rom
) -> dict[str, str]:
"""Populate ROM-specific paths after retrieving metadata from cache"""
preferred_media_types = get_preferred_media_types()
# Create a copy of the metadata to avoid modifying the cached version
updated_metadata: dict[str, str] = {}
# Set paths for media types that are preferred
if MetadataMediaType.BOX2D_BACK in preferred_media_types and rom_metadata.get(
"box2d_back_url"
):
updated_metadata["box2d_back_path"] = (
f"{fs_resource_handler.get_media_resources_path(rom.platform_id, rom.id, MetadataMediaType.BOX2D_BACK)}/box2d_back.png"
)
if MetadataMediaType.BOX3D in preferred_media_types and rom_metadata.get(
"box3d_url"
):
updated_metadata["box3d_path"] = (
f"{fs_resource_handler.get_media_resources_path(rom.platform_id, rom.id, MetadataMediaType.BOX3D)}/box3d.png"
)
if MetadataMediaType.FANART in preferred_media_types and rom_metadata.get(
"fanart_url"
):
updated_metadata["fanart_path"] = (
f"{fs_resource_handler.get_media_resources_path(rom.platform_id, rom.id, MetadataMediaType.FANART)}/fanart.png"
)
if MetadataMediaType.MARQUEE in preferred_media_types and rom_metadata.get(
"marquee_url"
):
updated_metadata["marquee_path"] = (
f"{fs_resource_handler.get_media_resources_path(rom.platform_id, rom.id, MetadataMediaType.MARQUEE)}/marquee.png"
)
if MetadataMediaType.MIXIMAGE in preferred_media_types and rom_metadata.get(
"miximage_url"
):
updated_metadata["miximage_path"] = (
f"{fs_resource_handler.get_media_resources_path(rom.platform_id, rom.id, MetadataMediaType.MIXIMAGE)}/miximage.png"
)
if MetadataMediaType.MIXIMAGE_V2 in preferred_media_types and rom_metadata.get(
"miximage_v2_url"
):
updated_metadata["miximage_v2_path"] = (
f"{fs_resource_handler.get_media_resources_path(rom.platform_id, rom.id, MetadataMediaType.MIXIMAGE_V2)}/miximage_v2.png"
)
if MetadataMediaType.PHYSICAL in preferred_media_types and rom_metadata.get(
"physical_url"
):
updated_metadata["physical_path"] = (
f"{fs_resource_handler.get_media_resources_path(rom.platform_id, rom.id, MetadataMediaType.PHYSICAL)}/physical.png"
)
if MetadataMediaType.TITLE_SCREEN in preferred_media_types and rom_metadata.get(
"title_screen_url"
):
updated_metadata["title_screen_path"] = (
f"{fs_resource_handler.get_media_resources_path(rom.platform_id, rom.id, MetadataMediaType.TITLE_SCREEN)}/title_screen.png"
)
if MetadataMediaType.VIDEO in preferred_media_types and rom_metadata.get(
"video_url"
):
updated_metadata["video_path"] = (
f"{fs_resource_handler.get_media_resources_path(rom.platform_id, rom.id, MetadataMediaType.VIDEO)}/video.mp4"
)
return updated_metadata
class GamelistHandler(MetadataHandler):
"""Handler for ES-DE gamelist.xml metadata source"""
def __init__(self):
# Cache for storing parsed gamelist data by platform ID
self._gamelist_cache = {}
async def populate_cache(self, platform: Platform):
if not self.is_enabled():
return
# Find the gamelist.xml file for this platform
gamelist_file_path = await self._find_gamelist_file(platform)
if not gamelist_file_path:
return
# Parse the gamelist file
self._parse_gamelist_xml(gamelist_file_path, platform)
def clear_cache(self):
"""Clear the gamelist cache"""
self._gamelist_cache.clear()
@classmethod
def is_enabled(cls) -> bool:
return True
async def heartbeat(self) -> bool:
return True
async def _find_gamelist_file(self, platform: Platform) -> Path | None:
"""Find the gamelist.xml file for a platform"""
platform_dir = fs_platform_handler.get_platform_fs_structure(platform.fs_slug)
# Check for platform-level gamelist.xml
platform_gamelist = f"{platform_dir}/gamelist.xml"
if await fs_platform_handler.file_exists(platform_gamelist):
return fs_platform_handler.validate_path(platform_gamelist)
return None
def _iter_game_elements(self, gamelist_path: Path) -> Iterator[Element]:
"""Yield each top-level game/folder element of a gamelist.xml.
Parsing incrementally keeps one entry alive at a time instead of
holding a tree for the whole document, which matters on platforms
with thousands of games.
ES-DE writes an <alternativeEmulator> sibling to <gameList>, producing
invalid multi-root XML that the incremental parser rejects. Those files
fall back to stripping the element from the document first. Entries are
keyed by filename by the caller, so re-yielding any element already
consumed before the failure is harmless.
"""
try:
for _, elem in ET.iterparse(gamelist_path, events=("end",)):
if elem.tag in ("game", "folder"):
yield elem
elem.clear()
return
except ET.ParseError:
pass
xml_content = gamelist_path.read_text(encoding="utf-8", errors="replace")
xml_content = ALTERNATIVE_EMULATOR_SELF_CLOSING_RE.sub("", xml_content)
xml_content = ALTERNATIVE_EMULATOR_PAIRED_RE.sub("", xml_content)
for elem in ET.fromstring(xml_content):
if elem.tag in ("game", "folder"):
yield elem
def _parse_gamelist_xml(
self, gamelist_path: Path, platform: Platform
) -> dict[str, GamelistRom]:
"""Parse a gamelist.xml file and return ROM data indexed by <path>.
Entries are keyed by their path relative to the gamelist itself, so two
identically named roms in different folders keep their own metadata. A bare
file name stays a fallback key, since that is all some tools write.
Results are cached by platform ID to avoid re-parsing the same file multiple times.
"""
# Check if we already have cached data for this platform
cache_key = platform.id
if cache_key in self._gamelist_cache:
log.debug(f"Using cached gamelist data for platform {platform.id}")
return self._gamelist_cache[cache_key]
preferred_media_types = get_preferred_media_types()
roms_data: dict[str, GamelistRom] = {}
by_filename: dict[str, GamelistRom] = {}
ambiguous_filenames: set[str] = set()
try:
for game in self._iter_game_elements(gamelist_path):
if game.tag not in ("game", "folder"):
continue
path_elem = game.find("path")
if path_elem is None or path_elem.text is None:
continue
rel_path = gamelist_path_to_rel_path(path_elem.text)
filename = gamelist_path_to_filename(path_elem.text)
# Extract metadata
name_elem = game.find("name")
desc_elem = game.find("desc")
lang_elem = game.find("lang")
region_elem = game.find("region")
sortname_elem = game.find("sortname")
name = (
name_elem.text if name_elem is not None and name_elem.text else ""
)
sort_name = (
sortname_elem.text
if sortname_elem is not None and sortname_elem.text
else None
)
summary = (
desc_elem.text if desc_elem is not None and desc_elem.text else ""
)
regions = (
_split_comma_separated_values(region_elem.text)
if region_elem is not None
else []
)
languages = (
_split_comma_separated_values(lang_elem.text)
if lang_elem is not None
else []
)
# Build ROM data
rom_metadata = extract_metadata_from_gamelist_rom(game, platform)
name_sort_key = compute_name_sort_key(sort_name) if sort_name else None
rom_data = GamelistRom(
gamelist_id=str(uuid.uuid4()),
name=name,
name_sort_key=name_sort_key,
summary=summary,
regions=regions,
languages=languages,
gamelist_metadata=rom_metadata,
)
# Choose which cover style to use
cover_url = rom_metadata["box2d_url"] or rom_metadata["image_url"]
if cover_url:
rom_data["url_cover"] = cover_url
# Grab the manual
manual_url = rom_metadata["manual_url"]
if manual_url and MetadataMediaType.MANUAL in preferred_media_types:
rom_data["url_manual"] = manual_url
# Build list of screenshot URLs. The title screen is stored in
# its own media folder via title_screen_path, so it must not
# also land in screenshots/.
url_screenshots = []
if (
rom_metadata["screenshot_url"]
and MetadataMediaType.SCREENSHOT in preferred_media_types
):
url_screenshots.append(rom_metadata["screenshot_url"])
rom_data["url_screenshots"] = url_screenshots
roms_data[rel_path] = rom_data
if filename in by_filename:
ambiguous_filenames.add(filename)
by_filename[filename] = rom_data
for name, data in by_filename.items():
if name not in ambiguous_filenames:
roms_data.setdefault(name, data)
# Cache the parsed data for this platform
self._gamelist_cache[cache_key] = roms_data
except ET.ParseError as e:
log.warning(f"Failed to parse gamelist.xml at {gamelist_path}: {e}")
# Entries read before the document turned out to be invalid are
# dropped, so a corrupt file yields nothing rather than a partial
# import that silently looks complete.
roms_data.clear()
except Exception as e:
log.error(f"Error reading gamelist.xml at {gamelist_path}: {e}")
return roms_data
async def get_rom(self, fs_name: str, platform: Platform, rom: Rom) -> GamelistRom:
"""Get ROM metadata from gamelist.xml files"""
if not self.is_enabled():
return GamelistRom(gamelist_id=None)
# Find the gamelist.xml file for this platform
gamelist_file_path = await self._find_gamelist_file(platform)
if not gamelist_file_path:
return GamelistRom(gamelist_id=None)
# Parse the gamelist file
all_roms_data = self._parse_gamelist_xml(gamelist_file_path, platform)
# The rom's own path wins over its bare file name, which a custom library
# structure can leave shared with a rom in another folder.
rel_path = join_rel_path(
rel_platform_folder(
rom.fs_path or "",
fs_platform_handler.get_platform_fs_structure(platform.fs_slug),
),
fs_name,
)
matched_key = next(
(key for key in (rel_path, fs_name) if key in all_roms_data), None
)
if matched_key is not None:
log.debug(f"Found exact gamelist match for {matched_key}")
matched_rom = pydash.clone_deep(all_roms_data[matched_key])
gamelist_metadata = matched_rom.get("gamelist_metadata")
# Populate ROM-specific paths using the actual rom object
if gamelist_metadata:
rom_specific_paths = populate_rom_specific_paths(gamelist_metadata, rom)
gamelist_metadata.update(**rom_specific_paths) # type: ignore
matched_rom["gamelist_metadata"] = gamelist_metadata
return matched_rom
return GamelistRom(gamelist_id=None)