Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow config in command decorator #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# Pending

- Allow passing configuration(s) directly to `@app.command()`

# v0.2.1 - 11-05-2023

- Fix un-allowed kwargs: accepted signatures are `fn(paramA, paramB=..., ... **kwargs)`
Expand Down
25 changes: 24 additions & 1 deletion confit/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import sys
import tempfile
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Type, Union

Expand Down Expand Up @@ -76,6 +77,7 @@ def command( # noqa
# Rich settings
rich_help_panel: Union[str, None] = Default(None),
registry: Any = None,
config: Optional[Union[Config, List[Config]]] = None,
) -> Callable[[CommandFunctionType], CommandFunctionType]:
typer_command = super().command(
name=name,
Expand All @@ -96,14 +98,35 @@ def command( # noqa
},
)

initial_config = config

def wrapper(fn):
validated = validate_arguments(fn)

@typer_command
def command(ctx: Context, config: Optional[List[Path]] = None):
config_path = config
config_path = config or []

has_meta = _fn_has_meta(fn)

if initial_config is not None:
initial_configs = (
[initial_config]
if isinstance(initial_config, Config)
else initial_config
)

initial_config_path = []

for c in initial_configs:
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.write(
c.to_str().encode()
) # Write the string to the file
temp_file.close()
initial_config_path.append(Path(temp_file.name))
config_path = initial_config_path + config_path

if config_path:
config, name_from_file = merge_from_disk(config_path)
Comment on lines +112 to 131
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel the disk writing operation is not really necessary, if its only purpose is to use merge_from_disk. Could we drop merge_from_disk, load all configs and merge them directly from memory instead ?

else:
Expand Down
52 changes: 51 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typer.testing import CliRunner

from confit import Cli, Registry
from confit import Cli, Config, Registry
from confit.registry import RegistryCollection, set_default_registry

runner = CliRunner()
Expand Down Expand Up @@ -94,6 +94,56 @@ def test_cli_merge(change_test_dir):
assert "Other: 99" in result.stdout


app_with_initial_config = Cli(pretty_exceptions_show_locals=False)
initial_config = [
Config(
{
"modelA": {
"submodel": {
"value": 1,
}
},
"script": {
"other": 4,
},
}
),
Config(
{
"script": {
"other": 5,
},
}
),
]


@app_with_initial_config.command(name="script", config=initial_config)
def app_with_initial_config_function(
modelA: BigModel,
modelB: BigModel,
other: int,
seed: int,
):
assert modelA.submodel is modelB.submodel
assert modelA.submodel.value == 12 # --config has precedent over initial_config
assert other == 5 # initial_config[-1] has precedent over initial_config[0]


def test_cli_working_with_initial_config(change_test_dir):
result = runner.invoke(
app_with_initial_config,
[
"--config",
"config.cfg",
"--seed",
"42",
],
)
print(result.exit_code)
assert result.exit_code == 0, result.stdout


app_with_meta = Cli(pretty_exceptions_show_locals=False)


Expand Down