Coverage for src / dotbot / config.py: 80%

30 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-29 10:55 -0800

1import json 

2import os.path 

3from typing import Any, List 

4 

5import yaml 

6 

7from dotbot.util import string 

8 

9 

10class ConfigReader: 

11 _config: List[Any] 

12 

13 def __init__(self, config_file_paths: List[str]): 

14 self._config = [] 

15 for path in config_file_paths: 

16 config = self._read(path) 

17 if config is None: 

18 continue 

19 if not isinstance(config, list): 

20 msg = "Configuration file must be a list of tasks" 

21 raise ReadingError(msg) 

22 self._config.extend(config) 

23 

24 def _read(self, config_file_path: str) -> Any: 

25 try: 

26 _, ext = os.path.splitext(config_file_path) 

27 with open(config_file_path, encoding="utf-8") as fin: 

28 return json.load(fin) if ext == ".json" else yaml.safe_load(fin) 

29 except Exception as e: 

30 msg = string.indent_lines(str(e)) 

31 msg = f"Could not read config file:\n{msg}" 

32 raise ReadingError(msg) from e 

33 

34 def get_config(self) -> Any: 

35 return self._config 

36 

37 

38class ReadingError(Exception): 

39 pass