Skip to content
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
24 changes: 17 additions & 7 deletions esmvalcore/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,20 @@ def __init__(self) -> None:
def show(
self,
filter: tuple[str] | None = ("extra_facets",), # noqa: A002
project: str | None = None,
) -> None:
"""Show the current configuration.

Parameters
----------
filter:
Filter this list of keys. By default, the `extra_facets`
key is filtered out, as it can be very large.
key is filtered out, as it can be very large. For example, to show
the full configuration, use `--filter=` and to filter out both
the `data` and `extra_facets` keys use `--filter=data,extra_facets`.
project:
Only show configuration for this project. For example, to only show
the configuration for the CMIP7 project, use `--project=CMIP7`.

"""
import yaml
Expand All @@ -195,15 +201,19 @@ def show(
from esmvalcore.config import CFG

cfg = dict(CFG)
msg = "# Current configuration"
if project and "projects" in cfg and project in cfg["projects"]:
cfg = {"projects": {project: cfg["projects"][project]}}
msg += f" for project '{project}'"
if filter:
if isinstance(filter, str):
filter = (filter,) # noqa: A001
for key in filter:
cfg = nested_delete(cfg, key)
exclude_msg = (
", excluding the keys " + ", ".join(f"'{f}'" for f in filter)
if filter
else ""
)
self.console.print(f"# Current configuration{exclude_msg}:")
msg += ", excluding the keys " + ", ".join(
f"'{key}'" for key in filter
)
self.console.print(f"{msg}:")
self.console.print(
Syntax(
yaml.safe_dump(cfg),
Expand Down
37 changes: 36 additions & 1 deletion tests/integration/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def test_config_show(
cfg_default: Config,
) -> None:
"""Test esmvaltool config show command."""
with arguments("esmvaltool", "config", "show", "--filter=None"):
with arguments("esmvaltool", "config", "show", "--filter="):
run()
stdout = capsys.readouterr().out
expected_header = "# Current configuration:\n"
Expand All @@ -216,6 +216,41 @@ def test_config_show(
assert cfg == reference


def test_config_show_single_project(
capsys: pytest.CaptureFixture,
cfg_default: Config,
) -> None:
"""Test esmvaltool config show command for a single project."""
with arguments("esmvaltool", "config", "show", "--project=CMIP7"):
run()
stdout = capsys.readouterr().out
expected_header = "# Current configuration for project 'CMIP7', excluding the keys 'extra_facets':\n"
assert expected_header in stdout
cfg_txt = stdout.split(expected_header)[1]
cfg = yaml.safe_load(cfg_txt)
assert "projects" in cfg
assert "CMIP7" in cfg["projects"]
assert "CMIP6" not in cfg["projects"]


def test_config_show_filter(
capsys: pytest.CaptureFixture,
cfg_default: Config,
) -> None:
"""Test esmvaltool config show command for a single project."""
with arguments("esmvaltool", "config", "show", "--filter=projects"):
run()
stdout = capsys.readouterr().out
expected_header = (
"# Current configuration, excluding the keys 'projects':\n"
)
assert expected_header in stdout
cfg_txt = stdout.split(expected_header)[1]
cfg = yaml.safe_load(cfg_txt)
assert cfg
assert "projects" not in cfg


def test_config_show_brief_by_default(capsys: pytest.CaptureFixture) -> None:
"""Test that the `esmvaltool config show` command produces readable results."""
with arguments("esmvaltool", "config", "show"):
Expand Down