fix: write rom-folder refs through one formatter

This commit is contained in:
Abdessamad Derraz committed 2026-11-23 21:02:27 +01:00
1 parent eec854cc0b
commit ed73caf489
5 files changed
+71 -49

No files matched your search

+9
View File
@@ -31,6 +31,15 @@ SOURCES_DIR = ROMFOLDERS_DIR / "_sources"
_REF_PART = re.compile(r"^(?P<key>[a-z0-9][a-z0-9-]*) (?P<path>\S+?)(?::(?P<lines>\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*))?$")
def format_ref(key: str, path: str, *lines: int | str | None) -> str:
"""One source_ref part that split_refs reads back. A space in the path is
written %20, as in a URL, since the grammar ends a path at the first space.
Absent lines (None, empty, 0) are left out, the others joined by commas."""
path = path.replace("%", "%25").replace(" ", "%20")
kept = [str(n) for n in lines if n not in (None, "", 0)]
return f"{key} {path}:{','.join(kept)}" if kept else f"{key} {path}"
def split_refs(source_ref: str) -> list[tuple[str, str, str | None]]:
"""Each (source key, path, lines) a source_ref names, or ValueError."""
parts: list[tuple[str, str, str | None]] = []
+17 -22
View File
@@ -31,6 +31,8 @@ from collections.abc import Iterable
from configparser import ConfigParser
from pathlib import PurePosixPath
from model.romfolders import format_ref
from . import Fetcher, SourceError, register
_LIBRETRO = re.compile(r"([A-Za-z0-9_+.\-${}]+?)_libretro\.so")
@@ -43,13 +45,6 @@ def line_of(text: str, needle: str, start: int = 0) -> int:
return text.count("\n", 0, idx) + 1 if idx >= 0 else 0
def ref(key: str, path: str, line: int | str | None = None) -> str:
"""A source_ref part. A space in a path is written %20, as in a URL:
the ref grammar ends a path at the first space."""
path = path.replace("%", "%25").replace(" ", "%20")
return f"{key} {path}:{line}" if line else f"{key} {path}"
def extensions(raw: str | Iterable[str] | None) -> list[str]:
"""Lowercased extensions with the dot, from a list or a `|`/`,` string."""
items = re.split(r"[|,]", raw) if isinstance(raw, str) else list(raw or [])
@@ -117,7 +112,7 @@ def parse_onion(spec: dict, fetcher: Fetcher) -> list[dict]:
script = fetcher.optional_text(key, script_path) or ""
packages.setdefault(_rompath_folder(config.get("rompath", "")), []).append({
"layer": layer, "package": package, "config": config,
"core": libretro_core(script), "ref": ref(key, path, line_of(text, '"rompath"')),
"core": libretro_core(script), "ref": format_ref(key, path, line_of(text, '"rompath"')),
"script": script_path,
})
rows = []
@@ -169,7 +164,7 @@ def parse_spruce(spec: dict, fetcher: Fetcher) -> list[dict]:
"aliases": [str(a) for a in config.get("alternativeFolderNames") or []],
"extensions": extensions(config.get("extlist")),
"core": config.get("default_emulator"),
"source_ref": ref(key, path, line_of(text, '"default_emulator"') or 1),
"source_ref": format_ref(key, path, line_of(text, '"default_emulator"') or 1),
"note": ". ".join(notes),
})
return rows
@@ -200,7 +195,7 @@ def parse_crossmix(spec: dict, fetcher: Fetcher) -> list[dict]:
"extensions": extensions(config.get("extlist")),
"core": core,
"emulator": "retroarch" if core else "standalone",
"source_ref": ref(key, path, line_of(text, '"rompath"') or 1),
"source_ref": format_ref(key, path, line_of(text, '"rompath"') or 1),
"note": note if core else f"{note}, names no libretro core",
})
return rows
@@ -247,7 +242,7 @@ def parse_koriki(spec: dict, fetcher: Fetcher) -> list[dict]:
"extensions": extensions(body.get("romExts")),
"core": core or (execs[0] if execs else None),
"emulator": "retroarch" if core else "standalone",
"source_ref": ref(key, path, line_of(text, f"[{section}]")),
"source_ref": format_ref(key, path, line_of(text, f"[{section}]")),
"note": note,
})
return rows
@@ -328,7 +323,7 @@ def parse_allium(spec: dict, fetcher: Fetcher) -> list[dict]:
"extensions": extensions(console.get("extensions")),
"core": core or first,
"emulator": "retroarch" if core else "standalone",
"source_ref": ref(key, params["consoles"], console["__line__"]),
"source_ref": format_ref(key, params["consoles"], console["__line__"]),
"note": "a folder named any pattern, or ending in '(<pattern>)', is this console. A file's own extension wins over its folder",
})
return rows
@@ -369,7 +364,7 @@ def parse_minui(spec: dict, fetcher: Fetcher) -> list[dict]:
script = fetcher.text(key, path)
core = libretro_core(script)
line = line_of(script, "EMU_EXE=") or line_of(script, "_libretro.so") or 1
paks.setdefault(match.group(1), {})[_pak_device(path)] = (core or "", ref(key, path, line))
paks.setdefault(match.group(1), {})[_pak_device(path)] = (core or "", format_ref(key, path, line))
rows = []
for tag, devices in sorted(paks.items()):
counts: dict[str, int] = {}
@@ -430,7 +425,7 @@ def parse_plumos(spec: dict, fetcher: Fetcher) -> list[dict]:
"extensions": extensions(system.get("extensions")),
"core": core or None,
"emulator": kind or None,
"source_ref": ref(key, params["path"], line),
"source_ref": format_ref(key, params["path"], line),
"note": ". ".join(notes),
})
return rows
@@ -466,7 +461,7 @@ def parse_muos(spec: dict, fetcher: Fetcher) -> list[dict]:
"aliases": friendly,
"core": core,
"emulator": "retroarch" if so else "external",
"source_ref": ref(key, params["path"], line_of(text, json.dumps(system) + ":")),
"source_ref": format_ref(key, params["path"], line_of(text, json.dumps(system) + ":")),
"note": "a folder matches when its name, lowercased without ' -_+', is one of the friendly names",
})
return rows
@@ -493,7 +488,7 @@ def parse_gmenu2x(spec: dict, fetcher: Fetcher) -> list[dict]:
"extensions": extensions(fields.get("selectorfilter")),
"core": link["core"],
"emulator": "standalone",
"source_ref": ref(link["source"], link["path"], line_of(text, "selectordir=")),
"source_ref": format_ref(link["source"], link["path"], line_of(text, "selectordir=")),
"note": f"gmenu2x link of the {link['core']} package",
}
key = params["source"]
@@ -503,7 +498,7 @@ def parse_gmenu2x(spec: dict, fetcher: Fetcher) -> list[dict]:
continue
rows[parts[0]] = {
"folder": parts[0],
"source_ref": ref(key, f"{params['shipped_roms']}/{parts[0]}"),
"source_ref": format_ref(key, f"{params['shipped_roms']}/{parts[0]}"),
"note": "folder the image ships. Its emulator link lives in a release package no repository holds",
}
return list(rows.values())
@@ -521,7 +516,7 @@ def parse_mister(spec: dict, fetcher: Fetcher) -> list[dict]:
{
"folder": folder,
"emulator": "mister-core",
"source_ref": ref(key, f"games/{folder}/"),
"source_ref": format_ref(key, f"games/{folder}/"),
"arcade": folder in arcade or None,
"note": params["arcade_note"] if folder in arcade else None,
}
@@ -573,9 +568,9 @@ def parse_romm(spec: dict, fetcher: Fetcher) -> list[dict]:
if found:
note = None if slug in stable else "core only with EmulatorJS nightly cores"
rows.append({"folder": slug, "core": found[0][0], "emulator": "emulatorjs",
"source_ref": ref(key, params["cores_map"], found[1]), "note": note})
"source_ref": format_ref(key, params["cores_map"], found[1]), "note": note})
else:
rows.append({"folder": slug, "source_ref": ref(key, params["bios_fixture"]),
rows.append({"folder": slug, "source_ref": format_ref(key, params["bios_fixture"]),
"note": "no EmulatorJS core, RomM keeps BIOS for it"})
return rows
@@ -613,11 +608,11 @@ def parse_bizhawk(spec: dict, fetcher: Fetcher) -> list[dict]:
continue
sids = [match.group(1)] if match.group(1) else combined.get(match.group(2), [])
exts: list[str] = []
refs = [ref(key, params["paths"], number)]
refs = [format_ref(key, params["paths"], number)]
for sid in sids:
if sid in ext_fields:
exts.extend(ext_fields[sid][0])
refs.append(ref(key, params["rom_loader"], ext_fields[sid][1]))
refs.append(format_ref(key, params["rom_loader"], ext_fields[sid][1]))
cores = [preferred[s] for s in sids if s in preferred]
note = "the open dialog starts here, nothing is scanned"
if cores:
+20 -24
View File
@@ -31,6 +31,7 @@ import shlex
from collections import Counter
from collections.abc import Iterator
from model.romfolders import format_ref
from model.safeparse import parse_untrusted_xml, yaml_load_upstream
from ..retropie_modules import PLATFORM_FLAGS, module_enabled, module_fields
@@ -38,11 +39,6 @@ from ..recalbox_romfs import Defconfig, defconfig_boards, read_system_ini
from . import Fetcher, SourceError, register
def _ref(key: str, path: str, *lines: int | str | None) -> str:
kept = [str(n) for n in lines if n not in (None, "")]
return f"{key} {path}:{','.join(kept)}" if kept else f"{key} {path}"
def _board(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}
@@ -172,10 +168,10 @@ 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, _ref(key, real, lines.get(name)))
refs = [_ref(key, es_path, es_lines.get(name))]
board_defaults[board] = _board(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(_ref(key, def_path, def_lines[name]))
refs.append(format_ref(key, def_path, def_lines[name]))
rows.append({
"folder": str(folder),
"name": name,
@@ -325,7 +321,7 @@ def _xml_rows(text: str, key: str, path: str, params: dict) -> list[dict]:
"extensions": _extensions(system.findtext("extension")),
"core": core or emulator,
"emulator": emulator,
"source_ref": _ref(key, path, row["line"]),
"source_ref": format_ref(key, path, row["line"]),
"offers": _offers(
[{"emulator": e, "core": c} for e, cores in emulators for c, _d in cores or [(e, False)]],
core, emulator),
@@ -364,7 +360,7 @@ def es_json(spec: dict, fetcher: Fetcher) -> list[dict]:
"extensions": _extensions(system.get("extensions")),
"core": core or emulator,
"emulator": emulator,
"source_ref": _ref(key, path, row["line"]),
"source_ref": format_ref(key, path, row["line"]),
"offers": _offers(
[{"emulator": e, "core": c} for e, cores in emulators for c, _d in cores or [(e, False)]],
core, emulator),
@@ -509,13 +505,13 @@ 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"], _ref(key, package, c["line"]))
board_defaults[board] = _board(c["core"], c["emulator"], format_ref(key, package, c["line"]))
refs = [
_ref(key, conf_path, value_lines.get("SYSTEM_PATH"), value_lines.get("SYSTEM_EXTENSION")),
_ref(key, package, *lines),
format_ref(key, conf_path, value_lines.get("SYSTEM_PATH"), value_lines.get("SYSTEM_EXTENSION")),
format_ref(key, package, *lines),
]
if pick:
refs.append(_ref(key, package, pick["line"]))
refs.append(format_ref(key, package, pick["line"]))
note = None
if pick and pick["branches"] and not _board_key(pick["branches"]) and pick["branches"][-1][1] != "*":
note = "default core chosen under a build condition: " + ", ".join(
@@ -531,7 +527,7 @@ def elec_conf(spec: dict, fetcher: Fetcher) -> list[dict]:
"offers": _offers([
{"emulator": c["emulator"], "core": c["core"],
"when": ", ".join(f"{v} {p}".strip() for v, p in c["branches"]),
"source_ref": _ref(key, package, c["line"])}
"source_ref": format_ref(key, package, c["line"])}
for c in mine
], pick["core"] if pick else None, pick["emulator"] if pick else None),
"arcade": values.get("SYSTEM_HARDWARE") == "arcade" or None,
@@ -666,7 +662,7 @@ def _searched_by(commands: list[tuple[str, str, int | None]], key: str, path: st
row["machines"] = record["machines"]
if record["any_machine"]:
row["any_machine"] = True
row["source_ref"] = _ref(key, path, *record["lines"])
row["source_ref"] = format_ref(key, path, *record["lines"])
rows.append(row)
return rows
@@ -709,7 +705,7 @@ def esde(spec: dict, fetcher: Fetcher) -> list[dict]:
]
systems[name] = {
"folder": folder, "extensions": extensions, "commands": commands,
"ref": _ref(key, path, lines.get(name)),
"ref": format_ref(key, path, lines.get(name)),
"searched_by": _searched_by(placed, key, path),
"placed": [(*c, key, path) for c in placed],
}
@@ -728,14 +724,14 @@ def esde(spec: dict, fetcher: Fetcher) -> list[dict]:
if name in alternatives:
label, line = alternatives[name]
match = next((c for c in commands if c[0] == label), None)
refs.append(_ref(alt["source"], alt["path"], line))
refs.append(format_ref(alt["source"], alt["path"], line))
if match:
chosen = match
else:
note = f"alternative emulator '{label}' names no command of this system"
emulator, core = command_core(chosen[1])
offers = _offers([
{"emulator": e, "core": c, "source_ref": _ref(key_, path_, line)}
{"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
], core, emulator)
@@ -838,8 +834,8 @@ def recalbox(spec: dict, fetcher: Fetcher) -> list[dict]:
cores.append({
"core": core, "emulator": emulator, "priority": int(priority or "255"),
"source_ref": "; ".join([
_ref(key, path, priority_line, core_line),
_ref(key, config_path, enabled[package]),
format_ref(key, path, priority_line, core_line),
format_ref(key, config_path, enabled[package]),
]),
})
if cores:
@@ -861,7 +857,7 @@ def recalbox(spec: dict, fetcher: Fetcher) -> list[dict]:
"files": sorted(names),
"core": core,
"emulator": emulator,
"source_ref": "; ".join([_ref(key, path, lines.get("system/roms.folder")), first["source_ref"]]),
"source_ref": "; ".join([format_ref(key, path, lines.get("system/roms.folder")), first["source_ref"]]),
"board_defaults": board_defaults,
"arcade": sections.get("properties", {}).get("type") == "arcade" or None,
"note": "an extension ending in ! is an archive the frontend does not open" if archive else None,
@@ -950,11 +946,11 @@ def retropie(spec: dict, fetcher: Fetcher) -> list[dict]:
defaults.setdefault(system, (emu, module["path"], line))
rows: list[dict] = []
for system, (raw, line) in sorted(exts.items()):
refs = [_ref(key, cfg_path, line)]
refs = [format_ref(key, cfg_path, line)]
core = None
if system in defaults:
core, path, call = defaults[system]
refs.append(_ref(key, path, call))
refs.append(format_ref(key, path, call))
emulator = "retroarch" if core and core.startswith("lr-") else core
rows.append({
"folder": system,