refactor: elect arcade boards in their own module

The board election, its lazy load and its cache sat in render, with the
cache flag in model.systemnames. sitegen.arcadeboards elects them into a
frozen ArcadeBoards, build.py installs them once the sections are the
run's, and render keeps the hrefs. systemnames counts its installs
instead of holding a board flag, and section_without_boards moves there.
The elected boards and the Arcade sections are identical before and
after on the repository's data.
This commit is contained in:
Abdessamad Derraz committed 2026-11-23 21:44:08 +01:00
1 parent 74924be233
commit 1ab22ec956
8 files changed
+300 -278

No files matched your search

+34 -3
View File
@@ -22,7 +22,9 @@ from model.collection import (
console_anchors: dict[tuple[str, str], str] = {}
boards_built = [False]
# How many times set_console_sections installed sections: what a page
# derives from them (the arcade boards) is stale once it moves.
sections_generation = [0]
# Every (manufacturer, console) section the system pages carry.
@@ -135,8 +137,7 @@ def set_console_sections(
known_program_names.clear()
known_program_names.update(name.casefold() for name in programs)
_sections_installed[0] = True
# Which ids are boards depends on which have a section elsewhere.
boards_built[0] = False
sections_generation[0] += 1
_sections_installed = [False]
@@ -231,6 +232,36 @@ def heading_name(mfr: str, console: str) -> str:
return f"{mfr} {console}"
# Of the program tops, the one whose headings never name a system, even read
# whole. An Arcade heading can (Namco System 246), an Other one cannot.
_NAMELESS_TOPS = frozenset({"Other"})
def section_without_boards(sys_id: str) -> tuple[str, str] | None:
"""The console section a system id leads to before arcade boards, as
(manufacturer slug, console): what the folders and the reviewed parents
give."""
own = own_section(sys_id)
if own is not None:
return manufacturer_slug(own[0]), own[1]
parent = SYSTEM_PARENTS.get(sys_id)
if parent is not None and parent in section_pairs:
return manufacturer_slug(parent[0]), parent[1]
try:
name = system_name(sys_id)
except KeyError:
return None
places = sections_by_name.get(name.casefold(), set())
if len(places) != 1:
return None
mfr, console = next(iter(places))
if mfr in _NAMELESS_TOPS or (
mfr in PROGRAM_TOPS and console.casefold() in known_program_names
):
return None
return manufacturer_slug(mfr), console
def system_name(sys_id: str) -> str:
"""The display name of a system id, the only one a page prints, among
the sections the site installed (system_name_in)."""
+215
View File
@@ -0,0 +1,215 @@
"""Which arcade board each bios_zip archive of the collection is the BIOS of."""
from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass, field
from core.memo import DerivedView
from model import siteinputs
from model.systemnames import (
SYSTEM_PARENTS,
ensure_console_sections,
section_pairs,
section_without_boards,
system_name,
)
# A board is a system id that a profile entry with ``category: bios_zip``
# names, through its ``system:``/``systems:`` or the profile's single
# system, whose archive the collection holds, and that has no section under
# a machine's maker. The archive is found by its name and, where the entry
# lists ``contents:``, kept only when it holds one of those CRC32s:
# maclc3.zip names a MAME set and an unrelated 189 MB file. The folder a
# copy sits in decides nothing.
@dataclass(frozen=True)
class ArcadeBoards:
"""The boards elected from the profiles, for the sections installed."""
# sha1 -> the board id the archive is the BIOS of.
archive_boards: Mapping[str, str] = field(default_factory=dict)
# Casefolded board name -> the board id that stands for it.
board_names: Mapping[str, str] = field(default_factory=dict)
# sha1 -> the (manufacturer, console) sections of the machines whose own
# set the archive is: neogeo.zip for the Neo Geo, mz2000.zip for the MZ.
archive_machines: Mapping[str, frozenset[tuple[str, str] | None]] = field(default_factory=dict)
def _owner_ids(entry: dict, systems: list[str]) -> set[str]:
"""The system ids a profile entry is declared for."""
owners = {str(entry["system"])} if entry.get("system") else set()
owners.update(str(sys_id) for sys_id in entry.get("systems") or [])
if not owners and len(systems) == 1:
owners = set(systems)
return owners
# _board_role for an id whose archive is its machine's: a section under the
# machine's maker, or a reviewed parent outside Arcade.
_MACHINE = "\0machine"
def _board_role(sys_id: str) -> str | None:
"""A board's name, _MACHINE for an id that names a machine with a
section of its own, None for an id that names neither (the generic
arcade id, an id with no name)."""
parent = SYSTEM_PARENTS.get(sys_id)
if parent is not None:
return None if parent[0] == "Arcade" else _MACHINE
section = section_without_boards(sys_id)
if section is not None and section[0] != "arcade":
return _MACHINE
try:
return system_name(sys_id)
except KeyError:
return None
def _arcade_tails(by_path_suffix: dict) -> frozenset[str]:
return frozenset(
sha1s[0]
for tail, sha1s in by_path_suffix.items()
if tail.startswith("Arcade/") and len(sha1s) == 1
)
_arcade_suffixes = DerivedView(_arcade_tails)
def in_arcade(sha1: str, db: dict) -> bool:
"""Whether a copy of the content sits under bios/Arcade/."""
entry = db.get("files", {}).get(sha1) or {}
if str(entry.get("path", "")).startswith("bios/Arcade/"):
return True
return sha1 in _arcade_suffixes(db.get("indexes", {}).get("by_path_suffix", {}))
def entry_archives(entry: dict, files: dict, by_name: dict) -> set[str]:
"""The archives of the collection a ``bios_zip`` entry names."""
from core.ziptools import zip_member_crcs
crcs = {
str(member["crc32"]).lower()
for member in entry.get("contents") or []
if isinstance(member, dict) and member.get("crc32")
}
names = {str(entry.get("name", ""))}
names.update(str(alias) for alias in entry.get("aliases") or [])
archives: set[str] = set()
for name in names:
found = by_name.get(name.rsplit("/", 1)[-1]) or []
for sha1 in [found] if isinstance(found, str) else found:
if sha1 not in files:
continue
if crcs:
held = zip_member_crcs(str(siteinputs.collection_path(files[sha1]["path"])))
# An archive the build cannot read keeps its name.
if held and not held & crcs:
continue
archives.add(sha1)
return archives
def _machine_section(sys_id: str) -> tuple[str, str] | None:
"""The (manufacturer, console) section SYSTEM_PARENTS files a machine
id under, outside Arcade. Only a reviewed parent moves an archive out
of the folder holding it: an id with a section of its own keeps its
archives where the collection files them."""
parent = SYSTEM_PARENTS.get(sys_id)
if parent is None or parent[0] == "Arcade" or parent not in section_pairs:
return None
return parent
@dataclass
class _Ballots:
"""The votes the profiles cast, archive by archive."""
# sha1 -> board name -> the profiles naming it. None: a machine's own.
votes: dict[str, dict[str, set[str]] | None] = field(default_factory=dict)
ids_by_name: dict[str, set[str]] = field(default_factory=dict)
machines: dict[str, set[tuple[str, str] | None]] = field(default_factory=dict)
def cast(self, key: str, entry: dict, owners: set[str], archives: set[str]) -> None:
for owner in owners:
role = _board_role(owner)
if role == _MACHINE:
# The profile's only system stands for an entry that names
# none: MAME's printer and expansion sets in a Dragon
# profile. Only an entry naming the machine moves its set,
# and only when every machine it names has that section:
# epson_fx80.zip, which sixteen machines load, has none.
declared = entry.get("system") or entry.get("systems")
places = {_machine_section(o) for o in owners
if _board_role(o) == _MACHINE} if declared else {None}
section = next(iter(places)) if len(places) == 1 else None
for sha1 in archives:
self.votes[sha1] = None
self.machines.setdefault(sha1, set()).add(section)
continue
if role is None:
continue
self.ids_by_name.setdefault(role.casefold(), set()).add(owner)
for sha1 in archives:
ballot = self.votes.setdefault(sha1, {})
if ballot is not None:
ballot.setdefault(role.casefold(), set()).add(key)
def elect(self, db: dict) -> ArcadeBoards:
archive_boards: dict[str, str] = {}
board_names: dict[str, str] = {}
for sha1, ballot in self.votes.items():
if not ballot:
continue
ranked = sorted(ballot.items(), key=lambda item: (-len(item[1]), item[0]))
# Two boards named as often: a device ROM several boards share.
if len(ranked) > 1 and len(ranked[0][1]) == len(ranked[1][1]):
continue
name = ranked[0][0]
board = sorted(self.ids_by_name[name])[0]
archive_boards[sha1] = board
if in_arcade(sha1, db):
board_names[name] = board
# A board none of whose archives sits under Arcade has no section there.
archive_boards = {sha1: board for sha1, board in archive_boards.items()
if system_name(board).casefold() in board_names}
machines = {sha1: frozenset(places) for sha1, places in self.machines.items()}
return ArcadeBoards(archive_boards, board_names, machines)
def elect_arcade_boards(profiles: dict[str, dict], db: dict) -> ArcadeBoards:
"""The board of each archive, elected from the profiles' ``bios_zip``
entries against the console sections installed."""
ensure_console_sections()
files = db.get("files", {})
by_name = db.get("indexes", {}).get("by_name", {})
ballots = _Ballots()
for key in sorted(profiles):
profile = profiles[key]
if profile.get("role") == "test":
continue
systems = [str(sys_id) for sys_id in profile.get("systems") or []]
for entry in profile.get("files") or []:
if not isinstance(entry, dict) or entry.get("category") != "bios_zip":
continue
owners = _owner_ids(entry, systems)
if not owners:
continue
ballots.cast(key, entry, owners, entry_archives(entry, files, by_name))
return ballots.elect(db)
def read_arcade_boards() -> ArcadeBoards:
"""The boards of the collection and profiles the site inputs name, none
without a database."""
from model.database import load_database
from model.profiles import load_emulator_profiles
inputs = siteinputs.current()
if not inputs.db.exists():
return ArcadeBoards()
db = load_database(str(inputs.db))
profiles = load_emulator_profiles(str(inputs.emulators), skip_aliases=False)
return elect_arcade_boards(profiles, db)
+5
View File
@@ -87,6 +87,7 @@ from sitegen.lookup import (
)
from sitegen.nav import overview_anchors, generate_nav
from sitegen.parts import PUBLISHED_PARTS_FILE, load_published_parts, record_parts
from sitegen.arcadeboards import elect_arcade_boards
from sitegen.places import system_pages_from_data
from sitegen.platformfiles import EXPORTED
from sitegen.platformindex import SETUP_TABLE_PAGE, generate_platform_index, with_setup_table
@@ -109,6 +110,7 @@ from sitegen.render import (
reset_console_pages,
set_system_page_map,
split_system_stems,
use_arcade_boards,
)
from sitegen.structured import (
dataset_structured_data,
@@ -671,6 +673,9 @@ def generate_site(args: argparse.Namespace) -> None:
# The plan names systems: the sections must be this run's data first.
run.manufacturers = group_by_manufacturer(db)
set_console_sections(run.manufacturers, program_names(profiles))
# Which archive is which board's depends on the sections: elected once
# they are this run's.
use_arcade_boards(elect_arcade_boards(profiles, db))
plan_emulator_pages(profiles, run.file_held)
# Build system_id -> manufacturer page map (needed by all generators)
print("Building system cross-reference map...")
+27 -257
View File
@@ -4,24 +4,20 @@ from __future__ import annotations
import re
import urllib.parse
from collections.abc import Callable
from collections.abc import Callable, Mapping
from model.database import load_database
from model import siteinputs
from sitegen import markup
from core.memo import DerivedView
from sitegen.arcadeboards import ArcadeBoards, read_arcade_boards
from core.paths import REPO_ROOT
from core.text import count_text as count_text
from model.collection import PROGRAM_TOPS, card_moves, manufacturer_slug
from model.collection import card_moves, manufacturer_slug
from model.systemnames import (
SYSTEM_PARENTS,
boards_built,
console_anchors,
ensure_console_sections,
own_section,
known_program_names,
section_pairs,
sections_by_name,
section_without_boards,
sections_generation,
system_name,
)
@@ -239,7 +235,7 @@ def system_section(sys_id: str) -> tuple[str, str] | None:
board = _board_of_id(BOARD_PARENTS[sys_id])
if board in _board_sections:
return "arcade", system_name(board)
section = _section_without_boards(sys_id)
section = section_without_boards(sys_id)
if (not _board_sections or sys_id in SYSTEM_PARENTS
or (section is not None and section[0] != "arcade")):
return section
@@ -249,30 +245,6 @@ def system_section(sys_id: str) -> tuple[str, str] | None:
return section
def _section_without_boards(sys_id: str) -> tuple[str, str] | None:
"""system_section before arcade boards: what the folders and the
reviewed parents give."""
own = own_section(sys_id)
if own is not None:
return manufacturer_slug(own[0]), own[1]
parent = SYSTEM_PARENTS.get(sys_id)
if parent is not None and parent in section_pairs:
return manufacturer_slug(parent[0]), parent[1]
try:
name = system_name(sys_id)
except KeyError:
return None
places = sections_by_name.get(name.casefold(), set())
if len(places) != 1:
return None
mfr, console = next(iter(places))
if mfr in _NAMELESS_TOPS or (
mfr in PROGRAM_TOPS and console.casefold() in known_program_names
):
return None
return manufacturer_slug(mfr), console
def _system_link(sys_id: str, prefix: str = "") -> str:
"""A system id linked to the section named after it, or the bare id."""
section = system_section(sys_id)
@@ -282,220 +254,27 @@ def _system_link(sys_id: str, prefix: str = "") -> str:
return sys_id
# Arcade boards. A board is a system id that a profile entry with
# ``category: bios_zip`` names, through its ``system:``/``systems:`` or the
# profile's single system, whose archive the collection holds, and that has
# no section under a machine's maker. The archive is found by its name and,
# where the entry lists ``contents:``, kept only when it holds one of those
# CRC32s: maclc3.zip names a MAME set and an unrelated 189 MB file. The
# folder a copy sits in decides nothing.
_arcade_source: list[tuple[dict, dict] | None] = [None]
# sha1 -> the board id the archive is the BIOS of.
_archive_boards: dict[str, str] = {}
# Casefolded board name -> the board id that stands for it.
_board_names: dict[str, str] = {}
# sha1 -> the (manufacturer, console) sections of the machines whose own
# set the archive is: neogeo.zip for the Neo Geo, mz2000.zip for the MZ.
_archive_machines: dict[str, set[tuple[str, str] | None]] = {}
# The arcade boards elected (sitegen.arcadeboards) for the sections
# installed, with the sections_generation they were elected under.
_arcade: list[tuple[int, ArcadeBoards] | None] = [None]
def set_arcade_boards(profiles: dict[str, dict], db: dict) -> None:
"""Install the profiles and database the arcade boards are read from.
Without it they are read from the repository on first use."""
_arcade_source[0] = (profiles, db)
boards_built[0] = False
def _owner_ids(entry: dict, systems: list[str]) -> set[str]:
"""The system ids a profile entry is declared for."""
owners = {str(entry["system"])} if entry.get("system") else set()
owners.update(str(sys_id) for sys_id in entry.get("systems") or [])
if not owners and len(systems) == 1:
owners = set(systems)
return owners
# _board_role for an id whose archive is its machine's: a section under the
# machine's maker, or a reviewed parent outside Arcade.
_MACHINE = "\0machine"
def _board_role(sys_id: str) -> str | None:
"""A board's name, _MACHINE for an id that names a machine with a
section of its own, None for an id that names neither (the generic
arcade id, an id with no name)."""
parent = SYSTEM_PARENTS.get(sys_id)
if parent is not None:
return None if parent[0] == "Arcade" else _MACHINE
section = _section_without_boards(sys_id)
if section is not None and section[0] != "arcade":
return _MACHINE
try:
return system_name(sys_id)
except KeyError:
return None
def _in_arcade(sha1: str, db: dict) -> bool:
"""Whether a copy of the content sits under bios/Arcade/."""
entry = db.get("files", {}).get(sha1) or {}
if str(entry.get("path", "")).startswith("bios/Arcade/"):
return True
return sha1 in _arcade_suffix_sha1s(db)
def _arcade_tails(by_path_suffix: dict) -> frozenset[str]:
return frozenset(
sha1s[0]
for tail, sha1s in by_path_suffix.items()
if tail.startswith("Arcade/") and len(sha1s) == 1
)
_arcade_suffixes = DerivedView(_arcade_tails)
def _arcade_suffix_sha1s(db: dict) -> frozenset[str]:
return _arcade_suffixes(db.get("indexes", {}).get("by_path_suffix", {}))
def _build_boards() -> None:
if boards_built[0]:
return
def use_arcade_boards(boards: ArcadeBoards) -> None:
"""Install the boards elected for the sections installed now. Without
it, or once the sections change, they are read from the site inputs on
first use."""
ensure_console_sections()
source = _arcade_source[0]
if source is None:
source = _read_arcade_source()
if source is None:
_archive_boards.clear()
_board_names.clear()
boards_built[0] = True
return
_arcade_source[0] = source
profiles, db = source
files = db.get("files", {})
by_name = db.get("indexes", {}).get("by_name", {})
# sha1 -> board name -> the profiles naming it. None: a machine's own.
votes: dict[str, dict[str, set[str]] | None] = {}
_archive_machines.clear()
ids_by_name: dict[str, set[str]] = {}
for key in sorted(profiles):
profile = profiles[key]
if profile.get("role") == "test":
continue
systems = [str(sys_id) for sys_id in profile.get("systems") or []]
for entry in profile.get("files") or []:
if not isinstance(entry, dict) or entry.get("category") != "bios_zip":
continue
owners = _owner_ids(entry, systems)
if not owners:
continue
archives = _entry_archives(entry, files, by_name)
_cast_votes(key, entry, owners, archives, votes, ids_by_name)
_elect_boards(votes, ids_by_name, db)
boards_built[0] = True
_arcade[0] = (sections_generation[0], boards)
def _read_arcade_source() -> tuple[dict, dict] | None:
"""The profiles and database of the repository, None without a database."""
from model.profiles import load_emulator_profiles
inputs = siteinputs.current()
database = inputs.db
if not database.exists():
return None
db = load_database(str(database))
profiles = load_emulator_profiles(str(inputs.emulators), skip_aliases=False)
return profiles, db
def _entry_archives(entry: dict, files: dict, by_name: dict) -> set[str]:
"""The archives of the collection a ``bios_zip`` entry names."""
from core.ziptools import zip_member_crcs
crcs = {
str(member["crc32"]).lower()
for member in entry.get("contents") or []
if isinstance(member, dict) and member.get("crc32")
}
names = {str(entry.get("name", ""))}
names.update(str(alias) for alias in entry.get("aliases") or [])
archives: set[str] = set()
for name in names:
found = by_name.get(name.rsplit("/", 1)[-1]) or []
for sha1 in [found] if isinstance(found, str) else found:
if sha1 not in files:
continue
if crcs:
held = zip_member_crcs(str(siteinputs.collection_path(files[sha1]["path"])))
# An archive the build cannot read keeps its name.
if held and not held & crcs:
continue
archives.add(sha1)
return archives
def _cast_votes(key: str, entry: dict, owners: set[str], archives: set[str],
votes: dict[str, dict[str, set[str]] | None],
ids_by_name: dict[str, set[str]]) -> None:
for owner in owners:
role = _board_role(owner)
if role == _MACHINE:
# The profile's only system stands for an entry that names
# none: MAME's printer and expansion sets in a Dragon
# profile. Only an entry naming the machine moves its set,
# and only when every machine it names has that section:
# epson_fx80.zip, which sixteen machines load, has none.
declared = entry.get("system") or entry.get("systems")
places = {_machine_section(o) for o in owners
if _board_role(o) == _MACHINE} if declared else {None}
section = next(iter(places)) if len(places) == 1 else None
for sha1 in archives:
votes[sha1] = None
_archive_machines.setdefault(sha1, set()).add(section)
continue
if role is None:
continue
ids_by_name.setdefault(role.casefold(), set()).add(owner)
for sha1 in archives:
ballot = votes.setdefault(sha1, {})
if ballot is not None:
ballot.setdefault(role.casefold(), set()).add(key)
def _elect_boards(votes: dict[str, dict[str, set[str]] | None],
ids_by_name: dict[str, set[str]], db: dict) -> None:
_archive_boards.clear()
_board_names.clear()
for sha1, ballot in votes.items():
if not ballot:
continue
ranked = sorted(ballot.items(), key=lambda item: (-len(item[1]), item[0]))
# Two boards named as often: a device ROM several boards share.
if len(ranked) > 1 and len(ranked[0][1]) == len(ranked[1][1]):
continue
name = ranked[0][0]
board = sorted(ids_by_name[name])[0]
_archive_boards[sha1] = board
if _in_arcade(sha1, db):
_board_names[name] = board
# A board none of whose archives sits under Arcade has no section there.
for sha1, board in list(_archive_boards.items()):
if system_name(board).casefold() not in _board_names:
del _archive_boards[sha1]
def _machine_section(sys_id: str) -> tuple[str, str] | None:
"""The (manufacturer, console) section SYSTEM_PARENTS files a machine
id under, outside Arcade. Only a reviewed parent moves an archive out
of the folder holding it: an id with a section of its own keeps its
archives where the collection files them."""
parent = SYSTEM_PARENTS.get(sys_id)
if parent is None or parent[0] == "Arcade" or parent not in section_pairs:
return None
return parent
def _boards() -> ArcadeBoards:
ensure_console_sections()
held = _arcade[0]
if held is not None and held[0] == sections_generation[0]:
return held[1]
boards = read_arcade_boards()
_arcade[0] = (sections_generation[0], boards)
return boards
def archive_machine(sha1: str) -> tuple[str, str] | None:
@@ -503,14 +282,12 @@ def archive_machine(sha1: str) -> tuple[str, str] | None:
the set of, when a profile declares it for one machine only: the Neo
Geo's neogeo.zip, the MZ-2000's mz2000.zip. Read from the same
``category: bios_zip`` entries as arcade_board(), never the folder."""
_build_boards()
places = _archive_machines.get(str(sha1), set())
places = _boards().archive_machines.get(str(sha1), frozenset())
return next(iter(places)) if len(places) == 1 else None
def _board_ids_by_name() -> dict[str, str]:
_build_boards()
return _board_names
def _board_ids_by_name() -> Mapping[str, str]:
return _boards().board_names
def _board_of_id(sys_id: str) -> str | None:
@@ -531,8 +308,7 @@ def arcade_board(sha1: str) -> str | None:
device ROM), or one a machine with its own section names (neogeo.zip
for the Neo Geo, cd32.zip for the CD32), has no board. Every id of a
board's name returns the same id, the first in sort order."""
_build_boards()
return _archive_boards.get(str(sha1))
return _boards().archive_boards.get(str(sha1))
def board_slug(sys_id: str) -> str:
@@ -564,8 +340,7 @@ def record_board_section(board: str) -> None:
def arcade_boards() -> dict[str, str]:
"""Every board, by board id -> display name."""
_build_boards()
return {board: system_name(board) for board in sorted(set(_board_names.values()))}
return {board: system_name(board) for board in sorted(set(_boards().board_names.values()))}
def scalar_text(key: object, value: object) -> str:
@@ -654,11 +429,6 @@ def by_mode(value: str | dict, mode: str) -> str:
return str(value or "")
# Of those, the top whose headings never name a system, even read whole.
# An Arcade heading can (Namco System 246), an Other one cannot.
_NAMELESS_TOPS = frozenset({"Other"})
# A system id that names a family of arcade boards -> the board its link
# leads to. RetroArch's sega-dreamcast-arcade lists the NAOMI, NAOMI 2 and
# Atomiswave BIOS: the NAOMI page comes first, the Dreamcast section links