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

1"""Target configuration models. 

2 

3Targets compose a Frontend and Device together. They represent a specific 

4deployment scenario like "RocknIX on R36S" or "Batocera on Steam Deck". 

5 

6Targets can also define overrides for specific combinations. 

7""" 

8 

9from typing import Any 

10 

11from pydantic import BaseModel, Field 

12 

13from .device import DeviceConfig 

14from .frontend import FrontendConfig, MediaType 

15 

16 

17class TargetOverrides(BaseModel): 

18 """Target-specific overrides that modify frontend/device behavior.""" 

19 

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 ) 

25 

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 ) 

30 

31 

32class TargetConfig(BaseModel): 

33 """Configuration for a target (frontend + device combination). 

34 

35 A target represents a specific deployment scenario. It references 

36 a frontend (software) and device (hardware) and can provide 

37 target-specific overrides. 

38 

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 """ 

44 

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 ) 

49 

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)") 

53 

54 # Target-specific overrides 

55 overrides: TargetOverrides = Field( 

56 default_factory=TargetOverrides, description="Target-specific overrides" 

57 ) 

58 

59 

60class ComposedTarget(BaseModel): 

61 """A fully resolved target with frontend and device configs loaded. 

62 

63 This is the runtime representation of a target after all configs 

64 have been loaded and composed together. 

65 """ 

66 

67 name: str = Field(description="Target name") 

68 description: str | None = Field(default=None) 

69 

70 # Loaded configurations 

71 frontend: FrontendConfig = Field(description="Loaded frontend configuration") 

72 device: DeviceConfig = Field(description="Loaded device configuration") 

73 

74 # Original target config for reference 

75 target_config: TargetConfig = Field(description="Original target configuration") 

76 

77 def supports_platform(self, platform: str) -> bool: 

78 """Check if this target supports a platform. 

79 

80 Platform must be supported by BOTH frontend (has config) and device 

81 (not in unsupported list). 

82 

83 Args: 

84 platform: Platform name to check 

85 

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 

92 

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 

98 

99 # Frontend support is more flexible - if no platform config, we assume 

100 # it's supported with default settings 

101 return True 

102 

103 def get_folder_name(self, platform: str) -> str: 

104 """Get the folder name for a platform on this target. 

105 

106 Args: 

107 platform: Internal platform name 

108 

109 Returns: 

110 Target-specific folder name 

111 """ 

112 return self.frontend.get_folder_name(platform) 

113 

114 def get_preferred_compression(self, platform: str, default: str = "none") -> str: 

115 """Get the preferred compression format for a platform. 

116 

117 Args: 

118 platform: Internal platform name 

119 default: Default if not specified 

120 

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) 

127 

128 def get_extensions(self, platform: str) -> list[str] | None: 

129 """Get supported file extensions for a platform. 

130 

131 Args: 

132 platform: Internal platform name 

133 

134 Returns: 

135 List of supported extensions, or None if not specified 

136 """ 

137 return self.frontend.get_extensions(platform) 

138 

139 def supports_media(self, media_type: MediaType) -> bool: 

140 """Check if this target supports a media type. 

141 

142 Args: 

143 media_type: The media type to check 

144 

145 Returns: 

146 True if frontend supports this media type 

147 """ 

148 return self.frontend.supports_media(media_type) 

149 

150 def get_unsupported_platforms(self) -> list[str]: 

151 """Get all platforms that are not supported by this target. 

152 

153 Combines device unsupported + target-specific additional unsupported. 

154 

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) 

161 

162 def get_optimal_image_size(self): 

163 """Get optimal image size based on device display. 

164 

165 Returns: 

166 Tuple of (width, height) 

167 """ 

168 return self.device.get_optimal_image_size() 

169 

170 def generate_output_folder_name(self, storage_budget: str, profile: str) -> str: 

171 """Generate the output folder name for this target. 

172 

173 Format: {frontend}-{device}-{storage}-{profile} 

174 

175 Args: 

176 storage_budget: Storage budget string (e.g., '512gb', 'unlimited') 

177 profile: Build profile (e.g., 'complete', 'test-10games') 

178 

179 Returns: 

180 Output folder name 

181 """ 

182 return f"{self.frontend.name}-{self.device.name}-{storage_budget}-{profile}"