Coverage for src/romfarmer/config/recipe.py: 88%
34 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-05 02:19 -0700
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-05 02:19 -0700
1"""Recipe configuration — reusable processing templates.
3A Recipe declares HOW a category of platforms should be processed.
4It's the bridge between platform intrinsics (WHAT) and deployment
5targets (WHERE).
7Recipes are composable — a build can stack multiple recipes, with
8later recipes overriding earlier ones for overlapping platforms.
10Examples:
11 # All cartridge platforms get 7z compression
12 nointro-7z:
13 platforms: [nes, snes, gb, gba, ...]
14 compression: 7z
15 dat_filter: retool_1g1r_eng
17 # All disc platforms get CHD compression
18 redump-chd:
19 platforms: [psx, saturn, dreamcast, ...]
20 compression: chd
21 dat_filter: redump_retool_1g1r_eng
23 # Test recipe: stack on top to limit to 10 games
24 test-10:
25 selection:
26 strategy: random
27 limit: 10
28 seed: 42
29"""
31from typing import Any
33from pydantic import BaseModel, Field, field_validator
35from .models import (
36 CompressionFormat,
37 MetadataFilterConfig,
38 SelectionConfig,
39)
42class RecipeSpec(BaseModel):
43 """A reusable processing template for a category of platforms.
45 Recipes answer: "For these platforms, what processing choices do I want?"
47 Key design decisions:
48 - `platforms` is a list of platform names. Empty means "applies to all
49 platforms in the build" (useful for test recipes that limit game count).
50 - `compression` is a string (not enum) because the recipe doesn't validate
51 against platform capabilities — the resolver does that.
52 - `selection` is optional — when present, it stacks on top of whatever
53 the platform would otherwise get.
54 """
56 name: str = Field(description="Recipe identifier (e.g., 'nointro-7z', 'redump-chd')")
57 description: str = Field(
58 default="", description="Human-readable description of what this recipe does"
59 )
61 # Which platforms this recipe applies to
62 # Empty list = applies to all platforms in the build
63 platforms: list[str] = Field(
64 default_factory=list,
65 description="Platform names this recipe applies to. Empty = all platforms in build.",
66 )
68 # Processing choices
69 dat_filter: str | None = Field(
70 None,
71 description="DAT filter variant override (e.g., 'retool_1g1r_eng'). "
72 "None = use platform's default dat.source.",
73 )
74 compression: str | None = Field(
75 None,
76 description="Compression format: '7z', 'zip', 'chd', 'rvz', 'none', etc. "
77 "None = use platform/target default.",
78 )
80 # Selection/filtering
81 selection: SelectionConfig | None = Field(
82 None,
83 description="Selection filter to apply (e.g., limit to N games, rating budget). "
84 "Stacks on top of any existing selection.",
85 )
86 metadata_filter: MetadataFilterConfig | None = Field(
87 None,
88 description="Metadata-driven filter (genre, rating, players, nongames). "
89 "Keeps/drops ROMs based on scraped_games DB entries.",
90 )
92 # List application
93 apply_lists: bool | None = Field(
94 None,
95 description="Whether to apply keep/delete lists. "
96 "None = default (True for platforms with list_patterns).",
97 )
99 # Metadata generation
100 metadata: bool | None = Field(
101 None, description="Whether to generate metadata (gamelist.xml). None = use target default."
102 )
104 # Arcade-specific overrides
105 arcade_filter_overrides: dict[str, Any] | None = Field(
106 None, description="Override arcade filter settings from platform config"
107 )
109 # PS3 DLC override
110 ps3_dlc_enabled: bool | None = Field(
111 None, description="Override PS3 DLC processing. None = use platform default."
112 )
113 ps3_updates_enabled: bool | None = Field(
114 None, description="Override PS3 updates. None = use platform default."
115 )
117 @field_validator("compression")
118 @classmethod
119 def validate_compression(cls, v: str | None) -> str | None:
120 """Validate compression format if specified."""
121 if v is not None:
122 valid = {"none", "7z", "zip", "chd", "cso", "rvz", "xiso", "sqfs", "jb", "gzip"}
123 if v.lower() not in valid:
124 raise ValueError(
125 f"Invalid compression format '{v}'. Must be one of: {', '.join(sorted(valid))}"
126 )
127 return v.lower()
128 return v
130 def applies_to(self, platform_name: str) -> bool:
131 """Check if this recipe applies to a given platform.
133 Args:
134 platform_name: Platform identifier
136 Returns:
137 True if this recipe applies (empty platforms list = applies to all)
138 """
139 if not self.platforms:
140 return True
141 return platform_name in self.platforms
143 def get_effective_compression(self) -> CompressionFormat | None:
144 """Get the compression format as an enum, if specified.
146 Returns:
147 CompressionFormat enum or None
148 """
149 if self.compression is None:
150 return None
152 # Map string to enum
153 compression_map = {
154 "none": CompressionFormat.NONE,
155 "7z": CompressionFormat.SEVENZ,
156 "zip": CompressionFormat.ZIP,
157 "chd": CompressionFormat.CHD,
158 "cso": CompressionFormat.CSO,
159 "rvz": CompressionFormat.RVZ,
160 "xiso": CompressionFormat.XISO,
161 "sqfs": CompressionFormat.SQUASHFS,
162 "jb": CompressionFormat.JB,
163 "gzip": CompressionFormat.GZIP,
164 }
165 return compression_map.get(self.compression)