mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-24 02:27:52 -06:00
refactor: one keyword-option guard in core
This commit is contained in:
1 parent
27330be60a
commit
b4573d310b
13 files changed
+42
-44
No files matched your search
@@ -0,0 +1,12 @@
|
||||
"""Keyword options a function takes through a TypedDict, checked as named parameters are."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
|
||||
|
||||
def reject_unknown(function: str, options: Mapping[str, object], known: frozenset[str]) -> None:
|
||||
"""Refuse a keyword the options do not declare, as a named parameter would."""
|
||||
for key in options:
|
||||
if key not in known:
|
||||
raise TypeError(f"{function}() got an unexpected keyword argument '{key}'")
|
||||
@@ -7,7 +7,7 @@ import os
|
||||
import shutil
|
||||
import tempfile
|
||||
import zipfile
|
||||
from collections.abc import Iterator, Mapping
|
||||
from collections.abc import Iterator
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
@@ -18,13 +18,6 @@ from core.paths import sanitize_pack_path
|
||||
from packs.paths import _has_path_conflict, _register_path, placement_block
|
||||
|
||||
|
||||
def _reject_unknown(function: str, options: Mapping[str, object], known: frozenset[str]) -> None:
|
||||
"""Refuse a keyword the options do not declare, as a named parameter would."""
|
||||
for key in options:
|
||||
if key not in known:
|
||||
raise TypeError(f"{function}() got an unexpected keyword argument '{key}'")
|
||||
|
||||
|
||||
# Formats that are already compressed: deflating them again took 28 s of the
|
||||
# RetroArch build for 2.1 GB of members and saved 9.6 MB (0.45 %), 16 GB and
|
||||
# about 72 MB across all packs. Measured per extension on the 2026-10-20
|
||||
|
||||
@@ -10,10 +10,11 @@ from pathlib import Path
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from core.hashing import compute_hashes
|
||||
from core.options import reject_unknown
|
||||
from model.declarations import declared_root
|
||||
from model.profiles import entry_system_ids, installed_data_refs
|
||||
from model.systems import _norm_system_id
|
||||
from packs.archive import PackRecord, _add_pack_member, _reject_unknown
|
||||
from packs.archive import PackRecord, _add_pack_member
|
||||
from packs.datadirs import cache_lock
|
||||
from packs.extras import prefixed_destination
|
||||
from packs.paths import MEDIA_MARKER, _has_path_conflict, _register_path, lands_on_android
|
||||
@@ -204,7 +205,7 @@ def _data_directory_members(
|
||||
``required_only`` leaves out a tree declared ``required: false``, one the
|
||||
core runs without, as it leaves out such a file.
|
||||
"""
|
||||
_reject_unknown("_data_directory_members", options, _TreeWalkOptions.__optional_keys__)
|
||||
reject_unknown("_data_directory_members", options, _TreeWalkOptions.__optional_keys__)
|
||||
refs_out = options.get("refs_out")
|
||||
reclaim = options.get("reclaim")
|
||||
required_only = options.get("required_only", False)
|
||||
|
||||
@@ -14,6 +14,7 @@ from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from core.cli import suggest_names
|
||||
from core.options import reject_unknown
|
||||
from core.paths import REPO_ROOT
|
||||
from core.ziptools import build_zip_contents_index
|
||||
from model.alternatives import settle_sets, slot_set_keys
|
||||
@@ -31,7 +32,6 @@ from packs.archive import (
|
||||
_Placed,
|
||||
_add_pack_member,
|
||||
_add_zip_to_pack,
|
||||
_reject_unknown,
|
||||
_same_bytes,
|
||||
_write_generated_member,
|
||||
_writing_pack,
|
||||
@@ -469,7 +469,7 @@ def generate_emulator_pack(
|
||||
`record` builds the pack in memory instead (PackRecord): nothing is
|
||||
written or read, and the paths are kept on it.
|
||||
"""
|
||||
_reject_unknown("generate_emulator_pack", options, EmulatorPackOptions.__optional_keys__)
|
||||
reject_unknown("generate_emulator_pack", options, EmulatorPackOptions.__optional_keys__)
|
||||
all_profiles = load_emulator_profiles(emulators_dir, skip_aliases=False)
|
||||
zip_contents = options.get("zip_contents")
|
||||
if zip_contents is None:
|
||||
@@ -558,7 +558,7 @@ def generate_system_pack(
|
||||
|
||||
Returns the path, "" when no file is needed, None on failure.
|
||||
"""
|
||||
_reject_unknown("generate_system_pack", options, SystemPackOptions.__optional_keys__)
|
||||
reject_unknown("generate_system_pack", options, SystemPackOptions.__optional_keys__)
|
||||
required_only = options.get("required_only", False)
|
||||
regions = options.get("regions")
|
||||
offline = options.get("offline")
|
||||
|
||||
@@ -6,10 +6,11 @@ this module builds so the report and the pack withdraw the same files."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable, Mapping
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from core.options import reject_unknown
|
||||
from model.profiles import PROFILE_IDENTITY_FIELDS, entry_system_ids
|
||||
from model.systems import _norm_system_id
|
||||
from model.alternatives import core_file_spares, required_only_spares, set_fields
|
||||
@@ -33,13 +34,6 @@ if TYPE_CHECKING:
|
||||
from typing_extensions import Unpack
|
||||
|
||||
|
||||
def reject_unknown(function: str, options: Mapping[str, object], known: frozenset[str]) -> None:
|
||||
"""Refuse a keyword the options do not declare, as a named parameter would."""
|
||||
for key in options:
|
||||
if key not in known:
|
||||
raise TypeError(f"{function}() got an unexpected keyword argument '{key}'")
|
||||
|
||||
|
||||
def _detect_extras_prefix(config: dict, base_dest: str) -> str:
|
||||
"""The prefix core-extra destinations take (may be empty).
|
||||
|
||||
|
||||
@@ -14,9 +14,10 @@ from pathlib import Path
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from core.artifacts import set_plain_mode
|
||||
from core.options import reject_unknown
|
||||
from model.registry import list_registered_platforms
|
||||
from model.targets import load_target_config
|
||||
from packs.archive import _reject_unknown, _write_generated_member
|
||||
from packs.archive import _write_generated_member
|
||||
from packs.integrity import generate_sha256sums, verify_pack, verify_pack_against_platform
|
||||
from packs.naming import _expected_pack_names, _narrows_contents, _pack_archives
|
||||
from packs.scope import annotate_pack_systems
|
||||
@@ -182,7 +183,7 @@ def verify_and_finalize_packs(
|
||||
|
||||
Returns True if all packs pass verification.
|
||||
"""
|
||||
_reject_unknown("verify_and_finalize_packs", options, FinalizeOptions.__optional_keys__)
|
||||
reject_unknown("verify_and_finalize_packs", options, FinalizeOptions.__optional_keys__)
|
||||
opts = _FinalizeSettings(**options)
|
||||
all_ok = True
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import zlib
|
||||
import sys
|
||||
from resolve.cores import filter_systems_by_target
|
||||
from core.hashing import md5_composite_of, parse_md5_list
|
||||
from core.options import reject_unknown
|
||||
import io
|
||||
from model.profiles import load_emulator_profiles
|
||||
from model.platforms import load_platform_config
|
||||
@@ -39,7 +40,7 @@ from resolve.gamedata import without_game_data
|
||||
from resolve.cores import resolve_platform_cores
|
||||
from core.paths import sanitize_pack_path
|
||||
import zipfile
|
||||
from collections.abc import Callable, Mapping
|
||||
from collections.abc import Callable
|
||||
|
||||
# Imported at run time where typing has it: a test reads the signature of
|
||||
# verify_pack_against_platform with its annotations evaluated.
|
||||
@@ -739,13 +740,6 @@ class _BaselineCount:
|
||||
decl_by_member: dict[str, list[dict]] = field(default_factory=dict)
|
||||
|
||||
|
||||
def _reject_unknown(function: str, options: Mapping[str, object], known: frozenset[str]) -> None:
|
||||
"""Refuse a keyword the options do not declare, as a named parameter would."""
|
||||
for key in options:
|
||||
if key not in known:
|
||||
raise TypeError(f"{function}() got an unexpected keyword argument '{key}'")
|
||||
|
||||
|
||||
def _cut_config(
|
||||
platform_name: str,
|
||||
platforms_dir: str,
|
||||
@@ -1044,7 +1038,7 @@ def verify_pack_against_platform(
|
||||
holds no file for that are a game's data or that no core the platform
|
||||
runs opens. verify.py counts them apart from firmware.
|
||||
"""
|
||||
_reject_unknown(
|
||||
reject_unknown(
|
||||
"verify_pack_against_platform", options, PlatformCheckOptions.__optional_keys__
|
||||
)
|
||||
opts = _PlatformCheckSettings(**options)
|
||||
|
||||
@@ -10,6 +10,7 @@ from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from core.artifacts import _build_timestamp
|
||||
from core.options import reject_unknown
|
||||
from core.paths import REPO_ROOT, sanitize_pack_path
|
||||
from model.declarations import OUTER_ROOTS, declared_root, outer_layout
|
||||
from model.platforms import load_platform_config
|
||||
@@ -18,7 +19,7 @@ from model.registry import load_data_dir_registry
|
||||
from model.safeparse import yaml_load
|
||||
from model.systems import platform_system_names
|
||||
from packs import slots
|
||||
from packs.archive import CASE_FOLDING_OS, PACK_DOCUMENTS, _Placed, _reject_unknown
|
||||
from packs.archive import CASE_FOLDING_OS, PACK_DOCUMENTS, _Placed
|
||||
from packs.datatrees import (
|
||||
_data_directory_members,
|
||||
_in_data_cache,
|
||||
@@ -709,7 +710,7 @@ def generate_manifest(
|
||||
--standalone read. `build_out` receives the slot choices and the
|
||||
profiles this build read, which that second build takes over.
|
||||
"""
|
||||
_reject_unknown("generate_manifest", options, ManifestOptions.__optional_keys__)
|
||||
reject_unknown("generate_manifest", options, ManifestOptions.__optional_keys__)
|
||||
opts = _ManifestSettings(**options)
|
||||
build, full_profiles = _start_manifest(
|
||||
platform_name, platforms_dir, db, bios_dir, registry_path, opts
|
||||
|
||||
@@ -6,12 +6,12 @@ from collections.abc import Callable
|
||||
from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from core.options import reject_unknown
|
||||
from core.paths import sanitize_pack_path
|
||||
from model.alternatives import core_file_spares
|
||||
from model.declarations import OUTER_ROOTS, ROMS_ROOT, declared_root
|
||||
from model.profiles import upstream_profile_index
|
||||
from model.targets import list_available_targets, load_target_config
|
||||
from packs.archive import _reject_unknown
|
||||
from packs.extras import (
|
||||
ExtraResolver,
|
||||
_detect_extras_prefix,
|
||||
@@ -235,7 +235,7 @@ def _manifest_core_entries(
|
||||
upstream hash carried by no local file leaves the entry with no URL.
|
||||
Returns the bytes added.
|
||||
"""
|
||||
_reject_unknown(
|
||||
reject_unknown(
|
||||
"_manifest_core_entries", state,
|
||||
_CoreEntryParts.__required_keys__ | _CoreEntryParts.__optional_keys__,
|
||||
)
|
||||
@@ -290,7 +290,7 @@ def _manifest_selection(
|
||||
decides on the full manifest withdraws what `--region` and
|
||||
`--one-per-slot` withdraw from the pack.
|
||||
"""
|
||||
_reject_unknown("_manifest_selection", options, _SelectionOptions.__optional_keys__)
|
||||
reject_unknown("_manifest_selection", options, _SelectionOptions.__optional_keys__)
|
||||
required_only = options.get("required_only", False)
|
||||
regions = options.get("regions")
|
||||
one_per_slot = options.get("one_per_slot", False)
|
||||
|
||||
@@ -8,9 +8,10 @@ import sys
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from core.options import reject_unknown
|
||||
from model.platforms import load_platform_config
|
||||
from model.profiles import ProfileSelectionError, load_emulator_profiles, select_emulator_profiles
|
||||
from packs.archive import _add_pack_member, _reject_unknown, _writing_pack
|
||||
from packs.archive import _add_pack_member, _writing_pack
|
||||
from packs.naming import _name_part, _narrowings
|
||||
from packs.paths import _resolve_destination
|
||||
from resolve.packfiles import resolve_file, sha1_for_hash
|
||||
@@ -188,7 +189,7 @@ def generate_md5_pack(
|
||||
files_db = db.get("files", {})
|
||||
if zip_contents is None:
|
||||
zip_contents = {}
|
||||
_reject_unknown("generate_md5_pack", options, Md5PackOptions.__optional_keys__)
|
||||
reject_unknown("generate_md5_pack", options, Md5PackOptions.__optional_keys__)
|
||||
opts = _Md5Settings(**options)
|
||||
layout = _md5_layout(opts)
|
||||
if layout is None:
|
||||
|
||||
@@ -13,6 +13,7 @@ from pathlib import Path
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from core.hashing import compute_hashes
|
||||
from core.options import reject_unknown
|
||||
from core.paths import sanitize_pack_path
|
||||
from model.alternatives import core_file_spares
|
||||
from model.declarations import OUTER_ROOTS, declared_root
|
||||
@@ -27,7 +28,6 @@ from packs.archive import (
|
||||
_add_pack_member,
|
||||
_add_zip_to_pack,
|
||||
_extract_zip_to_archive,
|
||||
_reject_unknown,
|
||||
_same_bytes,
|
||||
_write_generated_member,
|
||||
_writing_pack,
|
||||
@@ -161,7 +161,7 @@ def _select_variants(
|
||||
Returns the destinations to drop, the groups kept only as a
|
||||
fallback, and the slots no profile puts in order.
|
||||
"""
|
||||
_reject_unknown("_select_variants", options, _VariantOptions.__optional_keys__)
|
||||
reject_unknown("_select_variants", options, _VariantOptions.__optional_keys__)
|
||||
regions = options.get("regions")
|
||||
one_per_slot = options.get("one_per_slot", False)
|
||||
region_drops: set[str] = set()
|
||||
@@ -1097,7 +1097,7 @@ def generate_pack(
|
||||
|
||||
Returns the path to the generated ZIP, or None on failure.
|
||||
"""
|
||||
_reject_unknown("generate_pack", options, PackOptions.__optional_keys__)
|
||||
reject_unknown("generate_pack", options, PackOptions.__optional_keys__)
|
||||
opts = _PackSettings(**options)
|
||||
plan = _plan_platform_pack(platform_name, platforms_dir, db, bios_dir, output_dir, opts)
|
||||
if plan is None:
|
||||
@@ -1208,7 +1208,7 @@ def generate_split_packs(
|
||||
own placement (``within``). ``records`` builds the parts in memory, keyed
|
||||
by part name, with no archive written.
|
||||
"""
|
||||
_reject_unknown("generate_split_packs", options, SplitOptions.__optional_keys__)
|
||||
reject_unknown("generate_split_packs", options, SplitOptions.__optional_keys__)
|
||||
config = load_platform_config(platform_name, platforms_dir)
|
||||
records = options.get("records")
|
||||
narrowing: PackNarrowing = {
|
||||
|
||||
@@ -7,7 +7,7 @@ from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
|
||||
from packs.extras import reject_unknown
|
||||
from core.options import reject_unknown
|
||||
from model.region import label as region_label
|
||||
from siteguides import INSTALL_PS1, INSTALL_SH, pack_manual
|
||||
|
||||
|
||||
Reference in new issue
Block a user