Coverage for src/romfarmer/core/config.py: 64%

64 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-01 23:08 -0700

1"""Configuration management with YAML support and profile system.""" 

2 

3from pathlib import Path 

4 

5import yaml 

6from pydantic import BaseModel, Field, field_validator 

7 

8 

9class OrganizationProfile(BaseModel): 

10 """Configuration profile for ROM organization.""" 

11 

12 name: str = Field(description="Profile name") 

13 regions: list[str] = Field(default_factory=lambda: ["usa"], description="Regions to keep") 

14 organize_languages: bool = Field(default=False, description="Create language symlinks") 

15 exclude_language_regions: list[str] = Field( 

16 default_factory=list, description="Regions to exclude from language symlinks" 

17 ) 

18 organize_kinds: bool = Field(default=True, description="Organize by kind (Applications, etc)") 

19 keep_in_root_regions: list[str] = Field( 

20 default_factory=list, description="Regions to keep in root without subfolders" 

21 ) 

22 keep_in_root_kinds: list[str] = Field( 

23 default_factory=list, description="Kinds to keep in root without subfolders" 

24 ) 

25 

26 @field_validator("regions", "exclude_language_regions", "keep_in_root_regions") 

27 @classmethod 

28 def lowercase_regions(cls, v: list[str]) -> list[str]: 

29 """Ensure regions are lowercase.""" 

30 return [r.lower() for r in v] 

31 

32 

33class DatabaseConfig(BaseModel): 

34 """Database configuration.""" 

35 

36 path: Path = Field( 

37 default_factory=lambda: Path("catalog/database/catalog.db"), 

38 description="Database file path", 

39 ) 

40 echo: bool = Field(default=False, description="Echo SQL statements") 

41 pool_size: int = Field(default=5, description="Connection pool size") 

42 max_overflow: int = Field(default=10, description="Max connection overflow") 

43 

44 

45class LoggingConfig(BaseModel): 

46 """Logging configuration.""" 

47 

48 level: str = Field(default="INFO", description="Logging level") 

49 log_dir: Path | None = Field( 

50 default=None, description="Log directory (default: ./logs in project root)" 

51 ) 

52 enable_file_logging: bool = Field(default=True, description="Enable file logging") 

53 

54 @field_validator("level") 

55 @classmethod 

56 def uppercase_level(cls, v: str) -> str: 

57 """Ensure level is uppercase.""" 

58 return v.upper() 

59 

60 

61class RomGroomerConfig(BaseModel): 

62 """ 

63 Main configuration for ROM Farmer. 

64 

65 Configuration is loaded from: 

66 1. ~/.config/romfarmer/config.yaml (user config) 

67 2. /etc/romfarmer/config.yaml (system config) 

68 3. Environment variables (ROMGROOMER_*) 

69 4. Command-line arguments 

70 """ 

71 

72 version: str = Field(default="2.0.0", description="Config version") 

73 profiles: dict[str, OrganizationProfile] = Field( 

74 default_factory=dict, description="Organization profiles" 

75 ) 

76 database: DatabaseConfig = Field( 

77 default_factory=DatabaseConfig, description="Database configuration" 

78 ) 

79 logging: LoggingConfig = Field( 

80 default_factory=LoggingConfig, description="Logging configuration" 

81 ) 

82 

83 # Global settings 

84 parallel_workers: int = Field(default=4, description="Number of parallel workers") 

85 verify_hashes: bool = Field(default=True, description="Verify file hashes") 

86 case_insensitive_check: bool = Field( 

87 default=True, description="Check for case-insensitive filename collisions" 

88 ) 

89 

90 @classmethod 

91 def load(cls, config_path: Path | None = None) -> "RomGroomerConfig": 

92 """ 

93 Load configuration from file. 

94 

95 Args: 

96 config_path: Path to config file (default: ~/.config/romfarmer/config.yaml) 

97 

98 Returns: 

99 RomGroomerConfig instance 

100 """ 

101 if config_path is None: 

102 config_path = Path.home() / ".config" / "romfarmer" / "config.yaml" 

103 

104 if not config_path.exists(): 

105 # Return default configuration 

106 return cls() 

107 

108 with open(config_path) as f: 

109 data = yaml.safe_load(f) 

110 

111 return cls(**data) 

112 

113 def save(self, config_path: Path | None = None) -> None: 

114 """ 

115 Save configuration to file. 

116 

117 Args: 

118 config_path: Path to config file (default: ~/.config/romfarmer/config.yaml) 

119 """ 

120 if config_path is None: 

121 config_path = Path.home() / ".config" / "romfarmer" / "config.yaml" 

122 

123 config_path.parent.mkdir(parents=True, exist_ok=True) 

124 

125 with open(config_path, "w") as f: 

126 yaml.safe_dump( 

127 self.model_dump(mode="json", exclude_none=True), 

128 f, 

129 default_flow_style=False, 

130 sort_keys=False, 

131 ) 

132 

133 def get_profile(self, name: str) -> OrganizationProfile | None: 

134 """Get profile by name.""" 

135 return self.profiles.get(name) 

136 

137 def add_profile(self, profile: OrganizationProfile) -> None: 

138 """Add or update a profile.""" 

139 self.profiles[profile.name] = profile 

140 

141 @classmethod 

142 def create_default_config(cls) -> "RomGroomerConfig": 

143 """Create default configuration with example profiles.""" 

144 config = cls() 

145 

146 # NES USA profile 

147 config.add_profile( 

148 OrganizationProfile( 

149 name="nes-usa", 

150 regions=["usa", "world"], 

151 organize_languages=False, 

152 organize_kinds=True, 

153 ) 

154 ) 

155 

156 # NES English profile 

157 config.add_profile( 

158 OrganizationProfile( 

159 name="nes-eng", 

160 regions=["europe", "australia", "world"], 

161 organize_languages=True, 

162 exclude_language_regions=["usa"], 

163 organize_kinds=True, 

164 ) 

165 ) 

166 

167 # Saturn USA profile 

168 config.add_profile( 

169 OrganizationProfile( 

170 name="saturn-usa", 

171 regions=["usa"], 

172 organize_languages=False, 

173 organize_kinds=True, 

174 ) 

175 ) 

176 

177 # Saturn English profile 

178 config.add_profile( 

179 OrganizationProfile( 

180 name="saturn-eng", 

181 regions=["europe", "usa"], 

182 organize_languages=True, 

183 exclude_language_regions=["usa"], 

184 organize_kinds=True, 

185 ) 

186 ) 

187 

188 # All regions profile 

189 config.add_profile( 

190 OrganizationProfile( 

191 name="all", 

192 regions=["*"], # Special marker for all regions 

193 organize_languages=True, 

194 exclude_language_regions=[], 

195 organize_kinds=True, 

196 ) 

197 ) 

198 

199 return config