mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-12-02 22:38:54 -06:00
refactor: make names shared across modules public
This commit is contained in:
1 parent
c3b67d5485
commit
61e302d8f6
45 files changed
+653
-653
No files matched your search
@@ -34,12 +34,12 @@ from model.safeparse import parse_untrusted_xml, yaml_load_upstream
|
||||
from . import Fetcher, SourceError, register
|
||||
|
||||
|
||||
def _board(core: str | None, emulator: str | None, source_ref: str) -> dict:
|
||||
def board_entry(core: str | None, emulator: str | None, source_ref: str) -> dict:
|
||||
out = {"core": core or emulator, "emulator": emulator, "source_ref": source_ref}
|
||||
return {k: str(v) for k, v in out.items() if v}
|
||||
|
||||
|
||||
def _offers(choices: list[dict], core: str | None, emulator: str | None) -> list[dict]:
|
||||
def folder_offers(choices: list[dict], core: str | None, emulator: str | None) -> list[dict]:
|
||||
"""Every (emulator, core) a folder can start, in the list's order, or
|
||||
none when the default is the only one.
|
||||
|
||||
@@ -87,7 +87,7 @@ def _top_key_lines(text: str) -> dict[str, int]:
|
||||
return out
|
||||
|
||||
|
||||
def _strip_prefix(path: str, prefixes: list[str]) -> str | None:
|
||||
def strip_prefix(path: str, prefixes: list[str]) -> str | None:
|
||||
"""The folder under the roms root, or None for a path outside it."""
|
||||
for prefix in prefixes:
|
||||
if path.startswith(prefix):
|
||||
@@ -96,7 +96,7 @@ def _strip_prefix(path: str, prefixes: list[str]) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def _extensions(raw: str | list | None) -> list[str]:
|
||||
def extension_list(raw: str | list | None) -> list[str]:
|
||||
words = raw.split() if isinstance(raw, str) else [str(w) for w in raw or []]
|
||||
return [w if w.startswith(".") else f".{w}" for w in words if w]
|
||||
|
||||
@@ -163,14 +163,14 @@ def batocera_es(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
override = data.get(name) or {}
|
||||
b_emu, b_core = override.get("emulator", emulator), override.get("core", core)
|
||||
if (b_emu, b_core) != (emulator, core) and b_core:
|
||||
board_defaults[board] = _board(b_core, b_emu, format_ref(key, real, lines.get(name)))
|
||||
board_defaults[board] = board_entry(b_core, b_emu, format_ref(key, real, lines.get(name)))
|
||||
refs = [format_ref(key, es_path, es_lines.get(name))]
|
||||
if name in def_lines:
|
||||
refs.append(format_ref(key, def_path, def_lines[name]))
|
||||
rows.append({
|
||||
"folder": str(folder),
|
||||
"name": name,
|
||||
"extensions": _extensions(system.get("extensions")),
|
||||
"extensions": extension_list(system.get("extensions")),
|
||||
"core": str(core) if core else None,
|
||||
"emulator": str(emulator) if emulator else None,
|
||||
"source_ref": "; ".join(refs),
|
||||
@@ -184,7 +184,7 @@ def batocera_es(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
# Static es_systems.cfg (XML) and EmuELEC's es_systems.json
|
||||
|
||||
|
||||
def _first_lines(text: str, pattern: str) -> dict[str, int]:
|
||||
def first_lines(text: str, pattern: str) -> dict[str, int]:
|
||||
out: dict[str, int] = {}
|
||||
for number, line in enumerate(text.splitlines(), 1):
|
||||
match = re.search(pattern, line)
|
||||
@@ -261,7 +261,7 @@ def es_xml(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
for row in rows:
|
||||
alt = other.get(row["name"])
|
||||
if alt and alt["core"] and (alt["core"], alt["emulator"]) != (row["core"], row["emulator"]):
|
||||
row.setdefault("board_defaults", {})[board] = _board(
|
||||
row.setdefault("board_defaults", {})[board] = board_entry(
|
||||
alt["core"], alt["emulator"], alt["source_ref"])
|
||||
return rows
|
||||
|
||||
@@ -270,7 +270,7 @@ _BARE_AMPERSAND = re.compile(r"&(?!(?:[A-Za-z]+|#\d+|#x[0-9A-Fa-f]+);)")
|
||||
_COMMENT = re.compile(r"<!--.*?-->", re.DOTALL)
|
||||
|
||||
|
||||
def _lenient_xml(text: str) -> str:
|
||||
def lenient_xml(text: str) -> str:
|
||||
"""The list as EmulationStation's pugixml reads it, lines kept in place.
|
||||
|
||||
pugixml keeps a bare `&` as text (ArkOS ships `2>&1` in a command) and
|
||||
@@ -283,7 +283,7 @@ def _lenient_xml(text: str) -> str:
|
||||
|
||||
|
||||
def _xml_rows(text: str, key: str, path: str, params: dict) -> list[dict]:
|
||||
text = _lenient_xml(text)
|
||||
text = lenient_xml(text)
|
||||
root = parse_untrusted_xml(text, path)
|
||||
lines = _all_lines(text, r"<name>([^<]+)</name>")
|
||||
seen: dict[str, int] = {}
|
||||
@@ -296,7 +296,7 @@ def _xml_rows(text: str, key: str, path: str, params: dict) -> list[dict]:
|
||||
row = _name_once({"name": name}, lines, seen)
|
||||
if not name or "ignore" in (command, platform):
|
||||
continue
|
||||
folder = _strip_prefix((system.findtext("path") or "").strip(), params["path_prefixes"])
|
||||
folder = strip_prefix((system.findtext("path") or "").strip(), params["path_prefixes"])
|
||||
if not folder:
|
||||
continue
|
||||
emulators = [
|
||||
@@ -313,11 +313,11 @@ def _xml_rows(text: str, key: str, path: str, params: dict) -> list[dict]:
|
||||
rows.append({
|
||||
"folder": folder,
|
||||
"name": row["name"],
|
||||
"extensions": _extensions(system.findtext("extension")),
|
||||
"extensions": extension_list(system.findtext("extension")),
|
||||
"core": core or emulator,
|
||||
"emulator": emulator,
|
||||
"source_ref": format_ref(key, path, row["line"]),
|
||||
"offers": _offers(
|
||||
"offers": folder_offers(
|
||||
[{"emulator": e, "core": c} for e, cores in emulators for c, _d in cores or [(e, False)]],
|
||||
core, emulator),
|
||||
"arcade": _is_arcade(system.findtext("hardware"), platform),
|
||||
@@ -340,7 +340,7 @@ def es_json(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
row = _name_once({"name": name}, lines, seen)
|
||||
if "ignore" in (system.get("command"), system.get("platform")):
|
||||
continue
|
||||
folder = _strip_prefix(system.get("path") or "", params["path_prefixes"])
|
||||
folder = strip_prefix(system.get("path") or "", params["path_prefixes"])
|
||||
if not name or not folder:
|
||||
continue
|
||||
emulators = [
|
||||
@@ -352,11 +352,11 @@ def es_json(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
rows.append({
|
||||
"folder": folder,
|
||||
"name": row["name"],
|
||||
"extensions": _extensions(system.get("extensions")),
|
||||
"extensions": extension_list(system.get("extensions")),
|
||||
"core": core or emulator,
|
||||
"emulator": emulator,
|
||||
"source_ref": format_ref(key, path, row["line"]),
|
||||
"offers": _offers(
|
||||
"offers": folder_offers(
|
||||
[{"emulator": e, "core": c} for e, cores in emulators for c, _d in cores or [(e, False)]],
|
||||
core, emulator),
|
||||
"arcade": _is_arcade(system.get("hardware"), system.get("platform")),
|
||||
|
||||
@@ -10,7 +10,7 @@ from model.romfolders import format_ref
|
||||
from model.safeparse import parse_untrusted_xml
|
||||
|
||||
from . import Fetcher, SourceError, register
|
||||
from .es import _extensions, _first_lines, _lenient_xml, _offers, _strip_prefix
|
||||
from .es import extension_list, first_lines, lenient_xml, folder_offers, strip_prefix
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -33,13 +33,13 @@ def command_core(command: str) -> tuple[str | None, str | None]:
|
||||
|
||||
|
||||
def esde_systems(text: str, path: str) -> Iterator[tuple[str, str, list[str], list[tuple[str, str]]]]:
|
||||
root = parse_untrusted_xml(_lenient_xml(text), path)
|
||||
root = parse_untrusted_xml(lenient_xml(text), path)
|
||||
for system in root.iter("system"):
|
||||
name = (system.findtext("name") or "").strip()
|
||||
rom_path = (system.findtext("path") or "").strip()
|
||||
commands = [((c.get("label") or "").strip(), (c.text or "").strip())
|
||||
for c in system.findall("command")]
|
||||
yield name, rom_path, _extensions(system.findtext("extension")), commands
|
||||
yield name, rom_path, extension_list(system.findtext("extension")), commands
|
||||
|
||||
|
||||
def _alternative_emulators(text: str, function: str) -> dict[str, tuple[str, int]]:
|
||||
@@ -116,7 +116,7 @@ def _searched_by(commands: list[tuple[str, str, int | None]], key: str, path: st
|
||||
continue
|
||||
reads = [
|
||||
folder for part in words[at + 1].split(";")
|
||||
if (folder := _strip_prefix(part, ["%ROMPATH%/", "%ROMPATH%\\"]))
|
||||
if (folder := strip_prefix(part, ["%ROMPATH%/", "%ROMPATH%\\"]))
|
||||
]
|
||||
if not reads:
|
||||
continue
|
||||
@@ -167,11 +167,11 @@ def esde(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
systems: dict[str, dict] = {}
|
||||
for item in [params["systems"], *params.get("custom", [])]:
|
||||
key, path = item["source"], item["path"]
|
||||
text = _lenient_xml(fetcher.text(key, path))
|
||||
lines = _first_lines(text, r"<name>([^<]+)</name>")
|
||||
text = lenient_xml(fetcher.text(key, path))
|
||||
lines = first_lines(text, r"<name>([^<]+)</name>")
|
||||
command_lines = _command_lines(text)
|
||||
for name, rom_path, extensions, commands in esde_systems(text, path):
|
||||
folder = _strip_prefix(rom_path, ["%ROMPATH%/", "%ROMPATH%\\"])
|
||||
folder = strip_prefix(rom_path, ["%ROMPATH%/", "%ROMPATH%\\"])
|
||||
if not name or not folder:
|
||||
continue
|
||||
start = lines.get(name) or 0
|
||||
@@ -207,7 +207,7 @@ def esde(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
else:
|
||||
note = f"alternative emulator '{label}' names no command of this system"
|
||||
emulator, core = command_core(chosen[1])
|
||||
offers = _offers([
|
||||
offers = folder_offers([
|
||||
{"emulator": e, "core": c, "source_ref": format_ref(key_, path_, line)}
|
||||
for _label, command, line, key_, path_ in system["placed"]
|
||||
for e, c in [command_core(command)] if e
|
||||
|
||||
@@ -12,7 +12,7 @@ from model.romfolders import DEFAULT_ROW_SOURCE, format_ref
|
||||
from ..recalbox_romfs import Defconfig, defconfig_boards, read_system_ini
|
||||
from ..retropie_modules import PLATFORM_FLAGS, module_enabled, module_fields
|
||||
from . import Fetcher, SourceError, register
|
||||
from .es import _board, _extensions, _offers, _strip_prefix
|
||||
from .es import board_entry, extension_list, folder_offers, strip_prefix
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -134,7 +134,7 @@ def elec_conf(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
if conf_text is None:
|
||||
continue
|
||||
values, value_lines = _conf_values(conf_text)
|
||||
folder = _strip_prefix(values.get("SYSTEM_PATH", ""), params["path_prefixes"])
|
||||
folder = strip_prefix(values.get("SYSTEM_PATH", ""), params["path_prefixes"])
|
||||
if not folder:
|
||||
continue
|
||||
mine = [c for c in cores if c["system"] == name]
|
||||
@@ -150,7 +150,7 @@ def elec_conf(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
board = _board_key(c["branches"])
|
||||
if c is not pick and board and (c["emulator"], c["core"]) != (
|
||||
pick and pick["emulator"], pick and pick["core"]):
|
||||
board_defaults[board] = _board(c["core"], c["emulator"], format_ref(key, package, c["line"]))
|
||||
board_defaults[board] = board_entry(c["core"], c["emulator"], format_ref(key, package, c["line"]))
|
||||
refs = [
|
||||
format_ref(key, conf_path, value_lines.get("SYSTEM_PATH"), value_lines.get("SYSTEM_EXTENSION")),
|
||||
format_ref(key, package, *lines),
|
||||
@@ -164,12 +164,12 @@ def elec_conf(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
rows.append({
|
||||
"folder": folder,
|
||||
"name": name,
|
||||
"extensions": _extensions(values.get("SYSTEM_EXTENSION")),
|
||||
"extensions": extension_list(values.get("SYSTEM_EXTENSION")),
|
||||
"core": pick["core"] if pick else None,
|
||||
"emulator": pick["emulator"] if pick else None,
|
||||
"source_ref": "; ".join(refs),
|
||||
"board_defaults": board_defaults,
|
||||
"offers": _offers([
|
||||
"offers": folder_offers([
|
||||
{"emulator": c["emulator"], "core": c["core"],
|
||||
"when": ", ".join(f"{v} {p}".strip() for v, p in c["branches"]),
|
||||
"source_ref": format_ref(key, package, c["line"])}
|
||||
@@ -235,7 +235,7 @@ def recalbox(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
ini = read_system_ini(text)
|
||||
sections, lines = ini.sections, ini.lines
|
||||
system = sections.get("system", {})
|
||||
folder = _strip_prefix(system.get("roms.folder", ""), ["%ROOT%/"])
|
||||
folder = strip_prefix(system.get("roms.folder", ""), ["%ROOT%/"])
|
||||
if not folder:
|
||||
continue
|
||||
picks: dict[str, dict] = {}
|
||||
@@ -272,7 +272,7 @@ def recalbox(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
core, emulator = max(shared, key=lambda pair: shared[pair])
|
||||
first = next(pick for pick in picks.values() if (pick["core"], pick["emulator"]) == (core, emulator))
|
||||
board_defaults = {
|
||||
board: _board(pick["core"], pick["emulator"], pick["source_ref"])
|
||||
board: board_entry(pick["core"], pick["emulator"], pick["source_ref"])
|
||||
for board, pick in picks.items() if (pick["core"], pick["emulator"]) != (core, emulator)
|
||||
}
|
||||
archive = any(e.endswith("!") for e in extensions)
|
||||
@@ -380,7 +380,7 @@ def retropie(spec: dict, fetcher: Fetcher) -> list[dict]:
|
||||
emulator = "retroarch" if core and core.startswith("lr-") else core
|
||||
rows.append({
|
||||
"folder": system,
|
||||
"extensions": _extensions(raw),
|
||||
"extensions": extension_list(raw),
|
||||
"core": core,
|
||||
"emulator": emulator,
|
||||
"source_ref": "; ".join(refs),
|
||||
|
||||
Reference in new issue
Block a user