"""Profile lookup. Specific (build-pinned) profiles are tried before engine-wide ones.""" from __future__ import annotations from pathlib import Path from app.games.base import GameProfile _PROFILES: list[GameProfile] = [] def register(profile: GameProfile) -> GameProfile: if any(p.id == profile.id for p in _PROFILES): raise ValueError(f"Профиль уже зарегистрирован: {profile.id}") _PROFILES.append(profile) return profile def all_profiles() -> list[GameProfile]: _load() return list(_PROFILES) def select(engine: str, root: Path) -> GameProfile | None: """The profile that imports the game at ``root`` for a detected ``engine``.""" _load() candidates = [p for p in _PROFILES if p.engine == engine] for profile in sorted(candidates, key=lambda p: not p.specific): if profile.claims(root): return profile return None def for_runtime(runtime: str | None) -> GameProfile | None: _load() return next((p for p in _PROFILES if runtime and p.runtime == runtime), None) def for_id(profile_id: str | None) -> GameProfile | None: _load() return next((p for p in _PROFILES if p.id == profile_id), None) def _load() -> None: # Importing the package registers the built-in profiles. import app.games # noqa: F401