Coverage for src/romfarmer/cache/config.py: 0%

34 statements  

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

1"""Cache configuration models. 

2 

3These classes define configuration for the ROM cache system. 

4""" 

5 

6import os 

7from dataclasses import dataclass, field 

8from enum import Enum 

9from pathlib import Path 

10from typing import TYPE_CHECKING, Optional 

11 

12if TYPE_CHECKING: 

13 from .models import ROMCache 

14 

15 

16class CacheLinkMode(str, Enum): 

17 """How to provide cached files to output directories.""" 

18 

19 HARDLINK = "hardlink" # Same inode, zero space, same filesystem only 

20 SYMLINK = "symlink" # Cross-filesystem, but breaks if cache moves 

21 COPY = "copy" # Full copy, portable but uses 2x space 

22 

23 

24class CacheVerifyLevel(str, Enum): 

25 """How thoroughly to verify cached files.""" 

26 

27 NONE = "none" # Trust DB, fastest 

28 EXISTS = "exists" # Check file exists (default) 

29 SIZE = "size" # Check exists + size matches 

30 MD5 = "md5" # Full verification, slowest 

31 

32 

33@dataclass 

34class CacheConfig: 

35 """Cache configuration.""" 

36 

37 cache_dir: Path = field(default_factory=lambda: Path("cache")) 

38 enabled: bool = True 

39 link_mode: CacheLinkMode = CacheLinkMode.HARDLINK 

40 verify_level: CacheVerifyLevel = CacheVerifyLevel.EXISTS 

41 check_tool_version: bool = True 

42 

43 @classmethod 

44 def from_env(cls, workspace_root: Path | None = None) -> "CacheConfig": 

45 """Load configuration from environment variables.""" 

46 cache_dir = os.environ.get("ROMGROOMER_CACHE_DIR", "cache") 

47 if workspace_root and not Path(cache_dir).is_absolute(): 

48 cache_dir = workspace_root / cache_dir 

49 

50 return cls( 

51 cache_dir=Path(cache_dir), 

52 enabled=os.environ.get("ROMGROOMER_CACHE_ENABLED", "true").lower() == "true", 

53 link_mode=CacheLinkMode( 

54 os.environ.get("ROMGROOMER_CACHE_LINK_MODE", "hardlink").lower() 

55 ), 

56 verify_level=CacheVerifyLevel( 

57 os.environ.get("ROMGROOMER_CACHE_VERIFY_LEVEL", "exists").lower() 

58 ), 

59 check_tool_version=os.environ.get("ROMGROOMER_CACHE_CHECK_TOOL_VERSION", "true").lower() 

60 == "true", 

61 ) 

62 

63 

64@dataclass 

65class CacheResult: 

66 """Result of a cache operation.""" 

67 

68 hit: bool 

69 cache_path: Path | None = None 

70 linked_path: Path | None = None 

71 entry: Optional["ROMCache"] = None # Forward reference 

72 message: str = ""