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 14 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
35 changes: 35 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,37 @@ def create_config_obj(
raise ValueError(err_msg)
# Dump to file:
dump_method(path=outfile, cfg=config_obj)


def log_and_error(msg: str, log: Logger) -> None:
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved
"""
Will log a user-provided error message and raise a UWConfigError with the same message.
"""
log.error(msg)
raise UWConfigError(msg)


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.
"""
keys = []
for section in section_path:
keys.append(section)
current_path = " -> ".join(keys)
try:
subconfig = config[section]
except KeyError:
log_and_error(f"Bad config path: {current_path}", log)
maddenp-noaa marked this conversation as resolved.
Show resolved Hide resolved
if not type(subconfig) in (dict, list):
log_and_error(f"Value at {current_path} must be a dictionary or list", log)
config = subconfig
output_lines = set()
maddenp-noaa marked this conversation as resolved.
Show resolved Hide resolved
for item in config:
value = config[item] if isinstance(config, dict) else item
if type(value) not in (bool, float, int, str):
maddenp-noaa marked this conversation as resolved.
Show resolved Hide resolved
log_and_error(f"Non-scalar value {value} found at {current_path}", log)
if isinstance(config, dict):
output_lines.add(f"{item}={value}")
print("\n".join(sorted(output_lines)) if isinstance(config, dict) else f"{keys[-1]}={config}")
77 changes: 77 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,79 @@ 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_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 = """
flavor={{flavor}}
servings=0
side=False
type=pie
""".lstrip()
assert actual == expected


def test_print_config_section_ini_missing_section():
config_obj = config.INIConfig(fixture_path("simple3.ini"))
section = ["sandwich"]
msg = "Bad config path: sandwich"
with raises(UWConfigError) as e:
config.print_config_section(config_obj.data, section, log=Logger())
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
""".lstrip()
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) as e:
config.print_config_section(config_obj.data, section, log=Logger())
assert "Non-scalar value" in str(e.value)


def test_print_config_section_yaml_list(capsys):
config_obj = config.YAMLConfig(fixture_path("srw_example.yaml"))
section = ["FV3GFS", "nomads", "file_names", "grib2", "anl"]
config.print_config_section(config_obj.data, section, log=Logger())
actual = capsys.readouterr().out
expected = """
anl=['gfs.t{{ hh }}z.atmanl.nemsio', 'gfs.t{{ hh }}z.sfcanl.nemsio']
""".lstrip()
assert actual == expected


def test_print_config_section_yaml_list_nonscalar():
config_obj = config.YAMLConfig(fixture_path("result4.yaml"))
section = ["models"]
with raises(UWConfigError) as e:
config.print_config_section(config_obj.data, section, log=Logger())
elcarpenterNOAA marked this conversation as resolved.
Show resolved Hide resolved
assert "Non-scalar value" in str(e.value)


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) as e:
config.print_config_section(config_obj.data, section, log=Logger())
assert "must be a dictionary" in str(e.value)


def test_log_and_error():
maddenp-noaa marked this conversation as resolved.
Show resolved Hide resolved
with raises(UWConfigError) as e:
config.log_and_error("Must be scalar value", log=Logger())
assert "Must be scalar value" in str(e.value)