diff --git a/scripts/sitegen/build.py b/scripts/sitegen/build.py index 2c3366b16..077397059 100644 --- a/scripts/sitegen/build.py +++ b/scripts/sitegen/build.py @@ -262,10 +262,10 @@ def _write_system_pages(run: _SiteRun) -> None: }) # The arcade boards are recorded before any page links one, the - # system pages first among them. + # system pages and their install commands first among them. reset_console_pages() - sections, console_installs = arcade_board_sections( - manufacturers, system_install_choices(run.coverages)) + sections = arcade_board_sections(manufacturers) + console_installs = system_install_choices(run.coverages) linked, total = link_coverage( system_ids(run.profiles, [cov["config"] for cov in run.coverages.values()])) print(f" {linked} of {total} system ids linked") diff --git a/scripts/sitegen/cards.py b/scripts/sitegen/cards.py index 82c2da6e9..22e57a69e 100644 --- a/scripts/sitegen/cards.py +++ b/scripts/sitegen/cards.py @@ -23,7 +23,7 @@ from model.mamerelease import tag_version from sitegen.fields import search_excluded from sitegen.install import manifest_systems, platform_display from sitegen.render import ( - SHORT_HASH, card_anchor, code_span, count_text, emulator_card_page, system_page_map, + SHORT_HASH, card_anchor, code_span, count_text, emulator_card_page, system_section, ) @@ -750,18 +750,19 @@ def card_title(text: str, anchor: str, searchable: bool = True, weight: int = 10 def system_install_choices(coverages: dict) -> dict[str, dict[str, list[tuple[str, str]]]]: """slug -> console -> the (platform, system id) pairs linked to it. - Read through the same table as every system link, so a command sits in - the section the platform's system links to. Only a system the - platform's install manifest records is offered: --system refuses any - other name. + Placed by system_section, the answer every system link reads, so a + command sits in the section the platform's system links to: the folder + votes put Game Boy Color's under Game Boy and Super A'Can's under + Arcade. A system with no section (vgmplay, vvvvvv) links nowhere and + offers no command there. Call once the arcade boards are recorded, so a + board's systems reach the board's section. Only a system the platform's + install manifest records is offered: --system refuses any other name. """ - from sitegen.render import page_by_norm - choices: dict[str, dict[str, list[tuple[str, str]]]] = {} for name, cov in sorted(coverages.items()): recorded = manifest_systems(name) for sys_id in sorted(set(cov["config"].get("systems") or {}) & recorded): - page = system_page_map.get(sys_id) or page_by_norm(sys_id) + page = system_section(sys_id) if page: slug, console = page choices.setdefault(slug, {}).setdefault(console, []).append((name, sys_id)) diff --git a/scripts/sitegen/places.py b/scripts/sitegen/places.py index f8d3c3135..6cee5755d 100644 --- a/scripts/sitegen/places.py +++ b/scripts/sitegen/places.py @@ -1,4 +1,4 @@ -"""Which system page each system id leads to, read from the collection's folders.""" +"""Where the collection's folders place each system id: the run's set of system ids.""" from __future__ import annotations diff --git a/scripts/sitegen/render.py b/scripts/sitegen/render.py index 68147f4b3..a14b7ea5b 100644 --- a/scripts/sitegen/render.py +++ b/scripts/sitegen/render.py @@ -117,7 +117,9 @@ def system_icon_markdown(manufacturer: str, console_name: str) -> str: return "" return markup.image(console_name, _icon_url(name), width="24") + " " -# Global index: maps system_id -> (manufacturer_slug, console_name) for cross-linking +# The system ids the collection's folders place (sitegen.places), by the +# folder each is placed in. Read as the run's set of system ids: a link, an +# install command and a card's maker go where system_section() says. system_page_map: dict[str, tuple[str, str]] = {} def slugify_anchor(text: str) -> str: @@ -130,39 +132,10 @@ def slugify_anchor(text: str) -> str: slug = slug.strip("-") return slug -_norm_page_map: dict[str, tuple[str, str] | None] = {} - - def set_system_page_map(mapping: dict[str, tuple[str, str]]) -> None: - """Install the system id -> page table every link on the site reads. - - The table replaces the previous one, and the index of other spellings is - derived from it here. Kept anywhere else, it outlived a table of the same - size and linked to the pages of the previous one. - """ - from model.systems import norm_system_id - + """Install the run's placed system ids, replacing the previous table.""" system_page_map.clear() system_page_map.update(mapping) - _norm_page_map.clear() - for known, page in system_page_map.items(): - key = norm_system_id(known) - if _norm_page_map.get(key, page) != page: - _norm_page_map[key] = None - else: - _norm_page_map[key] = page - - -def page_by_norm(sys_id: str) -> tuple[str, str] | None: - """The page an id leads to under another spelling of the same system. - - Emulator cards name sega-megadrive or sinclair-zxspectrum, the pages are - keyed by the platforms' spellings: 2,431 of 4,195 cards had no link. A - spelling two pages share leads nowhere rather than to either. - """ - from model.systems import norm_system_id - - return _norm_page_map.get(norm_system_id(sys_id)) # A manufacturer page too large for one page is a summary, each console on diff --git a/scripts/sitegen/structured.py b/scripts/sitegen/structured.py index 43915417e..7d9056135 100644 --- a/scripts/sitegen/structured.py +++ b/scripts/sitegen/structured.py @@ -17,7 +17,7 @@ from sitegen.descriptions import EMULATOR_SNIPPETS, NOTHING_TO_FETCH, PAGE_DATA, from sitegen.emulatorcards import snippet_noun from sitegen.install import comes_with_app from sitegen.lookup import data_member_digests, hash_pairs -from sitegen.render import card_anchor, system_page_map +from sitegen.render import card_anchor, system_section from sitegen.write import write_if_changed @@ -302,7 +302,9 @@ def record_emulator_page(name: str, profile: dict) -> None: fetched = [f for f in listed if f.get("name") and not comes_with_app(f)] systems = profile.get("systems") or [] first = systems[0] if systems else "" - makers = [system_page_map[sys_id][0] for sys_id in systems if sys_id in system_page_map] + # The maker of the section each system links to, as the system pages + # place it. + makers = [section[0] for section in (system_section(str(sid)) for sid in systems) if section] if makers: sitestarlight.EMULATOR_CARDS[f"emulators/{name}"] = makers[0] else: diff --git a/scripts/sitegen/systempages.py b/scripts/sitegen/systempages.py index 388598f6f..5f0809bad 100644 --- a/scripts/sitegen/systempages.py +++ b/scripts/sitegen/systempages.py @@ -137,30 +137,10 @@ class _BoardSort: *(f for f in files if str(f.get("sha1", "")) not in held)] -def _installs_by_board( - installs: dict[str, dict[str, list[tuple[str, str]]]], -) -> dict[str, dict[str, list[tuple[str, str]]]]: - """A system's install commands, moved to its board's section.""" - from sitegen.render import system_section - - moved: dict[str, dict[str, list[tuple[str, str]]]] = {} - for slug, consoles in installs.items(): - for console, pairs in consoles.items(): - for platform, sys_id in pairs: - section = system_section(sys_id) - if section and section[0] == "arcade" and section[1] in _BOARD_CONSOLES: - slug_to, console_to = section - else: - slug_to, console_to = slug, console - moved.setdefault(slug_to, {}).setdefault(console_to, []).append((platform, sys_id)) - return moved - - def arcade_board_sections( manufacturers: dict[str, dict[str, list]], - installs: dict[str, dict[str, list[tuple[str, str]]]], -) -> tuple[dict[str, dict[str, list]], dict[str, dict[str, list[tuple[str, str]]]]]: - """The Arcade page grouped by board, and each board's install commands. +) -> dict[str, dict[str, list]]: + """The Arcade page grouped by board. The Arcade sections were the folders of bios/Arcade/: NAOMI, PGM and ZN-1 had no section, and their names stayed plain on 568 emulator @@ -170,15 +150,16 @@ def arcade_board_sections( copy. A set a reviewed machine owns (sitegen.render.archive_machine) goes to that machine's section. Files neither claims stay in their folders. Each board is recorded (sitegen.render.record_board_section) before - any page links one, and a system's install commands follow its link to - the board's section. Call after reset_console_pages().""" + any page links one, so the install commands placed after it + (sitegen.cards.system_install_choices) follow a system's link to the + board's section. Call after reset_console_pages().""" from sitegen.render import arcade_boards, record_board_section _BOARD_CONSOLES.clear() _BOARDS_FROM.clear() arcade = manufacturers.get("Arcade") if not arcade: - return manufacturers, installs + return manufacturers names = arcade_boards() by_name = {name.casefold(): board for board, name in names.items()} sort = _BoardSort() @@ -191,7 +172,7 @@ def arcade_board_sections( regrouped = {**sort.folders, **{names[board]: files for board, files in sort.boards.items()}} makers = {mfr: {console: files for console, files in consoles.items() if files} for mfr, consoles in sort.makers.items()} - return {**manufacturers, **makers, "Arcade": regrouped}, _installs_by_board(installs) + return {**manufacturers, **makers, "Arcade": regrouped} def _boards_line(manufacturer: str, console: str) -> list[str]: diff --git a/tests/test_site_exports.py b/tests/test_site_exports.py index e83c0ce0d..9818205f5 100644 --- a/tests/test_site_exports.py +++ b/tests/test_site_exports.py @@ -1224,13 +1224,17 @@ class TheOneLinerIsWhereUsersLand(unittest.TestCase): from sitegen import cards from sitegen import render as siterender - saved = dict(siterender.system_page_map) - self.addCleanup(siterender.set_system_page_map, saved) - siterender.set_system_page_map({"sony-playstation": ("sony", "PlayStation"), - "nowhere": ("sony", "PlayStation")}) - cov = {"retroarch": {"config": {"systems": {"sony-playstation": {}, "nowhere": {}}}}} + from model import systemnames + + systemnames.set_console_sections({("Sony", "PlayStation")}) + self.addCleanup(systemnames._sections_installed.__setitem__, 0, False) + siterender.reset_console_pages() + self.addCleanup(siterender.reset_console_pages) + # "playstation" leads to the same section, but the manifest records + # retroarch's own spelling only. + cov = {"retroarch": {"config": {"systems": {"sony-playstation": {}, "playstation": {}}}}} choices = cards.system_install_choices(cov) - self.assertEqual(choices["sony"]["PlayStation"], [("retroarch", "sony-playstation")]) + self.assertEqual(choices, {"sony": {"PlayStation": [("retroarch", "sony-playstation")]}}) class TheGapPageOpensOnCounts(unittest.TestCase): diff --git a/tests/test_site_system_links.py b/tests/test_site_system_links.py index df984ad60..41492b061 100644 --- a/tests/test_site_system_links.py +++ b/tests/test_site_system_links.py @@ -81,32 +81,6 @@ class OtherSpellingsOfOneSystem(unittest.TestCase): self.assertEqual(platformfiles.system_emulators("sega-mega-drive", index), ["genesis_plus_gx"]) self.assertEqual(platformfiles.system_emulators("nintendo-snes", index), []) - def _install(self, mapping: dict) -> None: - from sitegen import render as siterender - - saved = dict(siterender.system_page_map) - self.addCleanup(siterender.set_system_page_map, saved) - siterender.set_system_page_map(mapping) - - def test_a_profile_spelling_finds_the_platform_page(self): - from sitegen import render as siterender - - self._install({"sega-mega-drive": ("sega", "Mega Drive")}) - self.assertEqual(siterender.page_by_norm("sega-megadrive"), ("sega", "Mega Drive")) - self._install({"sega-mega-drive": ("sega", "Mega Drive"), - "megadrive": ("other", "Elsewhere")}) - self.assertIsNone(siterender.page_by_norm("sega-megadrive")) - - def test_a_table_of_the_same_size_replaces_the_other_spellings(self): - """The index of other spellings was kept while the table kept its - length, so a second table of one entry linked to the first one's page.""" - from sitegen import render as siterender - - self._install({"sega-mega-drive": ("sega", "Mega Drive")}) - self.assertEqual(siterender.page_by_norm("sega-megadrive"), ("sega", "Mega Drive")) - self._install({"sega-mega-drive": ("other", "Elsewhere")}) - self.assertEqual(siterender.page_by_norm("sega-megadrive"), ("other", "Elsewhere")) - def test_a_system_only_profiles_name_is_placed_by_their_files(self): manufacturers = {"Commodore": {"PET": [{"name": "basic4.bin"}]}} profiles = {"vice_xpet": {"systems": ["commodore-pet"], diff --git a/tests/test_site_system_sections.py b/tests/test_site_system_sections.py index 60641d3f6..c7c73bad8 100644 --- a/tests/test_site_system_sections.py +++ b/tests/test_site_system_sections.py @@ -167,12 +167,14 @@ class SystemLinks(unittest.TestCase): siterender.reset_console_pages() # The sections as the system pages hold them: each board, the # sets a reviewed machine owns under that machine. - cls.sections, _installs = arcade_board_sections(cls.manufacturers, {}) + cls.sections = arcade_board_sections(cls.manufacturers) platforms = [ load_platform_config(name, "platforms") for name in list_registered_platforms("platforms") ] cls.ids = siterender.system_ids(profiles, platforms) + cls.coverages = {name: {"config": config} for name, config in + zip(list_registered_platforms("platforms"), platforms)} entries = _entries_by_system(platforms, profiles) cls.held: dict[str, set[tuple[str, str]]] = _held_sections( @@ -196,6 +198,34 @@ class SystemLinks(unittest.TestCase): ) self.assertEqual(plain, [], "link these or name them in UNLINKED with the reason") + def test_install_commands_sit_where_the_system_links(self) -> None: + """The commands were placed by the folder votes of sitegen.places + and the links by system_section: Game Boy Color's commands sat under + Game Boy, Super A'Can's under Arcade, while their links led to their + own sections.""" + from sitegen.cards import system_install_choices + from sitegen.install import manifest_systems + + placed = { + (platform, sys_id): (slug, console) + for slug, consoles in system_install_choices(self.coverages).items() + for console, pairs in consoles.items() + for platform, sys_id in pairs + } + wrong = sorted( + (pair, place, siterender.system_section(pair[1])) + for pair, place in placed.items() + if siterender.system_section(pair[1]) != place + ) + self.assertEqual(wrong, []) + unplaced = sorted( + (name, sys_id) + for name, cov in self.coverages.items() + for sys_id in set(cov["config"].get("systems") or {}) & manifest_systems(name) + if siterender.system_section(sys_id) and (name, sys_id) not in placed + ) + self.assertEqual(unplaced, []) + def test_unlinked_list_is_current(self) -> None: stale = sorted( sys_id for sys_id in UNLINKED