Skip to content

fill

Fill command of somesy.

fill

fill(
    template_file: Path = typer.Option(
        None,
        "--template",
        "-t",
        help="Path to a Jinja2 template for somesy to fill (default: stdin).",
        **existing_file_arg_config
    ),
    input_file: Path = typer.Option(
        None,
        "--input-file",
        "-i",
        help="Path of somesy input file (default: try to infer).",
        **existing_file_arg_config
    ),
    output_file: Path = typer.Option(
        None,
        "--output-file",
        "-o",
        help="Path for target file (default: stdout).",
        **file_arg_config
    ),
)

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

Source code in src/somesy/cli/fill.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@app.callback(invoke_without_command=True)
@wrap_exceptions
def fill(
    template_file: Path = typer.Option(
        None,
        "--template",
        "-t",
        help="Path to a Jinja2 template for somesy to fill (default: stdin).",
        **existing_file_arg_config,
    ),
    input_file: Path = typer.Option(
        None,
        "--input-file",
        "-i",
        help="Path of somesy input file (default: try to infer).",
        **existing_file_arg_config,
    ),
    output_file: Path = typer.Option(
        None,
        "--output-file",
        "-o",
        help="Path for target file (default: stdout).",
        **file_arg_config,
    ),
):
    """Fill a Jinja2 template with somesy project metadata (e.g. list authors in project docs)."""
    somesy_input = resolved_somesy_input(input_file=input_file)

    if template_file:
        logger.debug(f"Reading Jinja2 template from '{template_file}'.")
        with open(template_file, "r") as f:
            template_str = f.read()
    else:
        logger.debug("Reading Jinja2 template from stdin.")
        template_str = stdin.read()

    result = (
        Environment(
            loader=FunctionLoader(lambda _: template_str),
            autoescape=select_autoescape(),
        )
        .get_template("")
        .render(project=somesy_input.project)
    )

    if output_file:
        logger.debug(f"Writing result to '{output_file}'.")
        with open(output_file, "w") as f:
            f.write(result)
    else:
        logger.debug("Writing result to stdout.")
        print(result)