Files
libretro/scripts/sitegen/emulatorcards.py
T
Abdessamad Derraz 2786b3be34 refactor: split the emulator card module
File badges and the romset and version rows get modules of their own.
2026-11-24 06:02:03 +01:00

753 lines
31 KiB
Python

"""An emulator file's card: badges, place, size, hashes and copies."""
from __future__ import annotations
import html
import os
import re
import urllib.parse
from collections.abc import Callable, Iterable, Mapping
import dataclasses
from dataclasses import dataclass
from core.sizes import size_text
from model.manufacturers import manufacturer_slug
from sitegen.runsections import system_name
from packs.consumers import FileConsumers, entry_target, held_copy
from resolve.cores import resolve_platform_cores
from sitegen import markup
from sitegen.cards import (
SEARCH_SKIPPED,
card_title,
listed_words,
places_markdown,
platform_links,
platform_places,
search_skipped_apart,
card_fold,
)
from sitegen.config import REPO_URL
from sitegen.fields import hash_spans
from sitegen.install import (
platform_display,
comes_with_app,
data_root_words,
standalone_place,
)
from sitegen.render import SHORT_HASH, card_anchor, code_span, count_text, escape_tags
from sitegen.runlinks import card_page, console_href
from sitegen.sources import (
source_permalink,
source_ref_markdown,
link_prose_citations,
requested_by,
)
from sitegen.filebadges import file_badges
# Emulator pages
def named_system_link(sys_id: str, prefix: str = "") -> str:
"""A system by its display name, linked where runlinks.system_link links it.
The emulator pages printed the id ("sony-playstation") where every
other page says "Sony PlayStation". The id stays in the address only.
"""
from sitegen.runlinks import system_section
name = system_name(sys_id).replace("[", r"\[").replace("]", r"\]")
section = system_section(sys_id)
if not section:
return name
slug, console = section
return f"[{name}]({console_href(slug, console, prefix)})"
def snippet_noun(names: Iterable[str], firmware: int, fetched: int) -> str:
"""What an emulator's files are called in its snippet. A Switch
emulator reads keys, and "eden keys" is what readers search for. Ship
of Harkinian reads game ROMs, which are no BIOS."""
names = list(names)
keys = sum("key" in name.casefold() for name in names)
if keys >= 2 and any("firmware" in name.casefold() for name in names):
return "keys and firmware"
if names and keys * 2 > len(names):
return "keys"
return "BIOS" if firmware * 2 >= fetched else "files"
_LOAD_FROM = {
"system_dir": "the system (BIOS) directory",
"save_dir": "the save directory, not the BIOS directory",
"content_dir": "the folder of the loaded content, not the BIOS directory",
"working_directory": "the working directory, not the BIOS directory",
"data_dir": "the emulator's data directory, not the BIOS directory",
"hdd_image": "inside the emulated hard disk image, not the BIOS directory",
"internal_storage": "the app's internal storage, not the BIOS directory",
}
def _by_build(value: object, words: Callable[[str], str]) -> str:
"""A field keyed by build, said per build, or a plain value said once."""
if isinstance(value, dict):
return ", ".join(f"{words(str(v))} ({mode} build)" for mode, v in value.items() if v)
return words(str(value)) if value else ""
def _load_from_text(value: object) -> str:
"""Where the code reads a file, in words.
The card showed the Path alone: Dolphin's libretro core reads
gba_bios.bin from its save directory, and a reader put it under system/.
"""
if value in (None, "", "system_dir"):
return ""
return _by_build(value, lambda v: _LOAD_FROM.get(v, code_span(v)))
def _found_by_text(value: object) -> str:
"""How the code finds a file: by the name it opens, or by content."""
words = {
"name": "file name (the code opens this name)",
"content": "content (the code scans the folder and identifies the file "
"by its hash, any file name works)",
}
return _by_build(value, lambda v: words.get(v, code_span(v)))
def _on_mismatch_lines(value: object) -> list[str]:
"""What the code does when a declared check fails, where it is not reject."""
said = {"warn": "warns and runs with the file", "reject": "rejects the file"}
if value == "warn":
return [f"On a failed check: {said['warn']}"]
if isinstance(value, dict):
return [f"On a failed {check} check: {said.get(str(how), code_span(str(how)))}"
for check, how in value.items()]
return []
def emulator_setups(coverages: dict, profiles: dict) -> dict[str, list[str]]:
"""Each emulator with the platforms that run it, by display name."""
setups: dict[str, list[str]] = {}
for platform, cov in coverages.items():
for emulator in resolve_platform_cores(cov["config"], profiles):
setups.setdefault(emulator, []).append(platform)
return {
emulator: sorted(names, key=lambda p: platform_display(p).casefold())
for emulator, names in setups.items()
}
def _validation_names(validation: dict | list | None) -> set[str]:
"""The checks a `validation:` names, a list or one list per build."""
if isinstance(validation, dict):
return {str(v) for checks in validation.values() for v in checks or []}
return {str(v) for v in validation or []}
def _exact_size(size: int) -> str:
"""A rounded size with its bytes, where the bytes are not the same figure."""
return size_text(size) if size < 1024 else f"{size_text(size)} ({size:,} bytes)"
def _build_file_links(pin_build: list[dict]) -> list[str]:
"""Each platform build file that holds the revision, linked at the
commit it was read at when the source record holds it. The prose of
pin_frozen named the file as plain text."""
from sitegen.sources import browse_repo, tree_holds
links = []
for build in pin_build:
repo = browse_repo(str(build["repo"]))
path = str(build["path"])
revision = str(build["revision"])
text = f"{escape_tags(platform_display(str(build['platform'])))} {code_span(path)}"
# Linked where the record saw the file at that commit, as every
# source link is: a tag can move, and is never recorded.
if repo is not None and tree_holds(repo, revision, path):
text = f"[{text}]({source_permalink(repo, revision, path, None, None)})"
links.append(f"{text} at {code_span(str(build['revision']))} sets {code_span(str(build['variable']))}")
return links
def build_rows(profile: dict) -> list[str]:
"""Meta card rows for who ships the build, how its files are found, why
its revision is held and where its releases are announced."""
rows = []
built_by = profile.get("built_by") or []
if built_by:
names = ", ".join(f"[{p}](../platforms/{p}.md)" for p in map(str, built_by))
rows.append(f"| Built by | {names}, under its core name. The libretro "
"buildbot does not publish this build. |")
found = _found_by_text(profile.get("found_by"))
if found:
rows.append(f"| Files found by | {found} |")
# Each pin says why it is held: a bare upstream revision read as a
# profile left behind HEAD (mupen64plus, aps3e).
for field, label in (("pin_frozen", "Revision held"),
("upstream_frozen", "Upstream revision held")):
frozen = profile.get(field)
if frozen:
held = escape_tags(' '.join(str(frozen).split())).replace('|', r'\|')
rows.append(f"| {label} | {held} |")
builds = _build_file_links(profile.get("pin_build") or [])
if builds:
rows.append(f"| Build file | {'<br>'.join(builds)} |")
announced = profile.get("version_source")
if isinstance(announced, dict) and announced.get("url"):
url = str(announced["url"])
row = f"| Releases announced at | [{url}]({url})"
if announced.get("no_genuine_copy"):
row += (f" (version {escape_tags(str(announced['no_genuine_copy']))} "
"announced there, found only as rebuilt copies)")
rows.append(row + " |")
return rows
def entry_label(f: dict, profile: dict) -> str:
"""The name a file card goes by, its title without the badges."""
fname = f.get("name", "")
if f is any_name_bios(profile):
# Found by content: the name is an example, any image of the system works.
system = system_name((profile.get("systems") or [""])[0])
return (f"a {system} BIOS (example: {code_span(fname)})" if named_example(str(fname))
else f"a {system} BIOS, any file name")
return code_span(fname)
@dataclass(frozen=True)
class CardLayout:
"""Where a file card lands. `alias` is a second id the card answers to,
`data_href` links the data folder an app reads the entry under, from the
page the card lands on. `badges_apart`: the title holds the name alone,
for a table whose Status column shows the badges (sitegen/entries.py)."""
alias: str = ""
data_href: str = "#data-folder"
badges_apart: bool = False
_CARD_LAYOUT = CardLayout()
def _card_place_details(f: dict, profile: dict, owner: str, data_href: str) -> list[str]:
"""Where the file goes and how the code finds it."""
details = []
fname = f.get("name", "")
fpath = f.get("path", "")
fsystem = f.get("system", "")
# Where the app reads it under its data folder, said beside the path:
# Ryujinx's card read "Path: system/prod.keys" with no root.
place = standalone_place(profile, f) if owner else ""
under = f", in [the data folder]({data_href})"
standalone_path = f.get("standalone_path", "")
if (fpath and fpath != fname) or (place and place == fpath):
details.append(f"Path: `{fpath}`" + (under if place and place == fpath else ""))
if standalone_path and standalone_path != fpath:
# The standalone build reads it elsewhere: MesenCE's dsp1.rom goes
# under Firmware/, and a page naming only the core's path sent its
# users to the wrong folder.
details.append(f"Standalone path: `{standalone_path}`"
+ (under if place == standalone_path else ""))
read_from = _load_from_text(f.get("load_from"))
if read_from:
details.append(f"Read from: {read_from}")
found = _found_by_text(f.get("found_by"))
if found:
details.append(f"Found by: {found}")
if fsystem:
details.append(f"System: {named_system_link(fsystem, '../')}")
return details
def _card_size_details(f: dict) -> list[str]:
"""The exact sizes, then the window the code also accepts (size_refusal)."""
size = f.get("size")
fmin = f.get("min_size")
fmax = f.get("max_size")
# Where the code compares the size, the exact bytes: raze tells three
# DUKE3D.GRP apart by size, all three shown as 42.3 MB.
sized = _exact_size if "size" in _validation_names(f.get("validation", [])) else size_text
bounds = []
if fmin:
bounds.append(f"min {sized(fmin)}")
if fmax:
bounds.append(f"max {sized(fmax)}")
if size:
if isinstance(size, list):
size_str = " / ".join(sized(s) for s in size)
else:
size_str = sized(size)
if bounds:
size_str += f", or {', '.join(bounds)}"
return [f"Size: {size_str}"]
if bounds:
return [f"Size: {', '.join(bounds)}"]
return []
def _card_hash_details(f: dict) -> list[str]:
"""Every declared hash."""
details = []
fsha1 = f.get("sha1", "")
fmd5 = f.get("md5", "")
fcrc32 = f.get("crc32", "")
fsha256 = f.get("sha256", "")
fadler32 = f.get("known_hash_adler32", "")
# Starlight folds the hashes and gives each a copy button.
copyable = markup.starlight()
if fsha1:
details.append(f"SHA1: {hash_spans(fsha1, copyable)}")
if fmd5:
details.append(f"MD5: {hash_spans(fmd5, copyable)}")
if fcrc32:
crcs = fcrc32 if isinstance(fcrc32, list) else [fcrc32]
details.append("CRC32: " + ", ".join(
markup.hash_value(crc, True) if copyable else f"`{crc}`" for crc in crcs))
if fsha256:
details.append(f"SHA256: {hash_spans(fsha256, copyable)}")
if fadler32:
# Printed as the downloads spell a hash, one per code span: the list
# repr read `['0x66f334fe', ...]`. Dolphin hashes its DSP ROMs over
# byte-swapped 16-bit words, so the raw Adler-32 the downloads carry
# for the same file differs, and the card says which one it lists.
values = fadler32 if isinstance(fadler32, list) else [fadler32]
shown = ", ".join(code_span(str(v).lower().removeprefix("0x")) for v in values)
if f.get("adler32_byteswap"):
details.append(
f"Adler32 over the file's 16-bit words byte-swapped: {shown} "
"(the Adler-32 in the downloads is the file's own and differs)"
)
else:
details.append(f"Adler32: {shown}")
return details
def _validation_lines(validation: object) -> list[str]:
"""The checks the code runs, one line, or one line per build."""
if isinstance(validation, list):
return [f"Validation: {', '.join(validation)}"]
if isinstance(validation, dict):
return [f"Validation ({scope}): {', '.join(checks)}" for scope, checks in validation.items()]
return []
def _card_reading_details(f: dict, profile: dict) -> list[str]:
"""How the code reads the file, on which setups, and where it asks for it."""
details = []
aliases = f.get("aliases", [])
priority = f.get("priority")
fast_boot = f.get("fast_boot")
validation = f.get("validation", [])
source_ref = f.get("source_ref", "")
if aliases:
details.append(f"Aliases: {', '.join(f'`{a}`' for a in aliases)}")
if priority is not None:
details.append(f"Priority: {priority}")
if fast_boot is not None:
details.append(f"Fast boot: {'yes' if fast_boot else 'no'}")
if validation:
details.extend(_validation_lines(validation))
details.extend(_on_mismatch_lines(f.get("on_mismatch")))
if f.get("installed_on"):
details.append("Installed by the platform's own package on: "
+ ", ".join(sorted(f["installed_on"])))
if f.get("not_bundled_on"):
details.append("Not in the platform's package, so the pack brings it, on: "
+ ", ".join(sorted(f["not_bundled_on"])))
if f.get("unread_on"):
details.append("Never opened on these platforms, whose launcher names other files: "
+ ", ".join(sorted(f["unread_on"])))
if source_ref:
asker = requested_by(profile, source_ref)
if asker:
# A profile reading a libretro core and the original emulator
# cited both trees alike: readers could not tell which code asks
# for the file.
details.append(f"Asked for by: {asker}")
details.append(
f"Source: {source_ref_markdown(profile, source_ref)}"
)
return details
@dataclass(frozen=True)
class CardSources:
"""What a card reads besides its profile: who reads each collected file,
and each data directory's members by ref."""
consumers: FileConsumers
data_members: Mapping[str, dict[str, dict]] = dataclasses.field(default_factory=dict)
def _card_copy_line(
f: dict, sources: CardSources, held: str, sha1: str, in_repo: bool, details: list[str],
) -> list[str]:
"""Which collection copy answers the entry. `details` are the lines said
before, read for copies already named."""
parts = held.split("/")
if len(parts) >= 3 and parts[0] == "bios" and f.get("archive"):
return [_held_copy_line((held, f["name"]), sources)]
if len(parts) >= 3 and parts[0] == "bios":
# Which copy answers this entry, linked to its collection card
# (provenance, every hash, download): an entry declaring no hash
# said "in repo" without naming the file.
page = card_page(held, sha1)
return [
f"Collection copy: [{code_span(held)}]"
f"(../systems/{urllib.parse.quote(page)}.md#{card_anchor(sha1)})"
]
if in_repo and not any(d.startswith("Collection copies:") for d in details):
copy = held_copy(f)
if copy:
return [_held_copy_line(copy, sources)]
return []
def _card_copy_details(
f: dict, profile: dict, sources: CardSources | None, owner: str, in_repo: bool,
details: list[str],
) -> list[str]:
"""The collection copy that answers the entry and the setups that list
it. `details` are the lines said before."""
if not sources:
return []
consumers = sources.consumers
added = []
if f.get("type") == "directory":
added.extend(_directory_copies(f, consumers))
sha1 = consumers.sha1_of(
*entry_target(f, profile.get("files") or []), owner=owner)
held = consumers.path_of(sha1) if sha1 and in_repo else ""
added.extend(_card_copy_line(f, sources, held, sha1, in_repo, details + added))
plat_links = platform_links(sha1, consumers) if sha1 else []
if plat_links and markup.starlight():
# Grouped by where the setups put the file, as the system card
# says it: twelve setups repeating scph5502.bin took twelve
# names and twelve paths where a few say it. One sentence a
# place: a list or a line break was one more element a place.
places = places_markdown(platform_places(sha1, consumers))
added.append("Platforms: " + ". ".join(places))
elif plat_links:
added.append(f"Platforms: {', '.join(plat_links)}")
return added
def _card_extra_details(f: dict) -> list[str]:
"""The remaining optional fields, one line each."""
details = []
fname = f.get("name", "")
fpath = f.get("path", "")
dest = f.get("dest", f.get("destination", ""))
config_key = f.get("config_key", "")
fpattern = f.get("pattern", "")
region_check = f.get("region_check")
size_note = f.get("size_note", "")
size_range = f.get("size_range", "")
if dest and dest != fname and dest != fpath:
details.append(f"Destination: `{dest}`")
if config_key:
details.append(f"Config key: `{config_key}`")
if fpattern:
details.append(f"Pattern: `{fpattern}`")
if region_check is not None:
details.append(f"Region check: {'yes' if region_check else 'no'}")
if size_note:
details.append(f"Size note: {size_note}")
if size_range:
details.append(f"Size range: {size_range}")
return details
def _card_heading(f: dict, profile: dict, in_repo: bool, anchor: str, layout: CardLayout) -> list[str]:
"""The card's opening: its title with the badges, then the description."""
lines: list[str] = []
fsystem = f.get("system", "")
desc = f.get("description", "")
# melonDS reads DS and DSi files: "required" alone sat on the DSi NAND
# above the DS files, for an emulator most people run for DS games.
for_system = (system_name(str(fsystem))
if fsystem and len(profile.get("systems") or []) > 1 else "")
badges = file_badges(f, in_repo, for_system)
badge_str = " ".join(badges)
lines.append(markup.block_open("div", 'class="rb-file-entry"'))
lines.append("")
marker = f'<span id="{layout.alias}"></span>' if layout.alias else ""
label = entry_label(f, profile)
title = f"{marker}{label}" if layout.badges_apart else f"{marker}{label} {badge_str}"
lines.append(card_title(title, anchor, weight=5))
lines.append("")
if desc:
lines.extend([data_root_words(escape_tags(str(desc)), layout.data_href), ""])
return lines
def render_emulator_file(
f: dict,
profile: dict,
sources: CardSources | None,
files: list,
_file_available: Callable[[dict], bool],
anchor: str = "",
owner: str = "",
layout: CardLayout = _CARD_LAYOUT,
) -> list[str]:
"""Render one row of an emulator profile's file table.
`anchor` is the card's id on the page, so one file can be cited by URL,
and `layout` says where the card lands (CardLayout).
Split out of generate_emulator_page, where it was a 228-line loop body
carrying most of that function's branching: thirty-odd optional fields,
each with its own badge, hash line or note.
"""
in_repo = _file_available(f)
fnote = f.get("note", f.get("notes", ""))
contents = f.get("contents", [])
data_href = layout.data_href
lines = _card_heading(f, profile, in_repo, anchor, layout)
body_start = len(lines)
details = _card_place_details(f, profile, owner, data_href)
details.extend(_card_size_details(f))
details.extend(_card_hash_details(f))
details.extend(_card_reading_details(f, profile))
details.extend(_card_copy_details(f, profile, sources, owner, in_repo, details))
details.extend(_card_extra_details(f))
if markup.starlight():
lines.extend(_emulator_card_folds(f, details, fnote, profile, in_repo, contents,
data_href))
lines.extend(["", "</div>", ""])
return lines
lines.extend(f"- {d}" for d in details)
if fnote:
lines.append(f"- {data_root_words(escape_tags(link_prose_citations(profile, str(fnote))), data_href)}")
if not in_repo and f.get("unsourceable"):
lines.append(f"- {_unsourceable_line(f)}")
lines.extend(_contents_lines(contents))
lines[body_start:] = search_skipped_apart(lines[body_start:])
lines.append("")
lines.append("</div>")
lines.append("")
return lines
def _contents_lines(contents: list) -> list[str]:
"""Every member with the hashes the emulator checks: MAME and FBNeo
verify the members, never the archive, so a member's crc32 is the
value a reader compares a set against."""
if not contents:
return []
lines = [f"- Contents ({len(contents)} members):"]
for c in contents:
if isinstance(c, dict):
parts = []
if c.get("description"):
parts.append(escape_tags(str(c["description"])))
if c.get("size"):
parts.append(size_text(c["size"]))
if c.get("crc32"):
parts.append(f"CRC32 `{c['crc32']}`")
if c.get("sha1"):
parts.append(f"SHA1 `{c['sha1']}`")
extra = f": {', '.join(parts)}" if parts else ""
lines.append(f" - `{c.get('name', '')}`{extra}")
else:
lines.append(f" - {c}")
return lines
# What an emulator card shows at a glance: where the file goes, how big it
# is, which setups list it and which copy answers it. Fingerprints and the
# reading details open on demand.
_ENTRY_KEY_FACTS = ("Path:", "Standalone path:", "Read from:", "Found by:", "System:",
"Size:", "Aliases:", "Collection copy:", "Collection copies:",
"Platforms", "Destination:")
_ENTRY_HASHES = ("SHA1:", "MD5:", "CRC32:", "SHA256:", "Adler32")
# The words the fold of an emulator card names its facts by.
_ENTRY_MORE_WORDS = (
("Priority", "priority"), ("Fast boot", "fast boot"), ("Validation", "validation"),
("On a failed", "failed check"), ("Installed by the platform's own package", "packages"),
("Not in the platform's package", "packages"), ("Never opened", "unread setups"),
("Asked for by", "source"), ("Source", "source"), ("Config key", "config key"),
("Pattern", "pattern"), ("Region check", "region check"), ("Size note", "size note"),
("Size range", "size range"),
)
def _more_words(lines: list[str]) -> list[str]:
"""The words a fold names its facts by, once each, in their order."""
words: list[str] = []
for line in lines:
text = re.sub(r"<[^>]+>", "", line.removeprefix("- "))
word = next((w for prefix, w in _ENTRY_MORE_WORDS if text.startswith(prefix)),
text.split(":", 1)[0].lower())
if word not in words:
words.append(word)
return words
def _short_hash_line(f: dict) -> str:
"""The entry's strongest declared hash, cut to SHORT_HASH characters,
with a copy button for the whole value. "" when it declares none."""
from core.hashing import parse_hash_list
for field, label in (("sha1", "SHA1"), ("md5", "MD5"), ("sha256", "SHA256"), ("crc32", "CRC32")):
values = parse_hash_list(f.get(field) or "")
if values:
value = str(values[0])
more = f" and {len(values) - 1} more" if len(values) > 1 else ""
return (f'{label}: <span class="rb-hash" data-copy="{html.escape(value)}">'
f"{html.escape(value[:SHORT_HASH])}</span>{more}")
return ""
def _emulator_card_folds(
f: dict, details: list[str], fnote: object, profile: dict, in_repo: bool,
contents: list, data_href: str,
) -> list[str]:
"""An emulator card's body under Starlight: the key facts and the notes
open, the hashes and the rest in one closed fold.
Lines the search reads and lines it skips are kept in two runs: mixed,
each change of side was one more list and one more block, five lists
on a card of a dozen lines.
"""
def skipped(line: str) -> bool:
return f"- {line}".startswith(SEARCH_SKIPPED)
key = [d for d in details if d.startswith(_ENTRY_KEY_FACTS)]
hashes = [d for d in details if d.startswith(_ENTRY_HASHES)]
more = [d for d in details if d not in key and d not in hashes]
short = _short_hash_line(f)
shut_open = [d for d in key if skipped(d)]
sized = next((i for i, d in enumerate(shut_open) if d.startswith("Size:")), None)
if short and sized is not None:
# On the size's line: what tells this dump from another, one list
# item fewer on each card.
shut_open[sized] += ", " + short.replace(": ", " ", 1)
elif short:
shut_open.insert(0, short)
# What the search reads first, the notes included, then what it skips:
# one list each.
open_lines = [d for d in key if not skipped(d)]
if fnote:
open_lines.append(data_root_words(
escape_tags(link_prose_citations(profile, str(fnote))), data_href))
if not in_repo and f.get("unsourceable"):
open_lines.append(_unsourceable_line(f))
lines = [f"- {d}" for d in open_lines]
shut_fold = [f"- {d}" for d in hashes + [m for m in more if skipped(m)]]
read_fold = [f"- {m}" for m in more if not skipped(m)]
# The members of an archive, a list of their own under the fold's
# summary, which counts them: a "Contents" item holding them was two
# more elements and 44 bytes on each of 200 MAME cards.
members = [f"* {line.removeprefix(' - ')}" for line in _contents_lines(contents)[1:]]
fold: list[str] = []
if shut_fold or read_fold or members:
words = (["hashes"] if hashes else []) + _more_words(
[line for line in shut_fold + read_fold if not line.startswith(
tuple(f"- {h}" for h in _ENTRY_HASHES))])
words = words if len(words) <= 3 else [*words[:3], "more"]
if members:
words.append(count_text(len(members), "member"))
hidden_part = [*shut_fold, "", *members] if members else shut_fold
body = (hidden_part if not read_fold else [
"", markup.block_open("div", f'class="rb-search-skip" {markup.search_exclude()}'),
"", *hidden_part, "", "</div>", "", *read_fold])
fold = card_fold(listed_words(words), body, skip=False)
# A fold the search skips whole sits in the skipped block before it, with
# no attribute of its own: on a page of 200 MAME cards the attribute, the
# class and a block each were 12 KB.
hidden = bool(fold) and not read_fold
shut = [f"- {d}" for d in shut_open] + (["", *fold] if hidden else [])
if shut:
lines += ["", markup.block_open("div", f'class="rb-search-skip" {markup.search_exclude()}'),
"", *shut, "", "</div>", ""]
if fold and not hidden:
lines += ["", *fold, ""]
return lines
def _held_copy_line(copy: tuple[str, str], sources: CardSources) -> str:
"""Where a copy the database does not index sits: a ZIP member under
bios/, or a file of a data directory the packs fetch."""
consumers = sources.consumers
path, member = copy
parts = path.rstrip("/").split("/")
if member:
archive = code_span(path)
zsha1 = consumers.sha1_at(path)
if zsha1 and len(parts) >= 3 and parts[0] == "bios":
page = urllib.parse.quote(card_page(path, zsha1))
archive = f"[{archive}](../systems/{page}.md#{card_anchor(zsha1)})"
return f"Collection copy: member {code_span(member)} of {archive}"
if parts[0] == "data" and len(parts) >= 2:
ref, inside = parts[1], "/".join(parts[2:])
info = (sources.data_members.get(ref) or {}).get(inside) or {}
shown = code_span(inside)
if info.get("sha1"):
shown = (f'<a href="../../files/?sha1={info["sha1"]}">'
f"<code>{html.escape(inside)}</code></a>")
return f"Collection copy: {shown} in the {code_span(ref)} data directory"
tree = f"{REPO_URL}/tree/main/{urllib.parse.quote(path.rstrip('/'))}"
return f"Collection copy: [{code_span(path)}]({tree})"
def _directory_copies(entry: dict, consumers: FileConsumers) -> list[str]:
"""What the collection holds under a directory entry, and where.
The card said "in repo" and linked none of the files: alephone's
Plugins/ holds 393 of them, each with its own card on a system page.
"""
members = consumers.directory_members(entry)
held = sorted({consumers.path_of(sha1) for sha1 in members.values()} - {""})
if not held:
return []
folder = os.path.commonpath(held) if len(held) > 1 else os.path.dirname(held[0])
parts = folder.split("/")
where = code_span(folder + "/")
if len(parts) >= 3 and parts[0] == "bios":
where = f"[{where}]({console_href(manufacturer_slug(parts[1]), parts[2], '../')})"
tree = f"{REPO_URL}/tree/main/{urllib.parse.quote(folder)}"
return [f"Collection copies: {count_text(len(members), 'file')} under {where} "
f"([browse]({tree}))"]
def any_name_bios(profile: dict) -> dict | None:
"""The one BIOS an emulator needs under any name, or None.
A profile found by content (`found_by: content`, `bios_mode: agnostic`)
for one system, with one required file of no declared region: the
code identifies the image by what it holds, so the entry's name is an
example. AetherSX2's page showed `bios/ps2-0230a-20080220.bin` as the
file to get, while its notes say no name and no hash is matched.
"""
if profile.get("found_by") != "content" and profile.get("bios_mode") != "agnostic":
return None
if len(profile.get("systems") or []) != 1:
return None
required = [f for f in profile.get("files") or []
if isinstance(f, dict) and f.get("required") is True and not comes_with_app(f)]
if len(required) != 1 or required[0].get("region") or required[0].get("category") not in (None, "bios"):
return None
return required[0]
def named_example(name: str) -> bool:
"""A real file name, not a placeholder such as `<bios>.bin`."""
return bool(name) and "<" not in name
def _unsourceable_line(f: dict) -> str:
"""Why the collection holds no copy of a file a profile marks
`unsourceable:`, linked to the list of them. The flag only keeps a
same-named file from being served in its place: the file is still
wanted, and "Not collectable" sent readers away from KANJI2.ROM, which
MAME's fm7740sx.zip carries."""
return (f"Not collected yet: {escape_tags(str(f['unsourceable']))} "
"([files not collected yet](../gaps-not-collected.md))")