Skip to content

Commit

Permalink
Add a validity checker on CLI parameters (#1717)
Browse files Browse the repository at this point in the history
Closes #1714.

---------

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
Andrew-S-Rosen and deepsource-autofix[bot] authored Feb 17, 2024
1 parent 8cc4085 commit d6a3f30
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Added a `store_intermediate_files` keyword option to `quacc.runners.ase.run_opt()` to allow for storing of the logfiles in intermediate geometry optimization steps.
- Added support for Pymatgen-based input sets in VASP jobs
- Added an MP meta-GGA VASP static job
- Added a validity checker on CLI parameters

### Changed

Expand Down
14 changes: 12 additions & 2 deletions src/quacc/_cli/quacc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import typer
from rich import print as rich_print

from quacc.settings import QuaccSettings, _type_handler

app = typer.Typer()

if TYPE_CHECKING:
Expand Down Expand Up @@ -82,7 +84,9 @@ def set_(parameter: str, new_value: str) -> None:
CONFIG_FILE = SETTINGS.CONFIG_FILE or _DEFAULT_CONFIG_FILE_PATH
parameter = parameter.upper()

_parameter_handler(parameter, SETTINGS.model_dump())
settings = _type_handler({parameter: new_value})
new_value = settings[parameter]
_parameter_handler(parameter, SETTINGS.model_dump(), value=new_value)

rich_print(f"Setting `{parameter}` to `{new_value}` in {CONFIG_FILE}")
_update_setting(parameter, new_value, CONFIG_FILE)
Expand Down Expand Up @@ -143,7 +147,9 @@ def info() -> None:
)


def _parameter_handler(parameter: str, settings_dict: dict) -> None:
def _parameter_handler(
parameter: str, settings_dict: dict, value: Any | None = object
) -> None:
"""
Check if the parameter is a valid quacc configuration variable.
Expand All @@ -153,6 +159,8 @@ def _parameter_handler(parameter: str, settings_dict: dict) -> None:
The quacc parameter to check.
settings_dict
The quacc settings.
value
The value to set the parameter to.
Returns
-------
Expand All @@ -163,6 +171,8 @@ def _parameter_handler(parameter: str, settings_dict: dict) -> None:
if parameter == "CONFIG_FILE":
msg = "Cannot set the CONFIG_FILE parameter via the CLI."
raise ValueError(msg)
if value is not object:
QuaccSettings(**{f"{parameter}": value})


def _delete_setting(key: str, config_file: Path) -> None:
Expand Down
2 changes: 2 additions & 0 deletions tests/core/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def test_set(runner):
if "VASP_PARALLEL_CMD" in line:
val = line.split(":")[-1].strip()
assert val == "dummy"
response = runner.invoke(app, ["set", "WORKFLOW_ENGINE", "invalid"])
assert response.exit_code == 1


def test_unset(runner):
Expand Down

0 comments on commit d6a3f30

Please sign in to comment.