refactor: move es_systems readers out of batocera

es_systems.yml readers move to scraper/es_systems.py, which the board
target scraper imports at load time instead of inside a function to dodge
the cycle. The _default.remove_cores list is target override data: it is
model.targets.not_cores, no longer a private name of batocera_scraper.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 16:43:46 +01:00
1 parent 14cc76132f
commit e6295aba88
12 files changed
+174 -167

No files matched your search

+11 -1
View File
@@ -7,7 +7,7 @@ import re
import sys
from pathlib import Path
from core.paths import PLATFORMS_DIR
from core.paths import PLATFORMS_DIR, REPO_ROOT
from core.cli import SourceUnreadable, suggest_names
from model.sources import source_yaml_file
@@ -31,6 +31,16 @@ def target_overrides(platform: str, targets_dir: str | os.PathLike[str]) -> dict
return platform_overrides(read_target_overrides(targets_dir), platform)
def not_cores(
platform: str, targets_dir: str | os.PathLike[str] = REPO_ROOT / PLATFORMS_DIR / "targets",
) -> set[str]:
"""Names a platform's system list files under an emulator that are not
emulators (machine selectors, video plugins, launchers): its
`_default.remove_cores` overrides."""
default = target_overrides(platform, targets_dir).get("_default") or {}
return {str(name) for name in default.get("remove_cores") or []}
def target_aliases(overrides: dict, target: str) -> list[str]:
"""The other names a target answers to: the pack builder and the
installer read this one list, or the installer refuses a word the pack
+4 -4
View File
@@ -32,6 +32,7 @@ from __future__ import annotations
import re
from model.targets import not_cores
from .amberelec_launchers import extract_launchers
from .base_scraper import fetch_text, github_json
from .batocera_scraper import (
@@ -39,7 +40,6 @@ from .batocera_scraper import (
)
from .batocera_scraper import (
CheckBiosScraper,
_not_cores,
)
from .es_emulators import attach_system_cores, es_cores, system_cores, xml_systems
@@ -114,9 +114,9 @@ class Scraper(CheckBiosScraper):
# es_systems.cfg gives each <system> its <emulators> in menu order
# (atarist, lines 267-288: retroarch's hatari, then HATARISA).
es_systems = xml_systems(fetch_text(ES_SYSTEMS_TEMPLATE.format(ref=self.commit)))
not_cores = _not_cores(PLATFORM_NAME)
cores, standalone, dual = es_cores(es_systems, RUNNERS, BUILDS_OF, not_cores)
attach_system_cores(systems, system_cores(es_systems, RUNNERS, not_cores))
dropped = not_cores(PLATFORM_NAME)
cores, standalone, dual = es_cores(es_systems, RUNNERS, BUILDS_OF, dropped)
attach_system_cores(systems, system_cores(es_systems, RUNNERS, dropped))
return {
"platform": "AmberELEC",
"version": self.tag,
+6 -137
View File
@@ -11,12 +11,12 @@ from __future__ import annotations
import ast
import re
import sys
from collections.abc import Callable, Iterator
from collections.abc import Callable
from model.database import load_database
from core.paths import DB_FILE, PLATFORMS_DIR, REPO_ROOT
from model.targets import load_target_config, target_overrides
from core.paths import DB_FILE, REPO_ROOT
from model.targets import load_target_config, not_cores
from core.hashing import cut_md5_owner
from model.safeparse import yaml_load_upstream
@@ -33,6 +33,7 @@ from .base_scraper import (
)
from .batocera_configgen import Configgen, configs_root
from .batocera_launchers import extract_launchers
from .es_systems import es_systems_emulators, es_systems_libretro, es_systems_libretro_systems
from .targets.batocera_targets_scraper import BuildSelection
from .targets.batocera_targets_scraper import Scraper as BoardTargets
@@ -83,138 +84,6 @@ ES_SYSTEMS_URL = (
"/package/batocera/emulationstation/batocera-es-system/es_systems.yml"
)
# Names es_systems.yml files under an emulator that are not emulators:
# machine selectors, video plugins, launchers. Read from the overrides the
# target scraper applies, so one list says what is not a core.
TARGETS_DIR = REPO_ROOT / PLATFORMS_DIR / "targets"
def _not_cores(platform: str = "batocera") -> set[str]:
default = target_overrides(platform, TARGETS_DIR).get("_default") or {}
return {str(name) for name in default.get("remove_cores") or []}
def _es_systems_data(text: str) -> dict:
data = yaml_load_upstream(text)
if not isinstance(data, dict):
raise ValueError("es_systems.yml is not a mapping")
return data
def _system_entries(spec: object) -> Iterator[tuple[str, str | None, dict | None]]:
"""(emulator, core, core spec) for each core one es_systems.yml system offers.
A system lists each emulator a user may pick and the cores under it.
Keys whose value is not a mapping (archs_include) describe the entry,
not a core. An emulator that lists no core is yielded once with None.
"""
emulators = spec.get("emulators") if isinstance(spec, dict) else None
if not isinstance(emulators, dict):
return
for emulator, entries in emulators.items():
if not isinstance(entries, dict):
continue
offered = [(str(core), item) for core, item in entries.items() if isinstance(item, dict)]
if not offered:
yield str(emulator), None, None
for core, item in offered:
yield str(emulator), core, item
def es_systems_entries(text: str) -> Iterator[tuple[str, str, str | None, dict | None]]:
"""(system, emulator, core, core spec) for every core es_systems.yml offers.
`emulator` is `libretro` for a libretro core. Any other emulator is its
own core, and what it lists under it are machines or builds. Raises
ValueError when the file is not a mapping.
"""
for system, spec in _es_systems_data(text).items():
for emulator, core, item in _system_entries(spec):
yield str(system), emulator, core, item
def es_systems_emulators(text: str) -> tuple[set[str], set[str]]:
"""Every core es_systems.yml offers, and those that run standalone.
configgen-defaults.yml only names the one picked by default, so
reading it alone left DOSBox Staging, melonDS DS and nine other built
cores outside the pack. The non-cores a standalone emulator lists are
named in the overrides.
"""
cores: set[str] = set()
standalone: set[str] = set()
for _system, emulator, core, _spec in es_systems_entries(text):
names = {core} if core else set()
if emulator != "libretro":
names.add(emulator)
standalone |= names
cores |= names
return cores, standalone
def es_systems_libretro(text: str) -> set[str]:
"""The cores es_systems.yml offers under `libretro:` for some system."""
return {
core for _system, emulator, core, _spec in es_systems_entries(text)
if emulator == "libretro" and core
}
def es_systems_libretro_systems(text: str, dropped: set[str]) -> dict[str, list[str]]:
"""libretro core -> the systems es_systems.yml offers it under `libretro:`.
configgen hands a libretro core /userdata/saves/<system> at each launch
(libretroConfig.py: savefile_directory = SAVES / system.name), so a file
a core reads below its save directory sits once in each of these
system folders.
"""
found: dict[str, set[str]] = {}
for system, emulator, core, _spec in es_systems_entries(text):
if emulator == "libretro" and core and core not in dropped:
found.setdefault(core, set()).add(system)
return {core: sorted(systems) for core, systems in sorted(found.items())}
def es_systems_by_system(
es_systems_text: str, defaults_text: str, dropped: set[str],
built: Callable[[str], bool] | None = None,
) -> dict[str, list[str]]:
"""system -> the cores es_systems.yml offers it, in the file's order.
Read as configgen_cores reads the whole list: a core under `libretro:`
is a libretro core, any other emulator is its own core and so is each
build it lists. The core configgen-defaults.yml starts by default comes
last when es_systems.yml does not offer it.
"""
data = _es_systems_data(es_systems_text)
defaults = yaml_load_upstream(defaults_text)
if not isinstance(defaults, dict):
raise ValueError("configgen-defaults.yml is not a mapping")
result: dict[str, list[str]] = {}
for system, spec in data.items():
names: list[str] = []
for offered, core, _item in _system_entries(spec):
if offered != "libretro":
names.append(offered)
if core:
names.append(core)
default = defaults.get(system)
if isinstance(default, dict):
emulator, core = default.get("emulator"), default.get("core")
if (built is not None and emulator and emulator != "libretro"
and str(emulator) not in names and not built(str(emulator))):
# A default the build lacks (configgen_cores).
emulator = core = None
if emulator and emulator != "libretro":
names.append(str(emulator))
if core:
names.append(str(core))
names = [n for n in dict.fromkeys(names) if n not in dropped]
if names:
result[str(system)] = names
return result
SYSTEM_SLUG_MAP = {
"advision": "entex-advision",
"amiga500": "commodore-amiga",
@@ -651,7 +520,7 @@ class Scraper(CheckBiosScraper):
return configgen_cores(
fetch_text(CONFIGGEN_DEFAULTS_URL.format(tag=self.tag)),
fetch_text(ES_SYSTEMS_URL.format(tag=self.tag)),
_not_cores(),
not_cores("batocera"),
built,
)
@@ -676,7 +545,7 @@ class Scraper(CheckBiosScraper):
cores, standalone, dual = self._fetch_cores(built)
installable = installable_cores(selection, cores, standalone, dual)
libretro_systems = es_systems_libretro_systems(
fetch_text(ES_SYSTEMS_URL.format(tag=self.tag)), _not_cores()
fetch_text(ES_SYSTEMS_URL.format(tag=self.tag)), not_cores("batocera")
)
result = {
"platform": "Batocera",
+4 -4
View File
@@ -27,13 +27,13 @@ Verification (batocera-systems at the release tag):
from __future__ import annotations
from model.sourcerevision import required_revision
from model.targets import not_cores
from .base_scraper import fetch_github_latest_version, fetch_text
from .batocera_scraper import (
SYSTEM_SLUG_MAP as BATOCERA_SLUGS,
)
from .batocera_scraper import (
CheckBiosScraper,
_not_cores,
)
from .emuelec_launchers import extract_launchers
from .es_emulators import (
@@ -112,11 +112,11 @@ class Scraper(CheckBiosScraper):
json_systems(fetch_text(ES_SYSTEMS_TEMPLATE.format(ref=self.tag))),
port_engines(fetch_text(PORTS_TEMPLATE.format(ref=self.tag))),
)
not_cores = _not_cores(PLATFORM_NAME)
cores, standalone, dual = es_cores(es_systems, RUNNERS, BUILDS_OF, not_cores)
dropped = not_cores(PLATFORM_NAME)
cores, standalone, dual = es_cores(es_systems, RUNNERS, BUILDS_OF, dropped)
# Each system keeps the list es_systems.json gives it: it says whose
# files a platform entry asks for. What runs is what an image carries.
attach_system_cores(systems, system_cores(es_systems, RUNNERS, not_cores))
attach_system_cores(systems, system_cores(es_systems, RUNNERS, dropped))
built = self._built()
unbuilt = {n for n in cores if n not in built}
cores, standalone, dual = ([n for n in names if n in built] for names in (cores, standalone, dual))
+129
View File
@@ -0,0 +1,129 @@
"""The systems and cores Batocera's es_systems.yml offers."""
from __future__ import annotations
from collections.abc import Callable, Iterator
from model.safeparse import yaml_load_upstream
def es_systems_data(text: str) -> dict:
"""es_systems.yml as a mapping, ValueError when it is not one."""
data = yaml_load_upstream(text)
if not isinstance(data, dict):
raise ValueError("es_systems.yml is not a mapping")
return data
def system_entries(spec: object) -> Iterator[tuple[str, str | None, dict | None]]:
"""(emulator, core, core spec) for each core one es_systems.yml system offers.
A system lists each emulator a user may pick and the cores under it.
Keys whose value is not a mapping (archs_include) describe the entry,
not a core. An emulator that lists no core is yielded once with None.
"""
emulators = spec.get("emulators") if isinstance(spec, dict) else None
if not isinstance(emulators, dict):
return
for emulator, entries in emulators.items():
if not isinstance(entries, dict):
continue
offered = [(str(core), item) for core, item in entries.items() if isinstance(item, dict)]
if not offered:
yield str(emulator), None, None
for core, item in offered:
yield str(emulator), core, item
def es_systems_entries(text: str) -> Iterator[tuple[str, str, str | None, dict | None]]:
"""(system, emulator, core, core spec) for every core es_systems.yml offers.
`emulator` is `libretro` for a libretro core. Any other emulator is its
own core, and what it lists under it are machines or builds. Raises
ValueError when the file is not a mapping.
"""
for system, spec in es_systems_data(text).items():
for emulator, core, item in system_entries(spec):
yield str(system), emulator, core, item
def es_systems_emulators(text: str) -> tuple[set[str], set[str]]:
"""Every core es_systems.yml offers, and those that run standalone.
configgen-defaults.yml only names the one picked by default, so
reading it alone left DOSBox Staging, melonDS DS and nine other built
cores outside the pack. The non-cores a standalone emulator lists are
named in the overrides.
"""
cores: set[str] = set()
standalone: set[str] = set()
for _system, emulator, core, _spec in es_systems_entries(text):
names = {core} if core else set()
if emulator != "libretro":
names.add(emulator)
standalone |= names
cores |= names
return cores, standalone
def es_systems_libretro(text: str) -> set[str]:
"""The cores es_systems.yml offers under `libretro:` for some system."""
return {
core for _system, emulator, core, _spec in es_systems_entries(text)
if emulator == "libretro" and core
}
def es_systems_libretro_systems(text: str, dropped: set[str]) -> dict[str, list[str]]:
"""libretro core -> the systems es_systems.yml offers it under `libretro:`.
configgen hands a libretro core /userdata/saves/<system> at each launch
(libretroConfig.py: savefile_directory = SAVES / system.name), so a file
a core reads below its save directory sits once in each of these
system folders.
"""
found: dict[str, set[str]] = {}
for system, emulator, core, _spec in es_systems_entries(text):
if emulator == "libretro" and core and core not in dropped:
found.setdefault(core, set()).add(system)
return {core: sorted(systems) for core, systems in sorted(found.items())}
def es_systems_by_system(
es_systems_text: str, defaults_text: str, dropped: set[str],
built: Callable[[str], bool] | None = None,
) -> dict[str, list[str]]:
"""system -> the cores es_systems.yml offers it, in the file's order.
Read as configgen_cores reads the whole list: a core under `libretro:`
is a libretro core, any other emulator is its own core and so is each
build it lists. The core configgen-defaults.yml starts by default comes
last when es_systems.yml does not offer it.
"""
data = es_systems_data(es_systems_text)
defaults = yaml_load_upstream(defaults_text)
if not isinstance(defaults, dict):
raise ValueError("configgen-defaults.yml is not a mapping")
result: dict[str, list[str]] = {}
for system, spec in data.items():
names: list[str] = []
for offered, core, _item in system_entries(spec):
if offered != "libretro":
names.append(offered)
if core:
names.append(core)
default = defaults.get(system)
if isinstance(default, dict):
emulator, core = default.get("emulator"), default.get("core")
if (built is not None and emulator and emulator != "libretro"
and str(emulator) not in names and not built(str(emulator))):
# A default the build lacks (configgen_cores).
emulator = core = None
if emulator and emulator != "libretro":
names.append(str(emulator))
if core:
names.append(str(core))
names = [n for n in dict.fromkeys(names) if n not in dropped]
if names:
result[str(system)] = names
return result
+4 -4
View File
@@ -27,6 +27,8 @@ where it is older or its own.
from __future__ import annotations
from model.targets import not_cores
from .base_scraper import (
fetch_github_latest_version,
fetch_text,
@@ -38,15 +40,13 @@ from .batocera_scraper import (
)
from .batocera_scraper import (
CheckBiosScraper,
_not_cores,
boards_build,
configgen_cores,
es_systems_by_system,
es_systems_libretro_systems,
installable_cores,
records_run,
)
from .es_emulators import attach_system_cores
from .es_systems import es_systems_by_system, es_systems_libretro_systems
from .targets.knulli_targets_scraper import Scraper as BoardTargets
from .batocera_configgen import configs_root
from .knulli_launchers import BATOCERA_SUBMODULE, extract_launchers
@@ -92,7 +92,7 @@ class Scraper(CheckBiosScraper):
# KNULLI's own.
defaults = fetch_text(CONFIGGEN_DEFAULTS_TEMPLATE.format(ref=self.tag))
es_systems = fetch_text(ES_SYSTEMS_TEMPLATE.format(ref=self.tag))
dropped = _not_cores("batocera") | _not_cores(PLATFORM_NAME)
dropped = not_cores("batocera") | not_cores(PLATFORM_NAME)
# The batocera.linux commit KNULLI's batocera submodule pins at the tag.
revision = submodule_commit(REPO, BATOCERA_SUBMODULE, self.tag)
launchers = extract_launchers(
@@ -24,7 +24,7 @@ from datetime import datetime, timezone
from . import BaseTargetScraper, target_cli
from ..amberelec_scraper import BUILDS_OF, ES_SYSTEMS_TEMPLATE, RUNNERS
from ..base_scraper import fetch_text
from ..batocera_scraper import _not_cores
from model.targets import not_cores
from ..es_emulators import es_cores, xml_systems
PLATFORM_NAME = "amberelec"
@@ -91,7 +91,7 @@ def device_cores(es_systems: str, extra: list[str], keeps_extra: bool) -> list[s
"""Every core and standalone emulator one device's es_systems.cfg offers."""
text = es_systems if keeps_extra else without_cores(es_systems, extra)
cores, _standalone, _dual = es_cores(
xml_systems(text), RUNNERS, BUILDS_OF, _not_cores(PLATFORM_NAME)
xml_systems(text), RUNNERS, BUILDS_OF, not_cores(PLATFORM_NAME)
)
return sorted(set(cores))
@@ -23,6 +23,7 @@ from datetime import datetime, timezone
from . import BaseTargetScraper, target_cli
from ..base_scraper import fetch_present, fetch_text, github_json, github_tree, submodule_commit
from ..es_systems import es_systems_entries
PLATFORM_NAME = "batocera"
@@ -256,9 +257,6 @@ def _parse_es_systems(text: str) -> dict[str, list[str]]:
machines under their name, and the package that builds a machine builds
the emulator.
"""
# Imported here: batocera_scraper imports this module at load time.
from ..batocera_scraper import es_systems_entries
package_to_emulators: dict[str, list[str]] = {}
for _system, emulator, core, spec in es_systems_entries(text):
require = spec.get("requireAnyOf", []) if spec else None
@@ -34,7 +34,7 @@ from typing import NamedTuple
from . import BaseTargetScraper, target_cli
from ..base_scraper import fetch_text, github_json
from ..batocera_scraper import _not_cores
from model.targets import not_cores
from ..emuelec_systems import (
BUILDS_OF,
ES_SYSTEMS_TEMPLATE,
@@ -567,7 +567,7 @@ def device_cores(
):
installed.update(install.cores)
offered, _standalone, _dual = es_cores(
systems, RUNNERS, BUILDS_OF, _not_cores(PLATFORM_NAME)
systems, RUNNERS, BUILDS_OF, not_cores(PLATFORM_NAME)
)
return sorted(installed & set(offered))
@@ -575,7 +575,7 @@ def device_cores(
def check_names(systems: SystemEmulators) -> None:
"""Every ES core name is installed by a package read or known unbuilt."""
offered, _standalone, _dual = es_cores(
systems, RUNNERS, BUILDS_OF, _not_cores(PLATFORM_NAME)
systems, RUNNERS, BUILDS_OF, not_cores(PLATFORM_NAME)
)
provided = {core for installs in PROVIDERS.values() for i in installs for core in i.cores}
unread = sorted(set(offered) - provided - set(UNBUILT))
+7 -7
View File
@@ -34,7 +34,7 @@ amiga500:
class ThePlatformListsEveryOfferedCore(unittest.TestCase):
def test_libretro_cores_and_standalone_emulators(self):
from scripts.scraper.batocera_scraper import es_systems_emulators
from scripts.scraper.es_systems import es_systems_emulators
cores, standalone = es_systems_emulators(ES_SYSTEMS)
self.assertEqual(
@@ -43,9 +43,9 @@ class ThePlatformListsEveryOfferedCore(unittest.TestCase):
self.assertEqual(standalone, {"dosbox", "amiberry", "A500"})
def test_the_overrides_drop_the_machine_selectors(self):
from scripts.scraper.batocera_scraper import _not_cores
from model.targets import not_cores
self.assertIn("A500", _not_cores())
self.assertIn("A500", not_cores("batocera"))
def test_the_registry_leaves_the_list_to_the_scraper(self):
import yaml
@@ -60,7 +60,7 @@ class OneWalkReadsEsSystems(unittest.TestCase):
"""The scraper and the target scraper read es_systems.yml through one walk."""
def test_every_core_in_file_order(self):
from scripts.scraper.batocera_scraper import es_systems_entries
from scripts.scraper.es_systems import es_systems_entries
rows = [(s, e, c) for s, e, c, _spec in es_systems_entries(ES_SYSTEMS)]
self.assertEqual(rows, [
@@ -71,13 +71,13 @@ class OneWalkReadsEsSystems(unittest.TestCase):
])
def test_an_emulator_without_core_is_yielded_once(self):
from scripts.scraper.batocera_scraper import es_systems_entries
from scripts.scraper.es_systems import es_systems_entries
text = "x:\n emulators:\n vice:\n archs_include: [x86_64]\n"
self.assertEqual(list(es_systems_entries(text)), [("x", "vice", None, None)])
def test_a_file_that_is_not_a_mapping_is_refused(self):
from scripts.scraper.batocera_scraper import es_systems_entries
from scripts.scraper.es_systems import es_systems_entries
from scripts.scraper.targets.batocera_targets_scraper import _parse_es_systems
with self.assertRaises(ValueError):
@@ -102,7 +102,7 @@ class AnEmulatorLaunchedBothWaysGetsBothLayouts(unittest.TestCase):
dolphin-emu/Sys/GC/ files: no IPL, no DSP ROM."""
def test_the_scraper_names_dual_mode_emulators(self):
from scripts.scraper.batocera_scraper import es_systems_emulators, es_systems_libretro
from scripts.scraper.es_systems import es_systems_emulators, es_systems_libretro
text = ES_SYSTEMS + (
"gamecube:\n emulators:\n libretro:\n dolphin: { requireAnyOf: [X] }\n"
+2 -1
View File
@@ -268,6 +268,7 @@ class OnlyWhatABoardBuildsIsRun(unittest.TestCase):
def test_only_what_a_board_builds_or_the_user_supplies_is_run(self):
from scraper import batocera_scraper as bs
from scraper.es_systems import es_systems_by_system
# A board selects kodi's package and buildroot's mpv. Citron is
# listed by es_systems, so the symbol of a folder of that name does
@@ -281,7 +282,7 @@ class OnlyWhatABoardBuildsIsRun(unittest.TestCase):
self.assertEqual(sorted(standalone), ["kodi", "lexaloffle", "mpv", "vox_official"])
# Each system keeps the list es_systems.yml writes (whose files its
# entries ask for), less a default no package builds.
by_system = bs.es_systems_by_system(self.ES, self.DEFAULTS, set(), built)
by_system = es_systems_by_system(self.ES, self.DEFAULTS, set(), built)
self.assertEqual(by_system["switch"], ["citron"])
self.assertEqual(by_system["voxatron"], ["lexaloffle", "vox_official"])
self.assertEqual(by_system["gba"], ["mgba"])
+1 -1
View File
@@ -787,7 +787,7 @@ class TheScrapersReadTheSystemLists(unittest.TestCase):
"""libretro_systems, read from each platform's own system list."""
def test_batocera_and_knulli_es_systems_yml(self):
from scripts.scraper.batocera_scraper import es_systems_libretro_systems
from scripts.scraper.es_systems import es_systems_libretro_systems
text = (
"gamecube:\n emulators:\n dolphin:\n dolphin: { requireAnyOf: [A] }\n"