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

27 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-04-30 09:42 +0000

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

2import logging 

3from pathlib import Path 

4 

5import tomlkit 

6 

7from somesy.core.core import get_input_content 

8from somesy.core.models import SomesyInput 

9 

10logger = logging.getLogger("somesy") 

11 

12 

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

14 """Initialize somesy configuration file. 

15 

16 Args: 

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

18 options (dict): CLI options. 

19 """ 

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

21 

22 content = get_input_content(input_path, no_unwrap=True) 

23 

24 is_somesy = SomesyInput.is_somesy_file_path(input_path) 

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

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

27 logger.verbose(msg) 

28 

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

30 

31 if "input_file" in options: 

32 del options["input_file"] 

33 if is_somesy: 

34 content["config"] = options 

35 else: 

36 if "tool" not in content: 

37 content["tool"] = {} 

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

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

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

41 

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

43 tomlkit.dump(content, f) 

44 

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

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