refactor: place systems by the section index

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:02:44 +01:00
1 parent e79d2404bc
commit 437690d4a8
3 files changed
+61 -48

No files matched your search

+40 -17
View File
@@ -2,6 +2,8 @@
from __future__ import annotations
from collections.abc import Iterable
from model import siteinputs
from model.collection import SECTION_LABELS, PROGRAM_TOPS, manufacturer_slug
@@ -48,8 +50,6 @@ def set_console_sections(
group them, or is a set of (manufacturer, console) pairs. ``programs``
are the emulator names, which never name a system.
"""
from model.systems import norm_system_id
pairs = (
{(mfr, console) for mfr, consoles in sections.items() for console in consoles}
if isinstance(sections, dict) else set(sections)
@@ -77,11 +77,7 @@ def set_console_sections(
used = {toc_slug(mfr)}
for console in sorted(consoles):
console_anchors[(manufacturer_slug(mfr), console)] = unique_anchor(toc_slug(console), used)
for mfr, console in pairs:
spelled = console.lower().replace(" ", "-")
slug = mfr.lower().replace(" ", "-")
for key in {norm_system_id(f"{slug}-{spelled}"), norm_system_id(spelled)}:
_console_sections.setdefault(key, set()).add((mfr, console))
_console_sections.update(console_index(pairs))
known_program_names.clear()
known_program_names.update(name.casefold() for name in programs)
_sections_installed[0] = True
@@ -107,25 +103,52 @@ def ensure_console_sections() -> None:
set_console_sections(group_by_manufacturer(db), program_names(profiles))
def own_section(sys_id: str) -> tuple[str, str] | None:
"""The console section that is this id's own, or None.
def console_index(pairs: Iterable[tuple[str, str]]) -> dict[str, set[tuple[str, str]]]:
"""Normalized system name -> the console sections that spell it.
Of two sections spelled alike (Atari and Camputers both file a Lynx),
the one under the maker the id names is kept. Two left is no answer,
and neither is a section that files a program's copies.
nintendo-nes, nes and the NES folder normalize alike, as do
sega-mastersystem and Sega/Master System.
"""
from model.systems import norm_system_id
ensure_console_sections()
places = _console_sections.get(norm_system_id(sys_id), set())
index: dict[str, set[tuple[str, str]]] = {}
for mfr, console in pairs:
spelled = console.lower().replace(" ", "-")
for key in {norm_system_id(f"{manufacturer_slug(mfr)}-{spelled}"), norm_system_id(spelled)}:
index.setdefault(key, set()).add((mfr, console))
return index
def spelled_section(
sys_id: str, index: dict[str, set[tuple[str, str]]],
) -> tuple[str, str] | None:
"""The one section of ``index`` the id spells, or None.
Of two sections spelled alike (Atari and Camputers both file a Lynx),
the one under the maker the id names is kept. Two left is no answer.
"""
from model.systems import norm_system_id
places = index.get(norm_system_id(sys_id), set())
if len(places) > 1:
places = {
(mfr, console) for mfr, console in places
if sys_id.lower().startswith(mfr.lower().replace(" ", "-") + "-")
if sys_id.lower().startswith(manufacturer_slug(mfr) + "-")
}
if len(places) != 1:
return next(iter(places)) if len(places) == 1 else None
def own_section(sys_id: str) -> tuple[str, str] | None:
"""The console section that is this id's own, or None.
The section the id spells (spelled_section), unless it files a
program's copies.
"""
ensure_console_sections()
section = spelled_section(sys_id, _console_sections)
if section is None:
return None
mfr, console = next(iter(places))
mfr, console = section
# A folder under a program's top, named like its own page (Dinothawr) or
# like an emulator holds a program's files, not a system's.
if (
+10 -31
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
from dataclasses import dataclass
from model.collection import copy_places, manufacturer_slug
from model.systemnames import console_index, spelled_section
from resolve.localfile import resolution_is_hash_exact, resolve_local_file
from resolve.ownership import BUCKET_FOLDERS, names_manufacturer
@@ -67,38 +68,12 @@ def _system_folder(
return None
def _folders_by_name(manufacturers: dict) -> dict[str, set[tuple[str, str]]]:
"""Normalized system name -> the console folders that spell it.
nintendo-nes, nes and the NES folder normalize alike, as do
sega-mastersystem and Sega/Master System.
"""
from model.systems import norm_system_id
index: dict[str, set[tuple[str, str]]] = {}
for mfr, consoles in manufacturers.items():
for console in consoles:
spelled = console.lower().replace(" ", "-")
for key in {
norm_system_id(f"{manufacturer_slug(mfr)}-{spelled}"), norm_system_id(spelled)
}:
index.setdefault(key, set()).add((mfr, console))
return index
def _folder_named(sys_id: str, by_name: dict[str, set[tuple[str, str]]]) -> tuple[str, str] | None:
from model.systems import norm_system_id
places = by_name.get(norm_system_id(sys_id), set())
return next(iter(places)) if len(places) == 1 else None
def _console_norm_index(manufacturers: dict) -> dict[str, tuple[str, str]]:
"""Normalized console name index for fuzzy matching."""
console_norm: dict[str, tuple[str, str]] = {}
for mfr, consoles in manufacturers.items():
slug = mfr.lower().replace(" ", "-")
mfr_norm = mfr.lower().replace(" ", "-")
slug = manufacturer_slug(mfr)
mfr_norm = slug
for console in consoles:
norm = console.lower().replace(" ", "-")
entry = (slug, console)
@@ -147,13 +122,13 @@ class _FolderIndex:
Strategy 2: the folder holding the files the system declares,
found by content, and most votes win. A bare name stands in only
where the collection files it in a single console."""
place = _folder_named(sys_id, self.folders_by_name) or _system_folder(
place = spelled_section(sys_id, self.folders_by_name) or _system_folder(
files, self.db, self.name_consoles, sys_id, self.copies
)
if place is None:
return None
mfr, console = place
return (mfr.lower().replace(" ", "-"), console)
return (manufacturer_slug(mfr), console)
def _profile_system_pages(
@@ -202,7 +177,11 @@ def system_pages_from_data(
console_norm = _console_norm_index(manufacturers)
# Map system_id -> (manufacturer, console) via platform file entries
index = _FolderIndex(db, name_consoles, _folders_by_name(manufacturers), _copies_by_path(db))
index = _FolderIndex(
db, name_consoles,
console_index((mfr, console) for mfr, consoles in manufacturers.items() for console in consoles),
_copies_by_path(db),
)
pages: dict[str, tuple[str, str]] = {}
for cov in coverages.values():
config = cov["config"]
+11
View File
@@ -115,6 +115,17 @@ class OtherSpellingsOfOneSystem(unittest.TestCase):
manufacturers, {}, {"files": {}, "indexes": {}}, profiles)
self.assertEqual(pages["commodore-pet"], ("commodore", "PET"))
def test_two_folders_spelled_alike_go_to_the_maker_the_id_names(self):
"""Atari and Camputers both file a Lynx: the system names agreed on
Atari's while the placement found no folder by its name."""
from model.systemnames import console_index
index = places._FolderIndex(
{"files": {}, "indexes": {}}, {},
console_index({("Atari", "Lynx"), ("Camputers", "Lynx")}), {})
self.assertEqual(index.place("atari-lynx", []), ("atari", "Lynx"))
self.assertIsNone(index.place("lynx", []))
class ACopyIsOnItsOwnFoldersPage(unittest.TestCase):
"""The database keeps one path per content: the Channel F BIOS was on the