refactor: public names for cross-module reads

This commit is contained in:
Abdessamad Derraz committed 2026-11-18 22:59:51 +01:00
1 parent f371790dc6
commit 3853ca2f1c
254 files changed
+2955 -2969

No files matched your search

+27 -27
View File
@@ -5,12 +5,12 @@ from __future__ import annotations
import re
from typing import TypeVar
from model.collection import _profiles_on_disk
from model.collection import profiles_on_disk
from model.composition import GAME_DATA_TOPS
from model.guides import INSTALL_PS1, INSTALL_SH
from model.profiles import entry_system_ids
from packs.crossref import FileConsumers
from sitegen.cards import _emulator_display, _romset_releases, _system_file_card
from sitegen.cards import emulator_display, romset_releases, system_file_card
# A manufacturer page above this much Markdown becomes a summary with one
@@ -41,7 +41,7 @@ SYSTEM_SUMMARY_HTML = 500_000
FITTED_ADDRESS_CHARS = {INSTALL_PS1: 67, INSTALL_SH: 66}
def _install_box_weight(lines: list[str]) -> int:
def install_box_weight(lines: list[str]) -> int:
weight = 0
for line in lines:
weight += len(line) + 1
@@ -54,7 +54,7 @@ def _install_box_weight(lines: list[str]) -> int:
# A system card: whether it is a variant, its lines, its sha1.
Card = tuple[bool, list[str], str]
_Item = TypeVar("_Item")
Item = TypeVar("Item")
def _reading_entries(sha1: str, consumers: FileConsumers) -> list[dict]:
@@ -68,10 +68,10 @@ def _reading_entries(sha1: str, consumers: FileConsumers) -> list[dict]:
return entries
_NO_PRIORITY = 1 << 30
NO_PRIORITY = 1 << 30
def _card_rank(f: dict, consumers: FileConsumers, start: dict[str, int] | None = None) -> tuple:
def card_rank(f: dict, consumers: FileConsumers, start: dict[str, int] | None = None) -> tuple:
"""Where a file's card sits in its console: the files the section's
start line names first, in its order (``start``, sha1 -> place), then
the files an emulator requires, then by the emulators' search order
@@ -89,9 +89,9 @@ def _card_rank(f: dict, consumers: FileConsumers, start: dict[str, int] | None =
priorities = [e["priority"] for e in entries if isinstance(e.get("priority"), int)]
required = any(e.get("required", True) for e in entries)
return (
(start or {}).get(sha1, _NO_PRIORITY),
(start or {}).get(sha1, NO_PRIORITY),
not required,
min(priorities, default=_NO_PRIORITY),
min(priorities, default=NO_PRIORITY),
-len(entries) if required else 0,
-len(consumers.platforms.get(sha1, ())),
f["name"].casefold(), f["name"], f.get("path", ""),
@@ -102,7 +102,7 @@ def _by_name(f: dict) -> tuple:
return f["name"].casefold(), f["name"], f.get("path", "")
def _console_cards(
def console_cards(
manufacturer: str,
files: list[dict],
consumers: FileConsumers,
@@ -120,13 +120,13 @@ def _console_cards(
def order(f: dict) -> tuple:
if by_name:
return (place.get(str(f.get("sha1", "")), _NO_PRIORITY), *_by_name(f))
return _card_rank(f, consumers, place)
return (place.get(str(f.get("sha1", "")), NO_PRIORITY), *_by_name(f))
return card_rank(f, consumers, place)
# Another revision of a MAME or FBNeo set is another release's set, its
# Romset line saying which: it sits open beside the set of its name.
# Twelve neogeo.zip were on five pages, the SNK ones closed under
# Variants.
romsets = _romset_releases()
romsets = romset_releases()
beside = [f for f in files if "/.variants/" in f["path"] and f["name"] in romsets]
main_files = [f for f in files if "/.variants/" not in f["path"]]
variant_files = [f for f in files if "/.variants/" in f["path"] and f not in beside]
@@ -143,9 +143,9 @@ def _console_cards(
elif by_set.get(f["name"]):
ordered.extend(by_set.pop(f["name"]))
ordered.extend(f for files in by_set.values() for f in files)
cards = [(False, _system_file_card(f, consumers, anchored, searchable), str(f.get("sha1", "")))
cards = [(False, system_file_card(f, consumers, anchored, searchable), str(f.get("sha1", "")))
for f in ordered]
cards += [(True, _system_file_card(v, consumers, anchored, searchable), str(v.get("sha1", "")))
cards += [(True, system_file_card(v, consumers, anchored, searchable), str(v.get("sha1", "")))
for v in sorted(variant_files, key=order)]
return cards
@@ -155,7 +155,7 @@ def _console_cards(
START_WITH_NAMES = 6
def _region_order() -> dict[str, int]:
def region_order() -> dict[str, int]:
"""The regions a start line lists, the territories first. A file read
for every region is a region-free stand-in for the BIOS of each one:
the PlayStation section opened on psxonpsp660.bin, which one emulator
@@ -169,7 +169,7 @@ def _region_order() -> dict[str, int]:
return {region: index for index, region in enumerate(ordered)}
def _reading_pairs(sha1: str, consumers: FileConsumers) -> list[tuple[str, dict]]:
def reading_pairs(sha1: str, consumers: FileConsumers) -> list[tuple[str, dict]]:
"""The profile entries that read a file, each with its emulator."""
files_of = getattr(consumers, "emulator_files", {})
pairs = []
@@ -180,12 +180,12 @@ def _reading_pairs(sha1: str, consumers: FileConsumers) -> list[tuple[str, dict]
return pairs
def _entry_systems(emulator: str, entry: dict) -> list[str]:
def entry_systems(emulator: str, entry: dict) -> list[str]:
"""The systems an entry is read for: its own `system:` or `systems:`
(a MAME device set several systems load), else its profile's only one."""
if entry.get("system") or entry.get("systems"):
return entry_system_ids(entry, {})
systems = (_profiles_on_disk().get(emulator) or {}).get("systems") or []
systems = (profiles_on_disk().get(emulator) or {}).get("systems") or []
return [str(systems[0])] if len(systems) == 1 else []
@@ -193,7 +193,7 @@ def _entry_systems(emulator: str, entry: dict) -> list[str]:
_SECTION_ENTRIES: dict[int, tuple[object, dict[tuple[str, str], list]]] = {}
def _entries_by_section(consumers: FileConsumers) -> dict[tuple[str, str], list]:
def entries_by_section(consumers: FileConsumers) -> dict[tuple[str, str], list]:
"""Each console section with the (sha1, emulator, entry, system) whose
system leads to it, read once per build."""
from sitegen.render import system_section
@@ -204,8 +204,8 @@ def _entries_by_section(consumers: FileConsumers) -> dict[tuple[str, str], list]
sections: dict[str, tuple[str, str] | None] = {}
index: dict[tuple[str, str], list] = {}
for sha1 in sorted(getattr(consumers, "emulator_entries", {})):
for emulator, entry in _reading_pairs(sha1, consumers):
for sys_id in _entry_systems(emulator, entry):
for emulator, entry in reading_pairs(sha1, consumers):
for sys_id in entry_systems(emulator, entry):
if sys_id not in sections:
sections[sys_id] = system_section(sys_id)
place = sections[sys_id]
@@ -216,7 +216,7 @@ def _entries_by_section(consumers: FileConsumers) -> dict[tuple[str, str], list]
return index
def _setup_name(f: dict, consumers: FileConsumers, pairs: list[tuple[str, dict]]) -> str:
def setup_name(f: dict, consumers: FileConsumers, pairs: list[tuple[str, dict]]) -> str:
"""The name most setups give a file, else most emulators: the collection
keeps BizHawk's GBA_bios.rom where eight setups write gba_bios.bin."""
counts: dict[str, int] = {}
@@ -236,22 +236,22 @@ def _setup_name(f: dict, consumers: FileConsumers, pairs: list[tuple[str, dict]]
return f["name"] if f["name"] in top else top[0]
def _without_files(emulators: list[str]) -> str:
def without_files(emulators: list[str]) -> str:
"""Who runs without the files: every emulator reading one, up to three
by name, more counted, as their order would rank nothing."""
names = sorted({_emulator_display(e) for e in emulators}, key=str.casefold)
names = sorted({emulator_display(e) for e in emulators}, key=str.casefold)
if len(names) > 3:
return f": none of its {len(names)} emulators requires one"
return " with " + (", ".join(names[:-1]) + f" or {names[-1]}" if len(names) > 1 else names[0])
def _visible(name: str) -> str:
def visible_name(name: str) -> str:
"""The file name a start line shows, out of its code span and link."""
match = re.match(r"\[?`([^`]*)`", name)
return match[1] if match else name
def _add_name(names: list[str], name: str) -> None:
def add_name(names: list[str], name: str) -> None:
"""A name once per line: two copies of sfcbox.zip read as one file."""
if all(_visible(n) != _visible(name) for n in names):
if all(visible_name(n) != visible_name(name) for n in names):
names.append(name)