Coverage for src/romfarmer/config/target.py: 79%
42 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"""Target configuration models.
3Targets compose a Frontend and Device together. They represent a specific
4deployment scenario like "RocknIX on R36S" or "Batocera on Steam Deck".
6Targets can also define overrides for specific combinations.
7"""
9from typing import Any
11from pydantic import BaseModel, Field
13from .device import DeviceConfig
14from .frontend import FrontendConfig, MediaType
17class TargetOverrides(BaseModel):
18 """Target-specific overrides that modify frontend/device behavior."""
20 # Platform-specific overrides at the target level
21 platforms: dict[str, dict[str, Any]] = Field(
22 default_factory=dict,
23 description="Per-platform overrides (e.g., disable a platform for this target)",
24 )
26 # Additional unsupported platforms beyond device limits
27 additional_unsupported: list[str] = Field(
28 default_factory=list, description="Additional platforms to exclude for this specific target"
29 )
32class TargetConfig(BaseModel):
33 """Configuration for a target (frontend + device combination).
35 A target represents a specific deployment scenario. It references
36 a frontend (software) and device (hardware) and can provide
37 target-specific overrides.
39 Examples:
40 - rocknix-r36s: RocknIX frontend on R36S handheld
41 - batocera-pc: Batocera frontend on desktop PC
42 - batocera-steamdeck: Batocera frontend on Steam Deck
43 """
45 name: str = Field(description="Target identifier (e.g., 'rocknix-r36s')")
46 description: str | None = Field(
47 default=None, description="Human-readable description of the target"
48 )
50 # References to frontend and device configs
51 frontend: str = Field(description="Frontend name (must match a frontend config file)")
52 device: str = Field(description="Device name (must match a device config file)")
54 # Target-specific overrides
55 overrides: TargetOverrides = Field(
56 default_factory=TargetOverrides, description="Target-specific overrides"
57 )
60class ComposedTarget(BaseModel):
61 """A fully resolved target with frontend and device configs loaded.
63 This is the runtime representation of a target after all configs
64 have been loaded and composed together.
65 """
67 name: str = Field(description="Target name")
68 description: str | None = Field(default=None)
70 # Loaded configurations
71 frontend: FrontendConfig = Field(description="Loaded frontend configuration")
72 device: DeviceConfig = Field(description="Loaded device configuration")
74 # Original target config for reference
75 target_config: TargetConfig = Field(description="Original target configuration")
77 def supports_platform(self, platform: str) -> bool:
78 """Check if this target supports a platform.
80 Platform must be supported by BOTH frontend (has config) and device
81 (not in unsupported list).
83 Args:
84 platform: Platform name to check
86 Returns:
87 True if both frontend and device support the platform
88 """
89 # Check device support
90 if not self.device.supports_platform(platform):
91 return False
93 # Check target-specific exclusions
94 if platform.lower() in [
95 p.lower() for p in self.target_config.overrides.additional_unsupported
96 ]:
97 return False
99 # Frontend support is more flexible - if no platform config, we assume
100 # it's supported with default settings
101 return True
103 def get_folder_name(self, platform: str) -> str:
104 """Get the folder name for a platform on this target.
106 Args:
107 platform: Internal platform name
109 Returns:
110 Target-specific folder name
111 """
112 return self.frontend.get_folder_name(platform)
114 def get_preferred_compression(self, platform: str, default: str = "none") -> str:
115 """Get the preferred compression format for a platform.
117 Args:
118 platform: Internal platform name
119 default: Default if not specified
121 Returns:
122 Preferred compression format
123 """
124 compression = self.frontend.get_preferred_compression(platform, default)
125 # Apply fallback if needed
126 return self.frontend.apply_compression_fallback(compression)
128 def get_extensions(self, platform: str) -> list[str] | None:
129 """Get supported file extensions for a platform.
131 Args:
132 platform: Internal platform name
134 Returns:
135 List of supported extensions, or None if not specified
136 """
137 return self.frontend.get_extensions(platform)
139 def supports_media(self, media_type: MediaType) -> bool:
140 """Check if this target supports a media type.
142 Args:
143 media_type: The media type to check
145 Returns:
146 True if frontend supports this media type
147 """
148 return self.frontend.supports_media(media_type)
150 def get_unsupported_platforms(self) -> list[str]:
151 """Get all platforms that are not supported by this target.
153 Combines device unsupported + target-specific additional unsupported.
155 Returns:
156 List of unsupported platform names
157 """
158 unsupported = set(self.device.unsupported_platforms)
159 unsupported.update(self.target_config.overrides.additional_unsupported)
160 return sorted(unsupported)
162 def get_optimal_image_size(self):
163 """Get optimal image size based on device display.
165 Returns:
166 Tuple of (width, height)
167 """
168 return self.device.get_optimal_image_size()
170 def generate_output_folder_name(self, storage_budget: str, profile: str) -> str:
171 """Generate the output folder name for this target.
173 Format: {frontend}-{device}-{storage}-{profile}
175 Args:
176 storage_budget: Storage budget string (e.g., '512gb', 'unlimited')
177 profile: Build profile (e.g., 'complete', 'test-10games')
179 Returns:
180 Output folder name
181 """
182 return f"{self.frontend.name}-{self.device.name}-{storage_budget}-{profile}"