mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-23 18:17:53 -06:00
refactor: read the declarations once per database
collect_all_aliases opened platforms/ and emulators/ from paths fixed to the working directory while declarations took them as parameters, so a build walked every YAML twice. read_declarations now reads both once, reports unreadable sources, and hands the alias pass and the proven-name index their entries. generate_db gains --platforms-dir and --emulators-dir and refuses a missing one.
This commit is contained in:
1 parent
a30984912c
commit
49dbb2828d
5 files changed
+174
-228
No files matched your search
@@ -2,8 +2,10 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
from core.cli import SourceUnreadable
|
||||
from model.profiles import source_yaml_paths
|
||||
from model.registry import list_registered_platforms
|
||||
from model.sources import source_yaml_file
|
||||
@@ -17,18 +19,59 @@ def hash_values(value: object) -> list[str]:
|
||||
return [str(v).strip().lower() for v in values if str(v).strip()]
|
||||
|
||||
|
||||
def declarations(platforms_dir: Path, emulators_dir: Path) -> list[dict]:
|
||||
"""Every file entry the registered platforms and the profiles declare."""
|
||||
sources = [
|
||||
platforms_dir / f"{name}.yml"
|
||||
for name in list_registered_platforms(str(platforms_dir))
|
||||
] if platforms_dir.is_dir() else []
|
||||
entries: list[dict] = []
|
||||
for source in sources:
|
||||
config = source_yaml_file(source) or {}
|
||||
for system in (config.get("systems") or {}).values():
|
||||
entries.extend(system.get("files") or [])
|
||||
class UnreadableSource(Exception):
|
||||
"""Platform or profile files that do not parse, named one per line."""
|
||||
|
||||
def __init__(self, sources: list[str]) -> None:
|
||||
super().__init__("\n".join(sources))
|
||||
self.sources = sources
|
||||
|
||||
|
||||
@dataclass
|
||||
class Declarations:
|
||||
"""The file entries of every registered platform, and of each profile apart."""
|
||||
|
||||
platform_entries: list[dict] = field(default_factory=list)
|
||||
# One list per profile: an entry's aliases are judged against its own profile.
|
||||
profile_entries: list[list[dict]] = field(default_factory=list)
|
||||
|
||||
def entries(self) -> list[dict]:
|
||||
"""Every declared file entry, platforms first."""
|
||||
return self.platform_entries + [
|
||||
entry for entries in self.profile_entries for entry in entries
|
||||
]
|
||||
|
||||
|
||||
def _read_source(path: Path, unreadable: list[str]) -> dict | None:
|
||||
"""One platform or profile file, None and its name recorded when it does not read."""
|
||||
try:
|
||||
return source_yaml_file(path) or {}
|
||||
except SourceUnreadable as e:
|
||||
unreadable.append(str(e))
|
||||
except OSError as e:
|
||||
unreadable.append(f"{path}: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def read_declarations(platforms_dir: Path, emulators_dir: Path) -> Declarations:
|
||||
"""Every file entry the registered platforms and the profiles declare.
|
||||
|
||||
A source that does not parse is reported and the run refused: written
|
||||
without it, the database lost every alias the file proves and the
|
||||
command exited 0 (dosbox-x's wqy fonts answered not_found).
|
||||
"""
|
||||
declared = Declarations()
|
||||
unreadable: list[str] = []
|
||||
if platforms_dir.is_dir():
|
||||
for name in list_registered_platforms(str(platforms_dir)):
|
||||
config = _read_source(platforms_dir / f"{name}.yml", unreadable)
|
||||
for system in ((config or {}).get("systems") or {}).values():
|
||||
declared.platform_entries.extend(system.get("files") or [])
|
||||
if emulators_dir.is_dir():
|
||||
for source in source_yaml_paths(emulators_dir):
|
||||
entries.extend((source_yaml_file(source) or {}).get("files") or [])
|
||||
return entries
|
||||
profile = _read_source(source, unreadable)
|
||||
if profile is not None:
|
||||
declared.profile_entries.append(profile.get("files") or [])
|
||||
if unreadable:
|
||||
raise UnreadableSource(unreadable)
|
||||
return declared
|
||||
Reference in new issue
Block a user