Coverage for src/romfarmer/config/tiers_loader.py: 39%

23 statements  

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

1"""Loader for platform tiers configuration. 

2 

3Provides functions to load and cache the platform tiers config. 

4""" 

5 

6import logging 

7from functools import lru_cache 

8from pathlib import Path 

9 

10import yaml 

11 

12from .models import PlatformTiersConfig 

13 

14logger = logging.getLogger(__name__) 

15 

16 

17@lru_cache(maxsize=1) 

18def load_platform_tiers(config_root: Path | None = None) -> PlatformTiersConfig: 

19 """Load the platform tiers configuration. 

20 

21 The tiers config is loaded from config/platform_tiers.yaml. 

22 Results are cached after first load. 

23 

24 Args: 

25 config_root: Root config directory. Defaults to 'config/' in current directory. 

26 

27 Returns: 

28 PlatformTiersConfig instance 

29 

30 Raises: 

31 FileNotFoundError: If platform_tiers.yaml doesn't exist 

32 """ 

33 if config_root is None: 

34 config_root = Path("config") 

35 

36 config_path = config_root / "platform_tiers.yaml" 

37 

38 if not config_path.exists(): 

39 logger.warning(f"Platform tiers config not found: {config_path}") 

40 logger.warning("Using empty default tiers config") 

41 return PlatformTiersConfig() 

42 

43 logger.debug(f"Loading platform tiers config: {config_path}") 

44 

45 with open(config_path) as f: 

46 data = yaml.safe_load(f) 

47 

48 # Parse the YAML structure into our models 

49 config = PlatformTiersConfig.model_validate(data) 

50 

51 logger.info( 

52 f"Loaded platform tiers: " 

53 f"{len(config.tier_1.platforms)} T1, " 

54 f"{len(config.tier_2.platforms)} T2, " 

55 f"{len(config.tier_3.platforms)} T3, " 

56 f"{len(config.tier_4.platforms)} T4, " 

57 f"{len(config.tier_5.platforms)} T5" 

58 ) 

59 

60 return config 

61 

62 

63def clear_tiers_cache(): 

64 """Clear the cached tiers config. 

65 

66 Useful for testing or when config files change. 

67 """ 

68 load_platform_tiers.cache_clear()