Skip to content
Merged
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
6 changes: 4 additions & 2 deletions src/legenddataflowscripts/utils/cfgtools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import copy
from collections.abc import Iterable, Mapping
from pathlib import Path

Expand Down Expand Up @@ -133,14 +134,15 @@ def get_rule_config(configs_path, rule_name, timestamp, datatype):

Wraps ``TextDB(...).on(...)["snakemake_rules"][rule_name]`` so that a
missing key names the rule, timestamp, datatype and config path instead
of raising a bare :class:`KeyError`.
of raising a bare :class:`KeyError`. Returns a writable deep copy, since
``TextDB.on()`` results are shared and read-only.
"""
if not Path(configs_path).is_dir():
msg = f"config directory {configs_path} does not exist"
raise FileNotFoundError(msg)
configs = TextDB(configs_path, lazy=True).on(timestamp, category=datatype)
try:
return configs["snakemake_rules"][rule_name]
return copy.deepcopy(configs["snakemake_rules"][rule_name])
except KeyError as err:
msg = (
f"config resolved from {configs_path} for timestamp {timestamp} "
Expand Down
6 changes: 6 additions & 0 deletions tests/test_utils_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ def test_get_rule_config(configs_tree):
config = get_rule_config(configs_tree, "tier_tcm", "20230201T000000Z", "cal")
assert config["inputs"]["config"] == "c.yaml"

# writable, and edits don't leak into the next call
config["inputs"]["config"] = "other.yaml"
config["inputs"].setdefault("extra", []).append(1)
again = get_rule_config(configs_tree, "tier_tcm", "20230201T000000Z", "cal")
assert again["inputs"] == {"config": "c.yaml"}

with pytest.raises(KeyError, match=r"no snakemake_rules\.tier_dsp entry"):
get_rule_config(configs_tree, "tier_dsp", "20230201T000000Z", "cal")

Expand Down
Loading