Skip to content

Commit

Permalink
Add --indent option to flepimop patch
Browse files Browse the repository at this point in the history
For configuration of the outputted yaml.
  • Loading branch information
TimothyWillard committed Dec 11, 2024
1 parent 02678a3 commit fe1dc2b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
23 changes: 18 additions & 5 deletions flepimop/gempyor_pkg/src/gempyor/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from click import pass_context, Context
import click
import yaml

from .shared_cli import (
config_files_argument,
Expand All @@ -20,12 +21,24 @@


# add some basic commands to the CLI
@cli.command(params=[config_files_argument] + list(config_file_options.values()))
@pass_context
def patch(ctx: Context = mock_context, **kwargs) -> None:
@cli.command(
params=[config_files_argument]
+ list(config_file_options.values())
+ [
click.Option(
["--indent"],
type=click.IntRange(min=1),
required=False,
default=2,
help="Indentation level for the output YAML.",
)
],
)
@click.pass_context
def patch(ctx: click.Context = mock_context, **kwargs) -> None:
"""Merge configuration files"""
parse_config_files(config, ctx, **kwargs)
print(config.dump())
print(yaml.dump(yaml.safe_load(config.dump()), indent=kwargs["indent"]))


if __name__ == "__main__":
Expand Down
24 changes: 24 additions & 0 deletions flepimop/gempyor_pkg/tests/cli/test_flepimop_patch_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,27 @@ def test_patch_seir_parameters_behavior(
patched_config = yaml.safe_load(result.output)
assert "seir" in patched_config
assert patched_config["seir"]["parameters"] == expected_parameters


@pytest.mark.parametrize("indent", (2, 4, 6))
def test_user_provided_indent_size(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, indent: int
) -> None:
# Setup the test
monkeypatch.chdir(tmp_path)
config = tmp_path / "config.yml"
config.write_text(yaml.dump({"seir": {"parameters": {"beta": {"value": 1.2}}}}))

# Invoke the command
runner = CliRunner()
result = runner.invoke(patch, [config.name, "--indent", str(indent)])
assert result.exit_code == 0

# Check the output indentation, manually since `yaml.load` abstracts spacing away
for line in result.output.split():
stripped_line = line.lstrip()
if stripped_line and not stripped_line.startswith("#"):
indent_size = len(line) - len(stripped_line)
if indent_size > 0:
assert indent_size == indent
break

0 comments on commit fe1dc2b

Please sign in to comment.