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

+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))