Coverage for src/romfarmer/config/frontend.py: 87%

46 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-05 02:19 -0700

1"""Frontend configuration models. 

2 

3Frontends represent software distributions like Batocera, RocknIX, RetroDeck, etc. 

4They define: 

5- Folder naming conventions (what the frontend calls each platform folder) 

6- Media support (what media types the frontend can display) 

7- Per-platform format support (what file extensions each emulator can open) 

8- Compression fallback rules 

9""" 

10 

11from enum import Enum 

12 

13from pydantic import BaseModel, Field 

14 

15 

16class MediaType(str, Enum): 

17 """Media types that frontends may support.""" 

18 

19 IMAGE = "image" # Box art, screenshots 

20 MIX = "mix" # Combined artwork (miximage) 

21 WHEEL = "wheel" # Logo wheels 

22 MARQUEE = "marquee" # Marquee images 

23 VIDEO = "video" # Video previews 

24 MANUAL = "manual" # PDF manuals 

25 CARTRIDGE = "cartridge" # 3D cartridge renders 

26 FANART = "fanart" # Fan artwork 

27 TITLESCREEN = "titlescreen" # Title screen captures 

28 BOXBACK = "boxback" # Back of box images 

29 

30 

31class FrontendPlatformConfig(BaseModel): 

32 """Per-platform configuration within a frontend. 

33 

34 Defines what file extensions the frontend's emulator for this platform 

35 can open, and the preferred compression format. 

36 """ 

37 

38 extensions: list[str] = Field( 

39 description="File extensions the emulator can open (e.g., ['.nes', '.zip', '.7z'])" 

40 ) 

41 preferred_compression: str = Field( 

42 default="none", 

43 description="Preferred compression format: none, zip, 7z, chd, cso, rvz, etc.", 

44 ) 

45 folder: str | None = Field( 

46 default=None, 

47 description="Override folder name for this platform (if different from folder_mapping)", 

48 ) 

49 

50 

51class FrontendDefaults(BaseModel): 

52 """Default settings for builds targeting this frontend.""" 

53 

54 organization: str = Field( 

55 default="flat", description="Default organization style: flat, balanced, minimal" 

56 ) 

57 metadata: bool = Field(default=True, description="Generate metadata (gamelist.xml) by default") 

58 

59 

60class FrontendConfig(BaseModel): 

61 """Configuration for a frontend (Batocera, RocknIX, etc.). 

62 

63 A frontend is a software distribution that runs on hardware devices. 

64 It defines how ROM files should be organized, what formats are supported, 

65 and what media types can be displayed. 

66 """ 

67 

68 name: str = Field(description="Frontend identifier (e.g., 'batocera', 'rocknix')") 

69 description: str | None = Field( 

70 default=None, description="Human-readable description of the frontend" 

71 ) 

72 

73 # Folder mapping: internal platform name → frontend folder name 

74 # Only list differences from internal names 

75 folder_mapping: dict[str, str] = Field( 

76 default_factory=dict, 

77 description="Map internal platform names to frontend folder names (e.g., megacd → segacd)", 

78 ) 

79 

80 # Media support: what media types this frontend can display 

81 media_support: list[MediaType] = Field( 

82 default_factory=lambda: [MediaType.IMAGE, MediaType.VIDEO], 

83 description="Media types this frontend can display", 

84 ) 

85 

86 # Per-platform format configuration 

87 # Key is platform name, value is FrontendPlatformConfig 

88 platforms: dict[str, FrontendPlatformConfig] = Field( 

89 default_factory=dict, 

90 description="Per-platform format configuration (extensions, compression)", 

91 ) 

92 

93 # Global compression fallback rules 

94 # If a requested format isn't supported, fall back to these 

95 compression_fallback: dict[str, str] = Field( 

96 default_factory=dict, description="Fallback compression formats (e.g., '7z' → 'zip')" 

97 ) 

98 

99 # Default settings for builds 

100 defaults: FrontendDefaults = Field( 

101 default_factory=FrontendDefaults, 

102 description="Default settings for builds targeting this frontend", 

103 ) 

104 

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

106 """Get the folder name for a platform on this frontend. 

107 

108 Args: 

109 platform: Internal platform name (e.g., 'megacd') 

110 

111 Returns: 

112 Frontend-specific folder name (e.g., 'segacd' for RocknIX) 

113 """ 

114 return self.folder_mapping.get(platform, platform) 

115 

116 def get_platform_config(self, platform: str) -> FrontendPlatformConfig | None: 

117 """Get the platform-specific configuration. 

118 

119 Args: 

120 platform: Internal platform name 

121 

122 Returns: 

123 FrontendPlatformConfig if defined, None otherwise 

124 """ 

125 return self.platforms.get(platform) 

126 

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

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

129 

130 Args: 

131 platform: Internal platform name 

132 default: Default compression if not specified 

133 

134 Returns: 

135 Preferred compression format string 

136 """ 

137 platform_config = self.get_platform_config(platform) 

138 if platform_config: 

139 return platform_config.preferred_compression 

140 return default 

141 

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

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

144 

145 Args: 

146 platform: Internal platform name 

147 

148 Returns: 

149 List of extensions if defined, None otherwise 

150 """ 

151 platform_config = self.get_platform_config(platform) 

152 if platform_config: 

153 return platform_config.extensions 

154 return None 

155 

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

157 """Check if this frontend supports a media type. 

158 

159 Args: 

160 media_type: The media type to check 

161 

162 Returns: 

163 True if supported, False otherwise 

164 """ 

165 return media_type in self.media_support 

166 

167 def apply_compression_fallback(self, requested: str) -> str: 

168 """Apply compression fallback if format not supported. 

169 

170 Args: 

171 requested: Requested compression format 

172 

173 Returns: 

174 Either the requested format or its fallback 

175 """ 

176 return self.compression_fallback.get(requested, requested)