Coverage for src / rhiza_tools / config.py: 96%

27 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-05 10:07 +0000

1"""Configuration management for rhiza-tools.""" 

2 

3from pathlib import Path 

4from typing import Any 

5 

6import tomlkit 

7from loguru import logger 

8 

9CONFIG_FILENAME = ".rhiza/.cfg.toml" 

10 

11 

12class RhizaConfig: 

13 """Rhiza tools configuration.""" 

14 

15 def __init__(self, config_path: Path | None = None): 

16 """Initialize RhizaConfig.""" 

17 self.config_path = config_path or Path(CONFIG_FILENAME) 

18 self._data: dict[str, Any] = {} 

19 self.load() 

20 

21 def load(self) -> None: 

22 """Load configuration from file.""" 

23 if not self.config_path.exists(): 

24 logger.debug(f"Configuration file {self.config_path} not found.") 

25 return 

26 

27 try: 

28 with open(self.config_path) as f: 

29 self._data = tomlkit.parse(f.read()) 

30 except Exception as e: 

31 logger.error(f"Failed to parse configuration file {self.config_path}: {e}") 

32 raise 

33 

34 @property 

35 def bumpversion(self) -> dict[str, Any]: 

36 """Get bumpversion configuration.""" 

37 return self._data.get("tool", {}).get("bumpversion", {}) 

38 

39 def get(self, key: str, default: Any = None) -> Any: 

40 """Get configuration value.""" 

41 return self._data.get(key, default) 

42 

43 

44def load_config(path: Path | None = None) -> RhizaConfig: 

45 """Load configuration.""" 

46 return RhizaConfig(path)