refactor: reuse the shared helpers left copied

Copies of helpers the first pass created, replaced by the helper:
- source_yaml_file for our YAML sources (generate_db read them in the
  locale's encoding, a syntax error named no file);
- data_dir_registry_at in registry_bundles, platform_registry_at (new,
  beside load_platform_registry) in the manifest;
- DATA_DIRS_FILE for the registry path five modules spelled;
- load_optional_database, sha1_at, VERSIONS_FILE, installer_module;
- DerivedView where a cache was keyed by id() of a dict;
- count_text and series where a plural or a list was written by hand.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 10:31:29 +01:00
1 parent 32a4ca1fa1
commit 036a6ebf29
35 files changed
+141 -170

No files matched your search

+2 -2
View File
@@ -18,6 +18,7 @@ from typing import Any
from model.profiles import PROFILE_BACKUP_SUFFIX
from core.artifacts import write_text_atomic
from model.safeparse import yaml_load
from model.sources import source_yaml_file
_MAME_RELEASE_RE = re.compile(r"^0\.\d+")
@@ -350,8 +351,7 @@ def _diff_fbneo(
def _load_yaml(path: str) -> dict[str, Any]:
with open(path, encoding="utf-8") as f:
return yaml_load(f) or {}
return source_yaml_file(path) or {}
def _load_json(path: str) -> dict[str, Any]:
+3 -3
View File
@@ -31,7 +31,8 @@ from core.upstream import (
)
from core.upstream import download as upstream_download
from core.upstream import forge_headers as upstream_headers
from model.safeparse import require_yaml, yaml_load
from model.safeparse import require_yaml
from model.sources import source_yaml_file
try:
import yaml
@@ -612,8 +613,7 @@ def _keep_existing(config: dict, output_path: Path) -> None:
"""Merge into existing YAML: preserve fields the scraper doesn't generate
(data_directories, case_insensitive_fs, manually added metadata).
The scraper replaces systems + files, everything else is preserved."""
with open(output_path) as f:
existing = yaml_load(f) or {}
existing = source_yaml_file(output_path) or {}
# Preserve existing keys not generated by the scraper.
# Only keys present in the NEW config are considered scraper-generated.
# Everything else in the existing file is preserved.
+2 -3
View File
@@ -12,11 +12,10 @@ import ast
import re
import sys
from collections.abc import Callable, Iterator
from pathlib import Path
from model.database import load_database
from core.paths import PLATFORMS_DIR, REPO_ROOT
from core.paths import DB_FILE, PLATFORMS_DIR, REPO_ROOT
from model.targets import load_target_config, target_overrides
from core.hashing import cut_md5_owner
from model.safeparse import yaml_load_upstream
@@ -315,7 +314,7 @@ _MD5_RE = re.compile(r"^[a-fA-F0-9]+$")
def _load_md5_index() -> dict[str, str]:
"""Load by_md5 index from database.json for prefix resolution."""
db_path = Path(__file__).resolve().parents[2] / "database.json"
db_path = REPO_ROOT / DB_FILE
if not db_path.exists():
return {}
return load_database(str(db_path)).get("indexes", {}).get("by_md5", {})
+3 -3
View File
@@ -9,6 +9,7 @@ import urllib.error
import urllib.parse
from core.upstream import remote_etag as get_remote_etag
from core.paths import DATA_DIRS_FILE
from model.registry import data_dir_registry_at, load_versions
from .base_scraper import fetch_text
@@ -18,7 +19,6 @@ log = logging.getLogger(__name__)
BUILDBOT_SYSTEM_URL = "https://buildbot.libretro.com/assets/system/"
INDEX_URL = BUILDBOT_SYSTEM_URL + ".index"
DEFAULT_REGISTRY = "platforms/_data_dirs.yml"
def fetch_index() -> set[str]:
@@ -27,7 +27,7 @@ def fetch_index() -> set[str]:
def load_tracked_entries(
registry_path: str = DEFAULT_REGISTRY,
registry_path: str = DATA_DIRS_FILE,
) -> dict[str, tuple[str, str]]:
"""Load buildbot entries from _data_dirs.yml.
@@ -43,7 +43,7 @@ def load_tracked_entries(
return entries
def check(registry_path: str = DEFAULT_REGISTRY) -> dict:
def check(registry_path: str = DATA_DIRS_FILE) -> dict:
"""Run the buildbot check. Returns structured report."""
try:
index_zips = fetch_index()
+4 -3
View File
@@ -25,6 +25,8 @@ from model.platforms import load_platform_config
from model.profiles import source_yaml_paths
from core.artifacts import write_text_atomic
from model.safeparse import yaml_load
from model.sources import source_yaml_file
from core.cli import SourceUnreadable
from core import upstream
@@ -123,8 +125,7 @@ def _find_mame_profiles() -> list[Path]:
profiles: list[Path] = []
for path in source_yaml_paths(_EMULATORS_DIR):
try:
with open(path, encoding="utf-8") as f:
data = yaml_load(f)
data = source_yaml_file(path)
if not isinstance(data, dict):
continue
upstream = data.get("upstream", "")
@@ -149,7 +150,7 @@ def _find_mame_profiles() -> list[Path]:
)
):
profiles.append(path)
except (yaml.YAMLError, OSError) as exc:
except (SourceUnreadable, OSError) as exc:
# Skipped, a broken mame.yml left the list and the refresh
# reported success without touching it.
raise RuntimeError(f"cannot read {path}: {exc}") from exc
+2 -3
View File
@@ -24,7 +24,7 @@ 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.safeparse import yaml_load
from model.sources import source_yaml_file
CACHE_DIR = upstream.CACHE_DIR
@@ -160,8 +160,7 @@ def load_sources(directory: Path) -> dict[str, dict]:
"""Each setup's declared sources and facts, keyed by its file name."""
out: dict[str, dict] = {}
for path in sorted(Path(directory).glob("*.yml")):
with path.open(encoding="utf-8") as handle:
out[path.stem] = yaml_load(handle) or {}
out[path.stem] = source_yaml_file(path) or {}
return out