Coverage for src/somesy/cli/fill.py: 48%

25 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-11-09 13:08 +0000

1"""Fill command of somesy.""" 

2 

3import logging 

4from pathlib import Path 

5from sys import stdin 

6 

7import typer 

8from jinja2 import Environment, FunctionLoader, select_autoescape 

9 

10from somesy.core.core import atomic_write 

11 

12from .util import ( 

13 existing_file_arg_config, 

14 file_arg_config, 

15 resolved_somesy_input, 

16 wrap_exceptions, 

17) 

18 

19logger = logging.getLogger("somesy") 

20app = typer.Typer() 

21 

22 

23@app.callback(invoke_without_command=True) 

24@wrap_exceptions 

25def fill( 

26 template_file: Path = typer.Option( 

27 None, 

28 "--template", 

29 "-t", 

30 help="Path to a Jinja2 template for somesy to fill (default: stdin).", 

31 **existing_file_arg_config, 

32 ), 

33 input_file: Path = typer.Option( 

34 None, 

35 "--input-file", 

36 "-i", 

37 help="Path of somesy input file (default: try to infer).", 

38 **existing_file_arg_config, 

39 ), 

40 output_file: Path = typer.Option( 

41 None, 

42 "--output-file", 

43 "-o", 

44 help="Path for target file (default: stdout).", 

45 **file_arg_config, 

46 ), 

47): 

48 """Fill a Jinja2 template with somesy project metadata (e.g. list authors in project docs).""" 

49 somesy_input = resolved_somesy_input(input_file=input_file) 

50 

51 if template_file: 

52 logger.debug(f"Reading Jinja2 template from '{template_file}'.") 

53 with open(template_file, "r", encoding="utf-8") as f: 

54 template_str = f.read() 

55 else: 

56 logger.debug("Reading Jinja2 template from stdin.") 

57 template_str = stdin.read() 

58 

59 result = ( 

60 Environment( 

61 loader=FunctionLoader(lambda _: template_str), 

62 autoescape=select_autoescape(), 

63 ) 

64 .get_template("") 

65 .render(project=somesy_input.project) 

66 ) 

67 

68 if output_file: 

69 logger.debug(f"Writing result to '{output_file}'.") 

70 atomic_write(output_file, result) 

71 else: 

72 logger.debug("Writing result to stdout.") 

73 print(result)