Coverage for src/cradle/config.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.10.3, created at 2025-08-16 05:53 +0000

1"""Configuration module for the Cradle CLI. 

2 

3This module handles reading and writing the configuration file, 

4which contains information about available template repositories. 

5""" 

6 

7from pathlib import Path 

8from typing import Any 

9 

10import yaml 

11 

12# Default configuration directory 

13CONFIG_DIR = Path.home() / ".cradle" 

14CONFIG_FILE = CONFIG_DIR / "config.yaml" 

15 

16# Default configuration 

17DEFAULT_CONFIG = { 

18 "templates": { 

19 "experiments": { 

20 "url": "https://github.com/tschm/experiments", 

21 "description": "Template for experimental projects with Marimo notebooks", 

22 }, 

23 "package": { 

24 "url": "https://github.com/tschm/package", 

25 "description": "Template for Python packages with PyPI publishing support", 

26 }, 

27 "paper": { 

28 "url": "https://github.com/tschm/paper", 

29 "description": "Template for academic papers with LaTeX support", 

30 }, 

31 } 

32} 

33 

34 

35def _ensure_config_file() -> None: 

36 """Ensure the configuration file exists.""" 

37 if not CONFIG_DIR.exists(): 

38 CONFIG_DIR.mkdir(parents=True) 

39 

40 if not CONFIG_FILE.exists(): 

41 with open(CONFIG_FILE, "w") as f: 

42 yaml.dump(DEFAULT_CONFIG, f) 

43 

44 

45def _read_config(config_file=None) -> dict[str, Any]: 

46 """Read the configuration file.""" 

47 if config_file is None: 

48 _ensure_config_file() 

49 config_file = CONFIG_FILE 

50 

51 with open(config_file) as f: 

52 return yaml.safe_load(f) 

53 

54 

55def get_all_templates(config_file=None) -> dict[str, dict[str, Any]]: 

56 """Get information about all templates.""" 

57 config = _read_config(config_file) 

58 return config.get("templates", {})