Skip to content

Commit 96095e3

Browse files
authored
Add the option to only show the configuration for a single project (#3020)
1 parent 65c3797 commit 96095e3

2 files changed

Lines changed: 53 additions & 12 deletions

File tree

esmvalcore/_main.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,20 @@ def __init__(self) -> None:
178178
def show(
179179
self,
180180
filter: tuple[str] | None = ("extra_facets",), # noqa: A002
181+
project: str | None = None,
181182
) -> None:
182183
"""Show the current configuration.
183184
184185
Parameters
185186
----------
186187
filter:
187188
Filter this list of keys. By default, the `extra_facets`
188-
key is filtered out, as it can be very large.
189+
key is filtered out, as it can be very large. For example, to show
190+
the full configuration, use `--filter=` and to filter out both
191+
the `data` and `extra_facets` keys use `--filter=data,extra_facets`.
192+
project:
193+
Only show configuration for this project. For example, to only show
194+
the configuration for the CMIP7 project, use `--project=CMIP7`.
189195
190196
"""
191197
import yaml
@@ -196,17 +202,19 @@ def show(
196202
from esmvalcore.config import CFG
197203

198204
cfg = dict(CFG)
205+
msg = "# Current configuration"
206+
if project and "projects" in cfg and project in cfg["projects"]:
207+
cfg = {"projects": {project: cfg["projects"][project]}}
208+
msg += f" for project '{project}'"
199209
if filter:
210+
if isinstance(filter, str):
211+
filter = (filter,) # noqa: A001
200212
for key in filter:
201213
cfg = nested_delete(cfg, key)
202-
exclude_msg = (
203-
", excluding the keys " + ", ".join(f"'{f}'" for f in filter)
204-
if filter
205-
else ""
206-
)
207-
self._console.print(
208-
Markdown(f"# Current configuration{exclude_msg}\n<br>"),
209-
)
214+
msg += ", excluding the keys " + ", ".join(
215+
f"'{key}'" for key in filter
216+
)
217+
self._console.print(Markdown(f"{msg}:\n<br>"))
210218
self._console.print(
211219
Syntax(
212220
yaml.safe_dump(cfg),

tests/integration/test_main.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,57 @@ def test_config_show(
205205
cfg_default: Config,
206206
) -> None:
207207
"""Test esmvaltool config show command."""
208-
with arguments("esmvaltool", "config", "show", "--filter=None"):
208+
with arguments("esmvaltool", "config", "show", "--filter="):
209209
run()
210210
stdout = capsys.readouterr().out
211-
expected_header = "Current configuration\n"
211+
expected_header = "Current configuration:\n"
212212
assert expected_header in stdout
213213
cfg_txt = stdout.split(expected_header)[1]
214214
cfg = yaml.safe_load(cfg_txt)
215215
reference = yaml.safe_load(yaml.safe_dump(dict(cfg_default))) # type: ignore[call-overload]
216216
assert cfg == reference
217217

218218

219+
def test_config_show_single_project(
220+
capsys: pytest.CaptureFixture,
221+
cfg_default: Config,
222+
) -> None:
223+
"""Test esmvaltool config show command for a single project."""
224+
with arguments("esmvaltool", "config", "show", "--project=CMIP7"):
225+
run()
226+
stdout = capsys.readouterr().out
227+
expected_header = "Current configuration for project 'CMIP7', excluding the keys 'extra_facets':\n"
228+
assert expected_header in stdout
229+
cfg_txt = stdout.split(expected_header)[1]
230+
cfg = yaml.safe_load(cfg_txt)
231+
assert "projects" in cfg
232+
assert "CMIP7" in cfg["projects"]
233+
assert "CMIP6" not in cfg["projects"]
234+
235+
236+
def test_config_show_filter(
237+
capsys: pytest.CaptureFixture,
238+
cfg_default: Config,
239+
) -> None:
240+
"""Test esmvaltool config show command for a single project."""
241+
with arguments("esmvaltool", "config", "show", "--filter=projects"):
242+
run()
243+
stdout = capsys.readouterr().out
244+
expected_header = "Current configuration, excluding the keys 'projects':\n"
245+
assert expected_header in stdout
246+
cfg_txt = stdout.split(expected_header)[1]
247+
cfg = yaml.safe_load(cfg_txt)
248+
assert cfg
249+
assert "projects" not in cfg
250+
251+
219252
def test_config_show_brief_by_default(capsys: pytest.CaptureFixture) -> None:
220253
"""Test that the `esmvaltool config show` command produces readable results."""
221254
with arguments("esmvaltool", "config", "show"):
222255
run()
223256
stdout = capsys.readouterr().out
224257
expected_header = (
225-
"Current configuration, excluding the keys 'extra_facets'\n"
258+
"Current configuration, excluding the keys 'extra_facets':\n"
226259
)
227260
assert expected_header in stdout
228261
# Check that the configuration that is listed by default is sufficiently

0 commit comments

Comments
 (0)