refactor: render setup files from the library

dump_setup moves next to scrape_setup, so readers of the setup format
no longer import the command. base_scraper names its dumper
PlatformDumper, and the command writes through write_if_changed.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 16:43:46 +01:00
1 parent 12b00e024b
commit b6599375f5
6 files changed
+21 -23

No files matched your search

+3 -3
View File
@@ -535,7 +535,7 @@ def fetch_branch_head(repo: str, branch: str | None = None) -> str:
return sha
class _PlatformDumper(yaml.SafeDumper):
class PlatformDumper(yaml.SafeDumper):
"""Quote scalars that a YAML reader could misparse.
Names such as "7800 BIOS (E).rom" or "Bandai - WonderSwan: x" are plain
@@ -557,7 +557,7 @@ def _quoted_str(dumper: yaml.SafeDumper, data: str) -> yaml.ScalarNode:
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style=style)
_PlatformDumper.add_representer(str, _quoted_str)
PlatformDumper.add_representer(str, _quoted_str)
def exit_on_scrape_error(error: Exception) -> NoReturn:
@@ -655,7 +655,7 @@ def _write_output(
# Written whole or not at all: a reader in the middle of a streamed
# dump parsed a valid platform with a fraction of its systems.
write_text_atomic(output_path, yaml.dump(
config, Dumper=_PlatformDumper,
config, Dumper=PlatformDumper,
default_flow_style=False, allow_unicode=True, sort_keys=False,
))
print(f"Written {len(reqs)} entries to {output}")
+11
View File
@@ -20,12 +20,16 @@ from collections.abc import Callable
from dataclasses import dataclass, field
from pathlib import Path
import yaml
if (_SCRIPTS_DIR := str(Path(__file__).resolve().parents[2])) not in sys.path:
sys.path.insert(0, _SCRIPTS_DIR)
from core import upstream
from model.sources import source_yaml_file
from ..base_scraper import PlatformDumper
CACHE_DIR = upstream.CACHE_DIR
# Keys a setup file carries, in the order they are written.
@@ -201,3 +205,10 @@ def scrape_setup(setup: str, spec: dict, fetcher: Fetcher) -> dict:
doc = {"setup": setup, **{k: spec[k] for k in SETUP_KEYS[1:-1] if k in spec}}
doc["folders"] = [folders[k] for k in sorted(folders, key=lambda f: (f.lower(), f))]
return doc
def dump_setup(doc: dict) -> str:
"""The bytes of platforms/romfolders/<setup>.yml for a setup document."""
return yaml.dump(
doc, Dumper=PlatformDumper, sort_keys=False, allow_unicode=True, width=100
)
+3 -15
View File
@@ -11,22 +11,13 @@ import argparse
import sys
from pathlib import Path
import yaml
from . import Fetcher, SourceError, load_sources, scrape_setup
from . import Fetcher, SourceError, dump_setup, load_sources, scrape_setup
from core.upstream import UpstreamError
from core.artifacts import write_text_atomic
from ..base_scraper import _PlatformDumper
from core.artifacts import write_if_changed
DEFAULT_DIR = Path("platforms") / "romfolders"
def dump_setup(doc: dict) -> str:
return yaml.dump(
doc, Dumper=_PlatformDumper, sort_keys=False, allow_unicode=True, width=100
)
def main() -> int:
parser = argparse.ArgumentParser(allow_abbrev=False, description=__doc__.splitlines()[0])
which = parser.add_mutually_exclusive_group(required=True)
@@ -64,10 +55,7 @@ def main() -> int:
print(f"{setup}: {len(doc['folders'])} folders")
if not args.dry_run:
args.output_dir.mkdir(parents=True, exist_ok=True)
target = args.output_dir / f"{setup}.yml"
text = dump_setup(doc)
if not target.exists() or target.read_text(encoding="utf-8") != text:
write_text_atomic(str(target), text)
write_if_changed(str(args.output_dir / f"{setup}.yml"), dump_setup(doc))
return 1 if failed else 0