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.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 16:43:46 +01:00
1 parent 8787f453c7
commit 69d29e786a
5 files changed
+144 -29

No files matched your search

+24 -9
View File
@@ -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]] = {}
+66 -10
View File
@@ -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)