Coverage for src/somesy/commands/init_config.py: 26%

27 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-29 07:42 +0000

1"""CLI command to initialize somesy configuration file.""" 

2 

3import logging 

4from pathlib import Path 

5 

6import tomlkit 

7 

8from somesy.core.core import get_input_content 

9from somesy.core.models import SomesyInput 

10 

11logger = logging.getLogger("somesy") 

12 

13 

14def init_config(input_path: Path, options: dict) -> None: 

15 """Initialize somesy configuration file. 

16 

17 Args: 

18 input_path (Path): Path to somesy file (will be created/overwritten). 

19 options (dict): CLI options. 

20 

21 """ 

22 logger.info(f"Updating input file ({input_path}) with CLI configurations...") 

23 

24 content = get_input_content(input_path, no_unwrap=True) 

25 

26 is_somesy = SomesyInput.is_somesy_file_path(input_path) 

27 input_file_type = "somesy" if is_somesy else "pyproject" 

28 msg = f"Found input file with {input_file_type} format." 

29 logger.verbose(msg) 

30 

31 logger.debug(f"Input file content: {options}") 

32 

33 if "input_file" in options: 

34 del options["input_file"] 

35 if is_somesy: 

36 content["config"] = options 

37 else: 

38 if "tool" not in content: 

39 content["tool"] = {} 

40 if "somesy" not in content["tool"]: 

41 content["tool"]["somesy"] = {} 

42 content["tool"]["somesy"]["config"] = options 

43 

44 with open(input_path, "w") as f: 

45 tomlkit.dump(content, f) 

46 

47 logger.info(f"Input file ({input_path}) updated.") 

48 logger.debug(f"Input file content: {content}")