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

Logging improvements in chgres_cube namelist creation #661

Merged
merged 9 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion src/uwtools/config/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def walk_key_path(config: dict, key_path: list[str]) -> tuple[dict, str]:
pathstr = "<unknown>"
for key in key_path:
keys.append(key)
pathstr = " -> ".join(keys)
pathstr = ".".join(keys)
try:
subconfig = config[key]
except KeyError as e:
Expand Down
2 changes: 1 addition & 1 deletion src/uwtools/config/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def validate(schema: dict, desc: str, config: dict) -> bool:
log_msg = "%s UW schema-validation error%s found in %s"
log_method(log_msg, len(errors), "" if len(errors) == 1 else "s", desc)
for error in errors:
log.error("Error at %s:", " -> ".join(str(k) for k in error.path))
log.error("Error at %s:", ".".join(str(k) for k in error.path))
log.error("%s%s", INDENT, error.message)
return not bool(errors)

Expand Down
14 changes: 7 additions & 7 deletions src/uwtools/drivers/chgres_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def namelist_file(self):
input_files = []
namelist = self.config[STR.namelist]
if base_file := namelist.get(STR.basefile):
input_files.append(base_file)
input_files.append((base_file, STR.basefile))
if update_values := namelist.get(STR.updatevalues):
config_files = update_values["config"]
for k in ["mosaic_file_target_grid", "varmap_file", "vcoord_file_target_grid"]:
input_files.append(config_files[k])
input_files.append((config_files[k], k))
Byrnetp marked this conversation as resolved.
Show resolved Hide resolved
for k in [
"atm_core_files_input_grid",
"atm_files_input_grid",
Expand All @@ -49,9 +49,9 @@ def namelist_file(self):
grid_path = Path(config_files["data_dir_input_grid"])
v = config_files[k]
if isinstance(v, str):
input_files.append(grid_path / v)
input_files.append((grid_path / v, k))
else:
input_files.extend([grid_path / f for f in v])
input_files.extend([(grid_path / f, k) for f in v])
for k in [
"orog_files_input_grid",
"orog_files_target_grid",
Expand All @@ -60,10 +60,10 @@ def namelist_file(self):
grid_path = Path(config_files[k.replace("files", "dir")])
v = config_files[k]
if isinstance(v, str):
input_files.append(grid_path / v)
input_files.append((grid_path / v, k))
else:
input_files.extend([grid_path / f for f in v])
yield [file(Path(input_file)) for input_file in input_files]
input_files.extend([(grid_path / f, k) for f in v])
yield [file(Path(input_file), pathstr) for input_file, pathstr in input_files]
Byrnetp marked this conversation as resolved.
Show resolved Hide resolved
self._create_user_updated_config(
config_class=NMLConfig,
config_values=namelist,
Expand Down
2 changes: 1 addition & 1 deletion src/uwtools/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _set_config_block(self) -> None:
for key in self._keys:
nav.append(key)
if key not in cfg:
raise UWConfigError("Failed following YAML key(s): %s" % " -> ".join(nav))
raise UWConfigError("Failed following YAML key(s): %s" % ".".join(nav))
log.debug("Following config key '%s'", key)
cfg = cfg[key]
if not isinstance(cfg, dict):
Expand Down
6 changes: 3 additions & 3 deletions src/uwtools/tests/config/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,17 +556,17 @@ def test_realize_config_values_needed_yaml(caplog):
def test_walk_key_path_fail_bad_key_path():
with raises(UWError) as e:
tools.walk_key_path({"a": {"b": {"c": "cherry"}}}, ["a", "x"])
assert str(e.value) == "Bad config path: a -> x"
assert str(e.value) == "Bad config path: a.x"


def test_walk_key_path_fail_bad_leaf_value():
with raises(UWError) as e:
tools.walk_key_path({"a": {"b": {"c": "cherry"}}}, ["a", "b", "c"])
assert str(e.value) == "Value at a -> b -> c must be a dictionary"
assert str(e.value) == "Value at a.b.c must be a dictionary"


def test_walk_key_path_pass():
expected = ({"c": "cherry"}, "a -> b")
expected = ({"c": "cherry"}, "a.b")
assert tools.walk_key_path({"a": {"b": {"c": "cherry"}}}, ["a", "b"]) == expected


Expand Down
2 changes: 1 addition & 1 deletion src/uwtools/tests/drivers/test_chgres_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_ChgresCube_namelist_file_missing_base_file(caplog, driverobj):
driverobj._config["namelist"]["base_file"] = base_file
path = Path(refs(driverobj.namelist_file()))
assert not path.exists()
assert regex_logged(caplog, "missing.nml: State: Not Ready (external asset)")
assert regex_logged(caplog, "missing.nml (base_file): State: Not Ready (external asset)")


def test_ChgresCube_output(driverobj):
Expand Down
2 changes: 1 addition & 1 deletion src/uwtools/tests/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_Stager__config_block_fail_bad_key_path(assets, source):
config = cfgdict if source == "dict" else cfgfile
with raises(UWConfigError) as e:
ConcreteStager(target_dir=dstdir, config=config, keys=["a", "x"])
assert str(e.value) == "Failed following YAML key(s): a -> x"
assert str(e.value) == "Failed following YAML key(s): a.x"


@mark.parametrize("val", [None, True, False, "str", 42, 3.14, [], tuple()])
Expand Down
6 changes: 4 additions & 2 deletions src/uwtools/utils/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ def existing(path: Path):


@external
def file(path: Path):
def file(path: Path, context: str = ""):
"""
An existing file or symlink to an existing file.

:param path: Path to the file.
:param context: Optional additional context for the file.
"""
yield "File %s" % path
suffix = " (" + context + ")" if context else ""
Byrnetp marked this conversation as resolved.
Show resolved Hide resolved
yield "File %s%s" % (path, suffix)
Byrnetp marked this conversation as resolved.
Show resolved Hide resolved
yield asset(path, path.is_file)


Expand Down
Loading