diff --git a/scripts/model/platforms.py b/scripts/model/platforms.py index 81f3b523a..a0771c887 100644 --- a/scripts/model/platforms.py +++ b/scripts/model/platforms.py @@ -152,6 +152,11 @@ def _mark_composite_archives(config: dict) -> None: } +# The fields _tag_platform_systems adds to each entry, which the platform's +# own list does not carry. +ENTRY_TAGS = ("platform_system", "platform_cores") + + def _tag_platform_systems(config: dict) -> None: """Each entry carries the system that declares it. @@ -198,6 +203,35 @@ def _attach_rom_folders(config: dict, platform_name: str, platforms_dir: str) -> config["_rom_folders"] = [row for row in folders if isinstance(row, dict)] +def _registry_entries(chain: list[str], platforms_dir: str) -> list[dict]: + """The registry entry of each platform in the chain, nearest first. + + The platform's own entry first, then its parents' for what it does not + say: RetroPie's scraper and install are its own, its verification mode + is RetroArch's. + """ + registry_path = os.path.join(platforms_dir, "_registry.yml") + if not os.path.exists(registry_path): + return [] + platforms = _cached_platform_yaml(registry_path).get("platforms", {}) + return [platforms.get(name) or {} for name in chain] + + +def registry_fields(config: dict) -> dict: + """The registry fields load_platform_config merged into a config, with + the values the config holds. + + Empty for a config built by hand, which names no registry key. + """ + platform_name = config.get("_key") + if not platform_name: + return {} + platforms_dir = config_platforms_dir(config) + chain = inheritance_chain(platform_name, platforms_dir) + merged = {key for entry in _registry_entries(chain, platforms_dir) for key in entry} + return {key: value for key, value in config.items() if key in merged} + + def load_platform_config(platform_name: str, platforms_dir: str = PLATFORMS_DIR) -> dict: """Load a platform config with inheritance and shared group resolution. @@ -214,22 +248,15 @@ def load_platform_config(platform_name: str, platforms_dir: str = PLATFORMS_DIR) # Merge metadata from _registry.yml. The registry is our curated source, # while the scraped YAML may be incomplete (missing cores, metadata fields). # Registry fields supplement (not replace) the scraped config. - registry_path = os.path.join(platforms_dir, "_registry.yml") - if os.path.exists(registry_path): - reg = _cached_platform_yaml(registry_path) - # The platform's own entry first, then its parents' for what it does - # not say: RetroPie's status, scraper and install are its own, its - # verification mode is RetroArch's. - for depth, name in enumerate(chain): - reg_entry = reg.get("platforms", {}).get(name, {}) - _merge_registry_cores(config, reg_entry, depth) + for depth, reg_entry in enumerate(_registry_entries(chain, platforms_dir)): + _merge_registry_cores(config, reg_entry, depth) - # Merge all registry fields absent from config (except cores, - # handled above with union logic). No hardcoded list: any field - # added to the registry is automatically available in the config. - for key, val in reg_entry.items(): - if key != "cores" and key not in config: - config[key] = val + # Merge all registry fields absent from config (except cores, + # handled above with union logic). No hardcoded list: any field + # added to the registry is automatically available in the config. + for key, val in reg_entry.items(): + if key != "cores" and key not in config: + config[key] = val if os.path.exists(shared_path): shared = _cached_platform_yaml(shared_path) diff --git a/scripts/sitegen/dataexports.py b/scripts/sitegen/dataexports.py index 917fa5234..a2a9baf1b 100644 --- a/scripts/sitegen/dataexports.py +++ b/scripts/sitegen/dataexports.py @@ -18,7 +18,7 @@ from core.artifacts import json_text as _json_text from core.hashing import file_digest from model import retired as model_retired from model.database import sha1_at -from model.platforms import PLATFORMS_DIR_KEY +from model.platforms import ENTRY_TAGS, registry_fields from model.profiles import is_emulator from packs.consumers import FileConsumers from sitegen.apiitems import core_index_items, system_index_items @@ -136,8 +136,6 @@ def _split_contract(config: dict) -> tuple[dict, dict]: """ allowed = _contract_keys() contract = {key: value for key, value in config.items() if key in allowed} - # platform_system and platform_cores are the resolver's copies of the key - # each entry sits under and of that system's cores. if isinstance(contract.get("systems"), dict): contract["systems"] = { sys_id: { @@ -146,7 +144,7 @@ def _split_contract(config: dict) -> tuple[dict, dict]: { k: v for k, v in f.items() - if k not in ("platform_system", "platform_cores") + if k not in ENTRY_TAGS } for f in system["files"] ], @@ -155,8 +153,8 @@ def _split_contract(config: dict) -> tuple[dict, dict]: else system for sys_id, system in contract["systems"].items() } - registry = {key: value for key, value in config.items() - if key not in allowed and key not in ("_key", PLATFORMS_DIR_KEY)} + registry = {key: value for key, value in registry_fields(config).items() + if key not in allowed} # The merged config gives a child its parent's release_pack: Lakka and # RetroPie said true in the API while every page said they had no ZIP. # The registry's own entry decides, as it does for the pages. diff --git a/tests/test_site_reference.py b/tests/test_site_reference.py index 7f4664eff..0962366dc 100644 --- a/tests/test_site_reference.py +++ b/tests/test_site_reference.py @@ -656,7 +656,7 @@ class TheContractsValidate(unittest.TestCase): self.skipTest("jsonschema is a tooling dependency") from model.registry import list_registered_platforms - from model.platforms import PLATFORMS_DIR_KEY, load_platform_config + from model.platforms import PLATFORMS_DIR_KEY, load_platform_config, registry_fields schema = json.loads((REPO_ROOT / "schemas" / "platform.schema.json").read_text()) validator = Draft202012Validator(schema) @@ -671,9 +671,11 @@ class TheContractsValidate(unittest.TestCase): self.assertNotIn("_key", registry) # The directory a run loaded the config from is no published field. self.assertNotIn(PLATFORMS_DIR_KEY, registry) - unpublished = {"_key", PLATFORMS_DIR_KEY} - self.assertEqual(set(contract) | set(registry) | unpublished, - set(config) | unpublished) + # The registry is what the loader merged from _registry.yml, + # never a key it adds for its own readers (_roms_root). + self.assertEqual(set(registry), set(registry_fields(config)) - set(contract)) + self.assertEqual([key for key in registry if key.startswith("_")], []) + self.assertLessEqual(set(contract) | set(registry), set(config)) def test_every_schema_id_is_the_address_it_is_served_at(self): for path in sorted((REPO_ROOT / "schemas").glob("*.schema.json")):