diff --git a/.github/workflows/deploy-site.yml b/.github/workflows/deploy-site.yml index a56764e1f..0d1719cee 100644 --- a/.github/workflows/deploy-site.yml +++ b/.github/workflows/deploy-site.yml @@ -143,6 +143,7 @@ on: - "scripts/sitegen/__init__.py" - "scripts/sitegen/agents.py" - "scripts/sitegen/apiitems.py" + - "scripts/sitegen/arcadeboards.py" - "scripts/sitegen/bootstrap.py" - "scripts/sitegen/build.py" - "scripts/sitegen/cards.py" diff --git a/scripts/model/systemnames.py b/scripts/model/systemnames.py index d31fa2f29..422480fae 100644 --- a/scripts/model/systemnames.py +++ b/scripts/model/systemnames.py @@ -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).""" diff --git a/scripts/sitegen/arcadeboards.py b/scripts/sitegen/arcadeboards.py new file mode 100644 index 000000000..d29e3e365 --- /dev/null +++ b/scripts/sitegen/arcadeboards.py @@ -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) diff --git a/scripts/sitegen/build.py b/scripts/sitegen/build.py index 077397059..77908ac9e 100644 --- a/scripts/sitegen/build.py +++ b/scripts/sitegen/build.py @@ -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...") diff --git a/scripts/sitegen/render.py b/scripts/sitegen/render.py index a14b7ea5b..848a8e1a3 100644 --- a/scripts/sitegen/render.py +++ b/scripts/sitegen/render.py @@ -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 diff --git a/tests/test_site_arcade_boards.py b/tests/test_site_arcade_boards.py index 824ddc219..297ee1118 100644 --- a/tests/test_site_arcade_boards.py +++ b/tests/test_site_arcade_boards.py @@ -2,8 +2,8 @@ The Arcade pages were cut by storage folder, so NAOMI, ZN-1, PGM and the other boards had no section and stayed plain on 987 emulator rows. -siterender.arcade_board() names the board each set archive is the BIOS of, -read from the profiles, and system_section() leads every id of that board +sitegen.arcadeboards elects the board each set archive is the BIOS of, +read from the profiles, and siterender.arcade_board() names it, and system_section() leads every id of that board to ``systems/arcade-/#``, the section the system pages write. The 28 ids that systems.json gave files and no page either link or carry their reason below. @@ -20,6 +20,7 @@ import zipfile from pathlib import Path +from sitegen import arcadeboards from sitegen import render as siterender from model import systemnames from core.anchors import toc_slug @@ -67,8 +68,7 @@ STAYS_PLAIN = { def _reset() -> None: """Back to the repository's sections and boards, none written.""" - siterender._arcade_source[0] = None - systemnames.boards_built[0] = False + siterender._arcade[0] = None systemnames._sections_installed[0] = False siterender.reset_console_pages() @@ -238,16 +238,17 @@ class BoardRules(unittest.TestCase): archive.writestr("rom.bin", crc_member) return str(path) - def _install(self, entries: dict[str, list[dict]], files: dict[str, str]) -> None: + def _install(self, entries: dict[str, list[dict]], files: dict[str, str], + suffixes: dict[str, list[str]] | None = None) -> None: """``files``: sha1 -> path. Names are the paths' base names.""" db = { "files": {sha1: {"path": path, "name": Path(path).name} for sha1, path in files.items()}, - "indexes": {"by_name": {}, "by_path_suffix": {}}, + "indexes": {"by_name": {}, "by_path_suffix": dict(suffixes or {})}, } for sha1, path in files.items(): db["indexes"]["by_name"].setdefault(Path(path).name, []).append(sha1) profiles = {key: {"systems": [], "files": rows} for key, rows in entries.items()} - siterender.set_arcade_boards(profiles, db) + siterender.use_arcade_boards(arcadeboards.elect_arcade_boards(profiles, db)) def test_folder_decides_nothing(self) -> None: self._install( @@ -300,11 +301,9 @@ class BoardRules(unittest.TestCase): {"core": [{"name": "good.zip", "category": "bios_zip", "system": "igs-pgm", "contents": [{"name": "rom.bin", "crc32": crc}]}]}, {"e" * 40: good, "f" * 40: bad}, + # Both copies filed under Arcade, as by_path_suffix records it. + {"Arcade/MAME/good.zip": ["e" * 40], "Arcade/FBNeo/good.zip": ["f" * 40]}, ) - # Both copies filed under Arcade, as by_path_suffix records it. - db = siterender._arcade_source[0][1] - db["indexes"]["by_path_suffix"] = { - "Arcade/MAME/good.zip": ["e" * 40], "Arcade/FBNeo/good.zip": ["f" * 40]} self.assertEqual(siterender.arcade_board("e" * 40), "igs-pgm") self.assertIsNone(siterender.arcade_board("f" * 40)) diff --git a/tests/test_site_inputs.py b/tests/test_site_inputs.py index 206e8e94f..f3a20bf79 100644 --- a/tests/test_site_inputs.py +++ b/tests/test_site_inputs.py @@ -87,7 +87,7 @@ class TheGivenDatabase(_Inputs): def test_board_election_opens_the_given_collection_archive(self) -> None: import zipfile - from sitegen import render + from sitegen import arcadeboards archive = self.db.parent / "bios" / "X" / "board.zip" archive.parent.mkdir(parents=True) @@ -97,9 +97,9 @@ class TheGivenDatabase(_Inputs): files = {SHA1: {"path": "bios/X/board.zip"}} entry = {"name": "board.zip", "contents": [{"crc32": crc}]} self.use(db=self.db) - self.assertEqual(render._entry_archives(entry, files, {"board.zip": [SHA1]}), {SHA1}) + self.assertEqual(arcadeboards.entry_archives(entry, files, {"board.zip": [SHA1]}), {SHA1}) other = {"name": "board.zip", "contents": [{"crc32": "deadbeef"}]} - self.assertEqual(render._entry_archives(other, files, {"board.zip": [SHA1]}), set()) + self.assertEqual(arcadeboards.entry_archives(other, files, {"board.zip": [SHA1]}), set()) def test_archive_members_are_read_from_the_given_collection(self) -> None: import zipfile diff --git a/tests/test_site_render.py b/tests/test_site_render.py index 385edd49e..4d2c4f6ed 100644 --- a/tests/test_site_render.py +++ b/tests/test_site_render.py @@ -11,6 +11,7 @@ from unittest import mock import yaml +from sitegen import arcadeboards from sitegen import render as siterender from model import systemnames @@ -570,9 +571,9 @@ class TheArcadeCacheHoldsItsDatabase(unittest.TestCase): return {"files": {}, "indexes": {"by_path_suffix": {"Arcade/x.zip": [sha1]}}} def test_a_new_database_at_a_reused_address_is_read_again(self): - self.addCleanup(siterender._arcade_suffixes.clear) + self.addCleanup(arcadeboards._arcade_suffixes.clear) first = self._db("a" * 40) - self.assertTrue(siterender._in_arcade("a" * 40, first)) + self.assertTrue(arcadeboards.in_arcade("a" * 40, first)) address = id(first) del first # CPython hands a freed dict's address to a later one of its size, @@ -584,8 +585,8 @@ class TheArcadeCacheHoldsItsDatabase(unittest.TestCase): del second else: second = self._db("b" * 40) - self.assertTrue(siterender._in_arcade("b" * 40, second)) - self.assertFalse(siterender._in_arcade("a" * 40, second)) + self.assertTrue(arcadeboards.in_arcade("b" * 40, second)) + self.assertFalse(arcadeboards.in_arcade("a" * 40, second)) if __name__ == "__main__":