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

UW-269 Extend set_config.py tool to "export" variables #280

Merged
merged 21 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
20 changes: 20 additions & 0 deletions src/uwtools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import yaml

from uwtools import exceptions, logger
from uwtools.exceptions import UWConfigError
from uwtools.j2template import J2Template
from uwtools.logger import Logger
from uwtools.utils import cli_helpers
Expand Down Expand Up @@ -752,3 +753,22 @@ def create_config_obj(
raise ValueError(err_msg)
# Dump to file:
dump_method(path=outfile, cfg=config_obj)


def print_config_section(config: dict, section_path: List[str], log: Logger) -> None:
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved
"""
Descends into the config via the given section keys, then prints the contents of the located
subtree as key=value pairs, one per line.
"""
for key in section_path:
if isinstance(config[key], dict):
config = config[key]
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved
else:
log.error(f"{key} type must be a dictionary")
raise UWConfigError("Key type must be a dictionary")
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved
for key, value in config.items():
if type(value) not in (bool, float, int, str):
maddenp-noaa marked this conversation as resolved.
Show resolved Hide resolved
log.error(f"Non-scalar value {key} was provided")
raise UWConfigError("Section values provided must be scalar values")
for key, value in config.items():
print(f"{key}={value}")
44 changes: 44 additions & 0 deletions src/uwtools/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from uwtools import config, exceptions
from uwtools.exceptions import UWConfigError
from uwtools.logger import Logger
from uwtools.tests.support import compare_files, fixture_path, line_in_lines, msg_in_caplog
from uwtools.utils import cli_helpers

Expand Down Expand Up @@ -810,3 +811,46 @@ def test_YAMLConfig__load_unexpected_error(tmp_path):
with raises(UWConfigError) as e:
config.YAMLConfig(config_path=cfgfile)
assert msg in str(e.value)


def test_print_config_section_yaml(capsys):
config_obj = config.YAMLConfig(fixture_path("FV3_GFS_v16.yaml"))
section = ["sgs_tke", "profile_type"]
config.print_config_section(config_obj.data, section, log=Logger())
actual = capsys.readouterr().out
expected = """name=fixed
surface_value=0.0\n"""
assert actual == expected


def test_print_config_section_yaml_for_nonscalar():
config_obj = config.YAMLConfig(fixture_path("FV3_GFS_v16.yaml"))
section = ["o3mr"]
with raises(UWConfigError):
config.print_config_section(config_obj.data, section, log=Logger())


def test_print_config_section_yaml_not_dict():
config_obj = config.YAMLConfig(fixture_path("FV3_GFS_v16.yaml"))
section = ["sgs_tke", "units"]
with raises(UWConfigError):
config.print_config_section(config_obj.data, section, log=Logger())


def test_print_config_section_ini(capsys):
config_obj = config.INIConfig(fixture_path("simple3.ini"))
section = ["dessert"]
config.print_config_section(config_obj.data, section, log=Logger())
actual = capsys.readouterr().out
expected = """type=pie
flavor={{flavor}}
side=False
servings=0\n"""
assert actual == expected


def test_print_config_section_ini_missing_section():
config_obj = config.INIConfig(fixture_path("simple3.ini"))
section = ["sandwich"]
with raises(KeyError):
config.print_config_section(config_obj.data, section, log=Logger())
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved