From 69d29e786acbbf5ad7e9252f7974037a8f1e8a8a Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 16:30:01 +0100 Subject: [PATCH] fix: title pack systems from the given collection system_titles read the site's installed sections, loaded from the repository's database.json and profiles, so a manifest built with --db and --emulators-dir titled its systems from another collection. The lookup now takes the console sections, and the manifest builds them from its own db, bios folder and profiles. --- scripts/model/collection.py | 33 ++++++++++---- scripts/model/systemnames.py | 76 ++++++++++++++++++++++++++++----- scripts/packs/manifest.py | 7 ++- scripts/packs/scope.py | 30 ++++++++++--- tests/test_install_selection.py | 27 ++++++++++-- 5 files changed, 144 insertions(+), 29 deletions(-) diff --git a/scripts/model/collection.py b/scripts/model/collection.py index c9e152dd4..eaec9e2dd 100644 --- a/scripts/model/collection.py +++ b/scripts/model/collection.py @@ -10,21 +10,26 @@ from __future__ import annotations import functools from dataclasses import dataclass +from pathlib import Path from model import siteinputs from model.composition import GAME_DATA_TOPS from model.profiles import is_emulator, load_emulator_profiles -def copy_places(db: dict) -> dict[str, set[tuple[str, str, str]]]: +def copy_places( + db: dict, bios_dir: Path | None = None, +) -> dict[str, set[tuple[str, str, str]]]: """sha1 -> every (manufacturer, console, file name) holding a copy. The database keeps one path per content, deduplication having left one copy. by_path_suffix still records the other folders a copy is filed in, - under the name that folder gives it. + under the name that folder gives it. ``bios_dir`` is the database's bios + folder, the site's inputs place it when not given. """ files = db.get("files", {}) - bios_dir = siteinputs.bios_dir(db) + if bios_dir is None: + bios_dir = siteinputs.bios_dir(db) places: dict[str, set[tuple[str, str, str]]] = {} for sha1, entry in files.items(): parts = str(entry.get("path", "")).split("/") @@ -65,24 +70,32 @@ def collection_paths(db: dict) -> dict[str, set[str]]: return paths -def group_by_manufacturer(db: dict) -> dict[str, dict[str, list]]: +def group_by_manufacturer( + db: dict, + bios_dir: Path | None = None, + profiles: dict[str, dict] | None = None, +) -> dict[str, dict[str, list]]: """Group files by manufacturer -> console -> files. The database keeps one path per content, and a copy filed elsewhere was on no page of its own: the Channel F BIOS sat under Arcade > FBNeo only, and Fairchild had no system page. Every folder holding a copy lists it, read from the full paths by_path_suffix records. The game engines' - files are grouped by engine (_engine_sections). + files are grouped by engine (_engine_sections). ``bios_dir`` and + ``profiles`` (aliases included) default to the site's inputs. """ files = db.get("files", {}) manufacturers: dict[str, dict[str, list]] = {} - for sha1, where in copy_places(db).items(): + for sha1, where in copy_places(db, bios_dir).items(): for manufacturer, console in sorted({(m, c) for m, c, _ in where}): manufacturers.setdefault(manufacturer, {}).setdefault(console, []).append(files[sha1]) - return _engine_sections(manufacturers) + return _engine_sections(manufacturers, profiles) -def _engine_sections(manufacturers: dict[str, dict[str, list]]) -> dict[str, dict[str, list]]: +def _engine_sections( + manufacturers: dict[str, dict[str, list]], + all_profiles: dict[str, dict] | None = None, +) -> dict[str, dict[str, list]]: """A game engine's files in one section headed with the engine's name. The Systems index listed ".variants", "scummvm.zip" and "wildmidi.cfg" @@ -94,7 +107,9 @@ def _engine_sections(manufacturers: dict[str, dict[str, list]]) -> dict[str, dic the engine's name (SECTION_LABELS), its address unchanged. """ - profiles = {key: profile for key, profile in profiles_on_disk().items() + if all_profiles is None: + all_profiles = profiles_on_disk() + profiles = {key: profile for key, profile in all_profiles.items() if is_emulator(profile)} engines = _Engines(profiles, _file_readers(profiles)) out: dict[str, dict[str, list]] = {} diff --git a/scripts/model/systemnames.py b/scripts/model/systemnames.py index 23781df63..d31fa2f29 100644 --- a/scripts/model/systemnames.py +++ b/scripts/model/systemnames.py @@ -2,10 +2,18 @@ from __future__ import annotations -from collections.abc import Iterable +from collections.abc import Iterable, Mapping +from collections.abc import Set as AbstractSet +from dataclasses import dataclass +from pathlib import Path from model import siteinputs -from model.collection import SECTION_LABELS, PROGRAM_TOPS, manufacturer_slug +from model.collection import ( + SECTION_LABELS, + PROGRAM_TOPS, + group_by_manufacturer, + manufacturer_slug, +) # (manufacturer slug, console) -> the id of the console's heading. Two @@ -28,6 +36,42 @@ sections_by_name: dict[str, set[tuple[str, str]]] = {} known_program_names: set[str] = set() +@dataclass(frozen=True) +class ConsoleSections: + """The console sections a collection files, by normalized id, and the + program names (casefolded) whose headings name no system.""" + index: Mapping[str, set[tuple[str, str]]] + programs: AbstractSet[str] + + +# The sections the site installed (set_console_sections): the same objects, +# filled in place. +_installed = ConsoleSections(_console_sections, known_program_names) + + +def console_sections( + sections: dict[str, dict] | set[tuple[str, str]], + programs: Iterable[str] = (), +) -> ConsoleSections: + """The sections ``sections`` carries, as set_console_sections reads + them, without installing anything.""" + return ConsoleSections( + console_index(section_pairs_of(sections)), + frozenset(name.casefold() for name in programs), + ) + + +def collection_sections( + db: dict, profiles: dict[str, dict], bios_dir: Path, +) -> ConsoleSections: + """The console sections of a collection: its database, the bios folder + it describes and every emulator profile, aliases included.""" + return console_sections( + group_by_manufacturer(db, bios_dir=bios_dir, profiles=profiles), + program_names(profiles), + ) + + def program_names(profiles: dict[str, dict]) -> set[str]: """Every name an emulator profile goes by: its key, its emulator and display names.""" @@ -103,7 +147,6 @@ def ensure_console_sections() -> None: them, so a page rendered on its own names systems as the site does.""" if _sections_installed[0]: return - from model.collection import group_by_manufacturer from model.database import load_database from model.profiles import load_emulator_profiles @@ -130,7 +173,7 @@ def console_index(pairs: Iterable[tuple[str, str]]) -> dict[str, set[tuple[str, def spelled_section( - sys_id: str, index: dict[str, set[tuple[str, str]]], + sys_id: str, index: Mapping[str, set[tuple[str, str]]], ) -> tuple[str, str] | None: """The one section of ``index`` the id spells, or None. @@ -149,13 +192,19 @@ def spelled_section( def own_section(sys_id: str) -> tuple[str, str] | None: - """The console section that is this id's own, or None. + """The console section that is this id's own among the sections the + site installed, or None (own_section_in).""" + ensure_console_sections() + return own_section_in(sys_id, _installed) + + +def own_section_in(sys_id: str, sections: ConsoleSections) -> tuple[str, str] | None: + """The console section of ``sections`` that is this id's own, or None. The section the id spells (spelled_section), unless it files a program's copies. """ - ensure_console_sections() - section = spelled_section(sys_id, _console_sections) + section = spelled_section(sys_id, sections.index) if section is None: return None mfr, console = section @@ -164,7 +213,7 @@ def own_section(sys_id: str) -> tuple[str, str] | None: if ( mfr in PROGRAM_TOPS or console.casefold() == mfr.casefold() - or heading_name(mfr, console).casefold() in known_program_names + or heading_name(mfr, console).casefold() in sections.programs ): return None return mfr, console @@ -183,9 +232,16 @@ def heading_name(mfr: str, console: str) -> str: def system_name(sys_id: str) -> str: - """The display name of a system id, the only one a page prints.""" + """The display name of a system id, the only one a page prints, among + the sections the site installed (system_name_in).""" + ensure_console_sections() + return system_name_in(sys_id, _installed) + + +def system_name_in(sys_id: str, sections: ConsoleSections) -> str: + """The display name of a system id among ``sections``.""" sys_id = str(sys_id) - section = own_section(sys_id) + section = own_section_in(sys_id, sections) if section is not None: return heading_name(*section) name = SYSTEM_NAMES.get(sys_id) diff --git a/scripts/packs/manifest.py b/scripts/packs/manifest.py index 890f86e65..15b5e6502 100644 --- a/scripts/packs/manifest.py +++ b/scripts/packs/manifest.py @@ -53,7 +53,7 @@ from packs.manifestentry import ( ) from packs.manifestselect import SelectionIndex from packs.paths import save_layout -from packs.scope import platform_list_version, system_titles +from packs.scope import pack_sections, platform_list_version, system_titles from packs.standalone import standalone_copies, standalone_layouts from resolve.contentlayout import TARGET_KEY from resolve.cores import filter_systems_by_target, resolve_platform_cores as _platform_cores @@ -550,7 +550,10 @@ class _ManifestBuild: # The names the platform itself gives its systems (Batocera's psx), # which --system accepts beside the ids. "system_names": platform_system_names(config, list(pack_systems)), - "system_titles": system_titles(manifest_files, omitted.values()), + "system_titles": system_titles( + pack_sections(self.db, self.bios_dir, opts.emulators_dir), + manifest_files, omitted.values(), + ), "files": manifest_files, } if self.data_directories: diff --git a/scripts/packs/scope.py b/scripts/packs/scope.py index 26ec93146..9c6168ef0 100644 --- a/scripts/packs/scope.py +++ b/scripts/packs/scope.py @@ -3,6 +3,10 @@ from __future__ import annotations from collections.abc import Iterable +from pathlib import Path + +from model.profiles import load_emulator_profiles +from model.systemnames import ConsoleSections, collection_sections, system_name_in def platform_list_version(config: dict, platform: str) -> str: @@ -22,8 +26,26 @@ def platform_list_version(config: dict, platform: str) -> str: return version -def system_titles(*entry_lists: Iterable[dict]) -> dict[str, str]: - """The name a person reads for each system the entries carry. +# The last collection's sections: one --all run titles every platform from +# the same database. +_last_sections: list[tuple[dict, str, str, ConsoleSections]] = [] + + +def pack_sections(db: dict, bios_dir: str, emulators_dir: str) -> ConsoleSections: + """The console sections of the collection a pack is built from.""" + for held, held_bios, held_emulators, sections in _last_sections: + if held is db and (held_bios, held_emulators) == (bios_dir, emulators_dir): + return sections + sections = collection_sections( + db, load_emulator_profiles(emulators_dir, skip_aliases=False), Path(bios_dir), + ) + _last_sections[:] = [(db, bios_dir, emulators_dir, sections)] + return sections + + +def system_titles(sections: ConsoleSections, *entry_lists: Iterable[dict]) -> dict[str, str]: + """The name a person reads for each system the entries carry, among + the collection's ``sections`` (pack_sections). Batocera names PlayStation `psx` and the installer showed `sony-playstation (psx)`: neither is what a beginner looks for. The @@ -32,8 +54,6 @@ def system_titles(*entry_lists: Iterable[dict]) -> dict[str, str]: and the installer shows its id: the site build refuses such a system, so only a test fixture reaches here with one. """ - from model.systemnames import system_name - ids = { sys_id for entries in entry_lists for entry in entries @@ -42,7 +62,7 @@ def system_titles(*entry_lists: Iterable[dict]) -> dict[str, str]: titles: dict[str, str] = {} for sys_id in sorted(ids): try: - titles[sys_id] = system_name(sys_id) + titles[sys_id] = system_name_in(sys_id, sections) except KeyError: continue return titles diff --git a/tests/test_install_selection.py b/tests/test_install_selection.py index b15eef141..9442723e1 100644 --- a/tests/test_install_selection.py +++ b/tests/test_install_selection.py @@ -1370,16 +1370,37 @@ class ThePlatformsOwnSystemNamesAreAccepted(unittest.TestCase): source) def test_every_listed_system_gets_the_title_the_site_prints(self): - from packs.scope import system_titles + from model.database import load_database + from packs.scope import pack_sections, system_titles + sections = pack_sections( + load_database(str(REPO_ROOT / "database.json")), str(REPO_ROOT / "bios"), + str(REPO_ROOT / "emulators"), + ) files = [{"dest": "a.bin", "systems": ["sony-playstation", "nintendo-gba"]}] omitted = [{"dest": "b.bin", "systems": ["3do"]}] - self.assertEqual(system_titles(files, omitted), { + self.assertEqual(system_titles(sections, files, omitted), { "3do": "3DO", "nintendo-gba": "Nintendo Game Boy Advance", "sony-playstation": "Sony PlayStation", }) - self.assertEqual(system_titles([{"dest": "c.bin", "systems": ["sys-a"]}]), {}) + self.assertEqual(system_titles(sections, [{"dest": "c.bin", "systems": ["sys-a"]}]), {}) + + def test_the_titles_come_from_the_collection_given(self): + # A manifest built from --db and --emulators-dir titled its systems + # from the repository's collection, read inside the name lookup. + from unittest import mock + + from model import systemnames + from packs.scope import system_titles + + sections = systemnames.console_sections({("Acme", "Widget")}) + with mock.patch.object(systemnames, "ensure_console_sections", + side_effect=AssertionError("site inputs read")): + self.assertEqual( + system_titles(sections, [{"dest": "a.bin", "systems": ["acme-widget"]}]), + {"acme-widget": "Acme Widget"}, + ) def test_a_native_name_narrows_and_lists(self): with FakeRepository() as repo: