mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-27 11:58:53 -06:00
refactor: give each installer module one role
The split had pushed each helper down into the lowest module using it. Names now sit with the concern they belong to (68 modules, 45 before), each with a one-line docstring saying it, and the import graph stays acyclic. Statement source and comments moved unchanged: every top-level statement of install.py keeps an identical ast.dump.
This commit is contained in:
1 parent
9b0c7a390c
commit
6b597f85a0
78 files changed
+30571
-30127
No files matched your search
+14
-229
@@ -1,21 +1,22 @@
|
||||
"""Narrowing a file list to a board, cores, regions, systems and one file per slot."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable, Collection, Iterable
|
||||
from collections.abc import Callable, Collection
|
||||
|
||||
from installer.constants import (
|
||||
AREA_TERRITORIES,
|
||||
FILE_GROUP,
|
||||
OTHER_SYSTEMS,
|
||||
REGIONS,
|
||||
REGION_ALIASES,
|
||||
WORLD_REGION,
|
||||
_REGION_PARENT,
|
||||
)
|
||||
from installer.cores import SYSTEM_KEY_FILES, SYSTEM_SHORT_NAMES, did_you_mean, entry_systems
|
||||
from installer.embedded import _name_key
|
||||
from installer.manifest import _COPY_FIELDS
|
||||
from installer.manifest import FILE_GROUP, OTHER_SYSTEMS, _COPY_FIELDS
|
||||
from installer.paths import _entry_key
|
||||
from installer.platforms import entry_systems
|
||||
from installer.regions import WORLD_REGION, _region_rank
|
||||
|
||||
|
||||
def board_target(board: str, targets: dict[str, dict]) -> str | None:
|
||||
"""The target a board name stands for: the name itself, else the one
|
||||
target whose images carry it. A card names its image (Batocera writes
|
||||
anbernic-rg35xx-plus for the h700 target, x86-64-v3 for zen3)."""
|
||||
if board in targets:
|
||||
return board
|
||||
owners = [name for name, info in targets.items() if board in info.get("images", ())]
|
||||
return owners[0] if len(owners) == 1 else None
|
||||
|
||||
|
||||
def _for_cores(entry: dict, chosen: set[str]) -> dict | None:
|
||||
@@ -218,40 +219,6 @@ def narrow_to_cores(
|
||||
return kept_files, kept(omitted) + displaced
|
||||
|
||||
|
||||
def canonical_region(raw: str) -> str:
|
||||
"""One region name or alias, spelled the way manifests spell it."""
|
||||
key = raw.strip().lower()
|
||||
key = REGION_ALIASES.get(key, key)
|
||||
if key not in REGIONS:
|
||||
import difflib
|
||||
|
||||
close = difflib.get_close_matches(key, [*REGION_ALIASES, *REGIONS], n=3, cutoff=0.5)
|
||||
hint = f" Close matches: {', '.join(close)}." if close else ""
|
||||
raise ValueError(
|
||||
f"unknown region: {raw.strip()}.{hint} "
|
||||
f"Short forms: {', '.join(REGION_ALIASES)}. Regions: "
|
||||
f"{', '.join(sorted(REGIONS))}"
|
||||
)
|
||||
return key
|
||||
|
||||
|
||||
def _region_comparable(a: str, b: str) -> bool:
|
||||
return (
|
||||
a == b
|
||||
or _REGION_PARENT.get(a) == b
|
||||
or _REGION_PARENT.get(b) == a
|
||||
or a in AREA_TERRITORIES.get(b, ())
|
||||
or b in AREA_TERRITORIES.get(a, ())
|
||||
)
|
||||
|
||||
|
||||
def _region_rank(file_regions: set[str], requested: list[str]) -> int:
|
||||
for index, wanted in enumerate(requested):
|
||||
if any(_region_comparable(wanted, region) for region in file_regions):
|
||||
return index
|
||||
return len(requested)
|
||||
|
||||
|
||||
def region_drops(
|
||||
entries: list[dict],
|
||||
requested: list[str],
|
||||
@@ -559,185 +526,3 @@ def find_by_hash(files: list[dict], value: str) -> tuple[str, list[dict], bool]:
|
||||
seen.add((_entry_key(found), found.get("sha1", "")))
|
||||
matches.append(found)
|
||||
return kind, matches, recorded
|
||||
|
||||
|
||||
def recorded_axes(files: list[dict], omitted: Iterable[dict] = ()) -> frozenset:
|
||||
"""What a file list lets --system, --core and --region narrow by: its
|
||||
systems, its cores, its regions. A list from an older release records
|
||||
none. The MinUI family's records cores and regions and no system: all
|
||||
three were refused on it, by a remedy that fetched the same list."""
|
||||
entries = [*files, *omitted]
|
||||
systems = any("systems" in entry for entry in entries)
|
||||
if not systems and not any("regions" in entry for entry in entries):
|
||||
# Systems and regions came in one release: a list with neither is
|
||||
# older, its cores read by nothing that narrows.
|
||||
return frozenset()
|
||||
# A current list records the regions of the files a region selects, and
|
||||
# none when it has no such file: --region then keeps every file, as the
|
||||
# pack built with it does.
|
||||
axes = {"regions", *(["systems"] if systems else [])}
|
||||
if any(copy.get("cores") for entry in files
|
||||
for copy in [entry, *entry.get("alternatives", []), *entry.get("slot_choices", [])]):
|
||||
axes.add("cores")
|
||||
return frozenset(axes)
|
||||
|
||||
|
||||
def records_selection(files: list[dict]) -> bool:
|
||||
"""Whether a manifest records anything narrowing reads. Older ones do not."""
|
||||
return bool(recorded_axes(files))
|
||||
|
||||
|
||||
def available_choices(files: list[dict], omitted: list[dict]) -> dict[str, list[str]]:
|
||||
"""The systems, cores and regions a manifest lets one narrow to."""
|
||||
systems = {s for e in files + omitted for s in entry_systems(e)}
|
||||
ordered = sorted(systems)
|
||||
if any(not entry_systems(f) for f in files):
|
||||
ordered.append(OTHER_SYSTEMS)
|
||||
cores = {
|
||||
c for f in files
|
||||
for copy in [f, *f.get("alternatives", []), *f.get("slot_choices", [])]
|
||||
for c in (copy.get("cores") or [])
|
||||
}
|
||||
regions = {
|
||||
r for e in files + omitted for r in (e.get("regions") or [])
|
||||
if r != WORLD_REGION
|
||||
}
|
||||
return {
|
||||
"systems": ordered,
|
||||
"cores": sorted(cores),
|
||||
"regions": sorted(regions),
|
||||
}
|
||||
|
||||
|
||||
def choosable_cores(files: list[dict], omitted: list[dict],
|
||||
systems: Collection[str] = ()) -> list[str]:
|
||||
"""The cores a choice is made among, for the files a run keeps once
|
||||
the save entries it cannot place and the game data --no-game-data cuts
|
||||
are left out, of the systems chosen (every system for none). Cores on
|
||||
the screen and c in line mode list these and count "N of M" out of
|
||||
them: the screen listed the whole file list's 189 where the run
|
||||
accepted 181."""
|
||||
if systems:
|
||||
files, omitted = narrow(files, omitted, list(systems), [], [])
|
||||
return available_choices(files, omitted)["cores"]
|
||||
|
||||
|
||||
def core_files(files: list[dict], core: str) -> list[dict]:
|
||||
"""The files one core adds to the platform's own: what line mode's
|
||||
Cores question and the screen's Cores rows size."""
|
||||
return _filter_by_target([entry for entry in files if entry.get("cores") is not None],
|
||||
[core])
|
||||
|
||||
|
||||
def resolve_choices(
|
||||
values: list[str], known: list[str], label: str,
|
||||
aliases: dict[str, str] | None = None,
|
||||
holders: dict[str, list[str]] | None = None,
|
||||
) -> list[str]:
|
||||
"""Names given on the command line, matched without regard to case.
|
||||
|
||||
Each value is one name or a comma-separated list. A whole value that is
|
||||
itself a name wins over splitting it: Recalbox names one system
|
||||
`msx1,msx2,msxturbor`. An unknown name is refused, never ignored, since
|
||||
carrying on would install something other than what was asked.
|
||||
aliases maps another name of a known one to it, as Batocera's `psx`
|
||||
stands for sony-playstation. A known name always wins over an alias.
|
||||
holders maps a system the list files under other ids to those ids
|
||||
(key_file_holders): an unknown name for it is answered with them.
|
||||
"""
|
||||
aliases = {alias: name for alias, name in (aliases or {}).items() if name in known}
|
||||
by_lower = {name.lower(): name for name in known}
|
||||
for alias, name in aliases.items():
|
||||
by_lower.setdefault(alias.lower(), name)
|
||||
# Spelled as the site writes it (Sony PlayStation), a name is still one
|
||||
# name when no other one reads the same once case and spacing go.
|
||||
by_key: dict[str, str | None] = {}
|
||||
for name in known:
|
||||
key = _name_key(name)
|
||||
by_key[key] = None if key in by_key else name
|
||||
for alias, name in aliases.items():
|
||||
by_key.setdefault(_name_key(alias), name)
|
||||
chosen: list[str] = []
|
||||
unknown: list[str] = []
|
||||
for value in values:
|
||||
value = value.strip()
|
||||
tokens = [value] if value.lower() in by_lower else value.split(",")
|
||||
for token in (t.strip() for t in tokens):
|
||||
if not token:
|
||||
continue
|
||||
picked = by_lower.get(token.lower()) or by_key.get(_name_key(token))
|
||||
if picked is None:
|
||||
unknown.append(token)
|
||||
elif picked not in chosen:
|
||||
chosen.append(picked)
|
||||
if unknown:
|
||||
if not known:
|
||||
raise ValueError(f"unknown {label}: {', '.join(unknown)}. This list records none.")
|
||||
listing = {"system": "--list-systems", "emulator": "--list-emulators"}.get(
|
||||
label, "--list-cores")
|
||||
hints, inside_from = aliases, 4
|
||||
held = holders or {}
|
||||
stands_for: dict[str, str] = {}
|
||||
notes = ""
|
||||
if label == "system":
|
||||
# gba is the end of nintendo-gba: three letters are enough here.
|
||||
hints = {**{short: name for short, name in SYSTEM_SHORT_NAMES.items()
|
||||
if name in known}, **aliases}
|
||||
inside_from = 3
|
||||
stands_for = {_name_key(system): system for system in held}
|
||||
for short, name in SYSTEM_SHORT_NAMES.items():
|
||||
if name in held:
|
||||
stands_for.setdefault(_name_key(short), name)
|
||||
named = dict.fromkeys(
|
||||
stands_for[_name_key(token)] for token in unknown
|
||||
if _name_key(token) in stands_for)
|
||||
notes = "".join(
|
||||
f"This list files {SYSTEM_KEY_FILES[system]} under "
|
||||
f"{', '.join(held[system])}. " for system in named)
|
||||
meant = {key: held[system] for key, system in stands_for.items()}
|
||||
raise ValueError(
|
||||
f"unknown {label}: {', '.join(unknown)}. {notes}"
|
||||
f"{did_you_mean(unknown, known, listing, hints, inside_from, meant)}"
|
||||
)
|
||||
if values and not chosen:
|
||||
raise ValueError(f"--{label} needs at least one name")
|
||||
return chosen
|
||||
|
||||
|
||||
def resolve_regions(values: list[str]) -> list[str]:
|
||||
"""A region priority list, best first, from names or aliases."""
|
||||
chosen: list[str] = []
|
||||
for value in values:
|
||||
for token in value.split(","):
|
||||
if token.strip():
|
||||
region = canonical_region(token)
|
||||
if region not in chosen:
|
||||
chosen.append(region)
|
||||
if values and not chosen:
|
||||
raise ValueError("--region needs at least one region")
|
||||
return chosen
|
||||
|
||||
|
||||
def parse_selection(text: str, count: int, ordered: bool = False) -> list[int]:
|
||||
"""Numbers typed at a prompt: `3`, `1,4`, `2-5`, or `all`, 1-based.
|
||||
|
||||
Returned sorted, or in the typed order when the order is a priority.
|
||||
"""
|
||||
if text.strip().lower() in ("all", "*"):
|
||||
return list(range(1, count + 1))
|
||||
chosen: list[int] = []
|
||||
for token in (t.strip() for t in text.split(",")):
|
||||
if not token:
|
||||
continue
|
||||
low, sep, high = token.partition("-")
|
||||
try:
|
||||
start = int(low)
|
||||
end = int(high) if sep else start
|
||||
except ValueError:
|
||||
raise ValueError(f"not a number or range: {token}") from None
|
||||
if start > end or start < 1 or end > count:
|
||||
raise ValueError(f"out of range: {token} (1-{count})")
|
||||
for number in range(start, end + 1):
|
||||
if number not in chosen:
|
||||
chosen.append(number)
|
||||
return chosen if ordered else sorted(chosen)
|
||||
Reference in new issue
Block a user