mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-27 20:08:51 -06:00
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:
1 parent
12b00e024b
commit
b6599375f5
6 files changed
+21
-23
No files matched your search
@@ -535,7 +535,7 @@ def fetch_branch_head(repo: str, branch: str | None = None) -> str:
|
|||||||
return sha
|
return sha
|
||||||
|
|
||||||
|
|
||||||
class _PlatformDumper(yaml.SafeDumper):
|
class PlatformDumper(yaml.SafeDumper):
|
||||||
"""Quote scalars that a YAML reader could misparse.
|
"""Quote scalars that a YAML reader could misparse.
|
||||||
|
|
||||||
Names such as "7800 BIOS (E).rom" or "Bandai - WonderSwan: x" are plain
|
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)
|
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:
|
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
|
# 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.
|
# dump parsed a valid platform with a fraction of its systems.
|
||||||
write_text_atomic(output_path, yaml.dump(
|
write_text_atomic(output_path, yaml.dump(
|
||||||
config, Dumper=_PlatformDumper,
|
config, Dumper=PlatformDumper,
|
||||||
default_flow_style=False, allow_unicode=True, sort_keys=False,
|
default_flow_style=False, allow_unicode=True, sort_keys=False,
|
||||||
))
|
))
|
||||||
print(f"Written {len(reqs)} entries to {output}")
|
print(f"Written {len(reqs)} entries to {output}")
|
||||||
|
|||||||
@@ -20,12 +20,16 @@ from collections.abc import Callable
|
|||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
if (_SCRIPTS_DIR := str(Path(__file__).resolve().parents[2])) not in sys.path:
|
if (_SCRIPTS_DIR := str(Path(__file__).resolve().parents[2])) not in sys.path:
|
||||||
sys.path.insert(0, _SCRIPTS_DIR)
|
sys.path.insert(0, _SCRIPTS_DIR)
|
||||||
|
|
||||||
from core import upstream
|
from core import upstream
|
||||||
from model.sources import source_yaml_file
|
from model.sources import source_yaml_file
|
||||||
|
|
||||||
|
from ..base_scraper import PlatformDumper
|
||||||
|
|
||||||
CACHE_DIR = upstream.CACHE_DIR
|
CACHE_DIR = upstream.CACHE_DIR
|
||||||
|
|
||||||
# Keys a setup file carries, in the order they are written.
|
# 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 = {"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))]
|
doc["folders"] = [folders[k] for k in sorted(folders, key=lambda f: (f.lower(), f))]
|
||||||
return doc
|
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
|
||||||
|
)
|
||||||
@@ -11,22 +11,13 @@ import argparse
|
|||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import yaml
|
from . import Fetcher, SourceError, dump_setup, load_sources, scrape_setup
|
||||||
|
|
||||||
from . import Fetcher, SourceError, load_sources, scrape_setup
|
|
||||||
from core.upstream import UpstreamError
|
from core.upstream import UpstreamError
|
||||||
from core.artifacts import write_text_atomic
|
from core.artifacts import write_if_changed
|
||||||
from ..base_scraper import _PlatformDumper
|
|
||||||
|
|
||||||
DEFAULT_DIR = Path("platforms") / "romfolders"
|
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:
|
def main() -> int:
|
||||||
parser = argparse.ArgumentParser(allow_abbrev=False, description=__doc__.splitlines()[0])
|
parser = argparse.ArgumentParser(allow_abbrev=False, description=__doc__.splitlines()[0])
|
||||||
which = parser.add_mutually_exclusive_group(required=True)
|
which = parser.add_mutually_exclusive_group(required=True)
|
||||||
@@ -64,10 +55,7 @@ def main() -> int:
|
|||||||
print(f"{setup}: {len(doc['folders'])} folders")
|
print(f"{setup}: {len(doc['folders'])} folders")
|
||||||
if not args.dry_run:
|
if not args.dry_run:
|
||||||
args.output_dir.mkdir(parents=True, exist_ok=True)
|
args.output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
target = args.output_dir / f"{setup}.yml"
|
write_if_changed(str(args.output_dir / f"{setup}.yml"), dump_setup(doc))
|
||||||
text = dump_setup(doc)
|
|
||||||
if not target.exists() or target.read_text(encoding="utf-8") != text:
|
|
||||||
write_text_atomic(str(target), text)
|
|
||||||
return 1 if failed else 0
|
return 1 if failed else 0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
from model.romfolders import source_refs, split_refs
|
from model.romfolders import source_refs, split_refs
|
||||||
from model.safeparse import yaml_load
|
from model.safeparse import yaml_load
|
||||||
from scraper.romfolders import SourceError, load_sources, scrape_setup
|
from scraper.romfolders import SourceError, dump_setup, load_sources, scrape_setup
|
||||||
from scraper.romfolders.__main__ import dump_setup
|
|
||||||
from scraper.romfolders.card import (
|
from scraper.romfolders.card import (
|
||||||
libretro_core,
|
libretro_core,
|
||||||
parse_allium,
|
parse_allium,
|
||||||
|
|||||||
@@ -19,11 +19,11 @@ from model.romfolders import setup_paths, source_refs, split_refs
|
|||||||
from scraper.romfolders import (
|
from scraper.romfolders import (
|
||||||
Fetcher,
|
Fetcher,
|
||||||
SourceError,
|
SourceError,
|
||||||
|
dump_setup,
|
||||||
load_formats,
|
load_formats,
|
||||||
load_sources,
|
load_sources,
|
||||||
scrape_setup,
|
scrape_setup,
|
||||||
)
|
)
|
||||||
from scraper.romfolders.__main__ import dump_setup
|
|
||||||
from scraper.retropie_modules import module_enabled
|
from scraper.retropie_modules import module_enabled
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parent.parent
|
ROOT = Path(__file__).resolve().parent.parent
|
||||||
|
|||||||
@@ -1274,10 +1274,10 @@ class PlatformFileHasNoAnchors(unittest.TestCase):
|
|||||||
the platform file read back the same, but held &id001 and *id001."""
|
the platform file read back the same, but held &id001 and *id001."""
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from scraper.base_scraper import _PlatformDumper
|
from scraper.base_scraper import PlatformDumper
|
||||||
|
|
||||||
names = ["kick34005.A500", "amiga-os-130.rom"]
|
names = ["kick34005.A500", "amiga-os-130.rom"]
|
||||||
text = yaml.dump([{"names": names}, {"names": names}], Dumper=_PlatformDumper)
|
text = yaml.dump([{"names": names}, {"names": names}], Dumper=PlatformDumper)
|
||||||
self.assertNotIn("&id", text)
|
self.assertNotIn("&id", text)
|
||||||
self.assertNotIn("*id", text)
|
self.assertNotIn("*id", text)
|
||||||
self.assertEqual(yaml.safe_load(text), [{"names": names}, {"names": names}])
|
self.assertEqual(yaml.safe_load(text), [{"names": names}, {"names": names}])
|
||||||
|
|||||||
Reference in new issue
Block a user