Coverage for src/romfarmer/config/device.py: 91%

45 statements  

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

1"""Device configuration models. 

2 

3Devices represent hardware like R36S, Steam Deck, PC, etc. 

4They define: 

5- Display resolution and aspect ratio 

6- Media sizing constraints (to avoid wasting storage on oversized images) 

7- Unsupported platforms (systems the device's CPU can't emulate) 

8""" 

9 

10from typing import Literal 

11 

12from pydantic import BaseModel, Field, field_validator 

13 

14 

15class DisplayConfig(BaseModel): 

16 """Display configuration for a device.""" 

17 

18 resolution: tuple[int, int] = Field( 

19 default=(1920, 1080), description="Display resolution as (width, height)" 

20 ) 

21 aspect_ratio: str = Field( 

22 default="16:9", description="Display aspect ratio (e.g., '4:3', '16:9', '16:10')" 

23 ) 

24 

25 @field_validator("resolution", mode="before") 

26 @classmethod 

27 def validate_resolution(cls, v): 

28 """Accept list or tuple for resolution.""" 

29 if isinstance(v, list): 

30 return tuple(v) 

31 return v 

32 

33 

34class MediaSizingConfig(BaseModel): 

35 """Media sizing constraints to optimize storage. 

36 

37 Images and videos larger than these limits are candidates for 

38 downscaling/transcoding to save storage space. 

39 """ 

40 

41 max_image_width: int = Field(default=1024, description="Maximum image width in pixels", gt=0) 

42 max_image_height: int = Field(default=768, description="Maximum image height in pixels", gt=0) 

43 video_max_resolution: str = Field( 

44 default="720p", description="Maximum video resolution (480p, 720p, 1080p)" 

45 ) 

46 video_max_bitrate_kbps: int = Field( 

47 default=3000, description="Maximum video bitrate in kbps", gt=0 

48 ) 

49 

50 

51class DeviceStorage(BaseModel): 

52 """Storage facts the intent layer budgets against.""" 

53 

54 reserve_bytes: int | None = Field( 

55 default=None, 

56 description="Bytes to hold back for OS partition, BIOS, media, saves. Spec may override.", 

57 ge=0, 

58 ) 

59 

60 

61class DevicePlatformCapability(BaseModel): 

62 """How well this device runs one platform (intent brief v4 §1 / strawman tiers). 

63 

64 tier: A = full speed, take the whole curated set; B = most runs well; 

65 C = selective — only titles on ``playable_list``; X = do not ship. 

66 quality: multiplier on a game's rating when scoring quality-per-byte. 

67 """ 

68 

69 tier: Literal["A", "B", "C", "X"] = "A" 

70 quality: float = Field(default=1.0, ge=0.0, le=1.0) 

71 playable_list: str | None = Field( 

72 default=None, description="curated/<name>@sha256:<hex> — required for tier C" 

73 ) 

74 notes: str = "" 

75 

76 

77class DeviceConfig(BaseModel): 

78 """Configuration for a device (R36S, Steam Deck, PC, etc.). 

79 

80 A device is hardware that runs a frontend. It defines hardware 

81 constraints like display resolution and CPU capability limits. 

82 

83 The `unsupported_platforms` list should be populated through testing - 

84 start empty and add platforms as you discover they don't run well 

85 on the device. 

86 """ 

87 

88 name: str = Field(description="Device identifier (e.g., 'r36s', 'steamdeck', 'pc')") 

89 description: str | None = Field( 

90 default=None, description="Human-readable description of the device" 

91 ) 

92 

93 # Display configuration 

94 display: DisplayConfig = Field( 

95 default_factory=DisplayConfig, 

96 description="Display configuration (resolution, aspect ratio)", 

97 ) 

98 

99 # Media sizing constraints 

100 media_sizing: MediaSizingConfig = Field( 

101 default_factory=MediaSizingConfig, description="Media sizing constraints for optimization" 

102 ) 

103 

104 # Platforms this device CANNOT run (discovered through testing) 

105 # These are CPU/performance limitations, not frontend support 

106 # Capability facts (data, not model knowledge — the agent READS these) 

107 storage: DeviceStorage = Field(default_factory=DeviceStorage) 

108 platforms: dict[str, DevicePlatformCapability] = Field( 

109 default_factory=dict, description="Per-platform capability tier / quality" 

110 ) 

111 platforms_default: DevicePlatformCapability | None = Field( 

112 default=None, 

113 description="Capability assumed for platforms not listed (pc: tier A). " 

114 "When None, an unlisted platform is an ERROR for the intent layer, not a guess.", 

115 ) 

116 

117 unsupported_platforms: list[str] = Field( 

118 default_factory=list, 

119 description="Platforms this device cannot emulate (e.g., ['ps2', 'ps3', 'gamecube'])", 

120 ) 

121 

122 @field_validator("unsupported_platforms", mode="before") 

123 @classmethod 

124 def validate_unsupported_platforms(cls, v): 

125 """Handle YAML files that have the key with only comments (returns None).""" 

126 if v is None: 

127 return [] 

128 return v 

129 

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

131 """Check if this device can run a platform. 

132 

133 Args: 

134 platform: Platform name to check 

135 

136 Returns: 

137 True if supported (not in unsupported list), False otherwise 

138 """ 

139 cap = self.platforms.get(platform) 

140 if cap is not None and cap.tier == "X": 

141 return False 

142 return platform.lower() not in [p.lower() for p in self.unsupported_platforms] 

143 

144 def get_optimal_image_size(self) -> tuple[int, int]: 

145 """Get the optimal image size for this device. 

146 

147 Returns: 

148 Tuple of (width, height) for optimal images 

149 """ 

150 return (self.media_sizing.max_image_width, self.media_sizing.max_image_height)