Skip to content

Commit 4e67e51

Browse files
authored
UW-653 controller key path (#604)
1 parent 38e4c7c commit 4e67e51

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

src/uwtools/drivers/driver.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(
4949
dry_run: bool = False,
5050
key_path: Optional[list[str]] = None,
5151
schema_file: Optional[Path] = None,
52-
controller: Optional[str] = None,
52+
controller: Optional[list[str]] = None,
5353
) -> None:
5454
config_input = config if isinstance(config, YAMLConfig) else YAMLConfig(config=config)
5555
config_input.dereference(
@@ -65,8 +65,7 @@ def __init__(
6565
self._config: dict = self._config_intermediate[self.driver_name()]
6666
except KeyError as e:
6767
raise UWConfigError("Required '%s' block missing in config" % self.driver_name()) from e
68-
if controller:
69-
self._config[STR.rundir] = self._config_intermediate[controller][STR.rundir]
68+
self._delegate(controller, STR.rundir)
7069
self.schema_file = schema_file
7170
self._validate()
7271
dryrun(enable=dry_run)
@@ -167,6 +166,19 @@ def _create_user_updated_config(
167166
else:
168167
log.debug(f"Failed to validate {path}")
169168

169+
def _delegate(self, controller: Optional[list[str]], config_key: str) -> None:
170+
"""
171+
Selectively delegate config to controller.
172+
173+
:param controller: Key(s) leading to block in config controlling run-time values.
174+
:param config_key: Name of config item to delegate to controller.
175+
"""
176+
if controller:
177+
val = self._config_intermediate[controller[0]]
178+
for key in controller[1:]:
179+
val = val[key]
180+
self._config[config_key] = val[config_key]
181+
170182
# Public helper methods
171183

172184
@classmethod
@@ -241,7 +253,7 @@ def __init__(
241253
dry_run: bool = False,
242254
key_path: Optional[list[str]] = None,
243255
schema_file: Optional[Path] = None,
244-
controller: Optional[str] = None,
256+
controller: Optional[list[str]] = None,
245257
):
246258
super().__init__(
247259
cycle=cycle,
@@ -274,7 +286,7 @@ def __init__(
274286
dry_run: bool = False,
275287
key_path: Optional[list[str]] = None,
276288
schema_file: Optional[Path] = None,
277-
controller: Optional[str] = None,
289+
controller: Optional[list[str]] = None,
278290
):
279291
super().__init__(
280292
cycle=cycle,
@@ -314,7 +326,7 @@ def __init__(
314326
dry_run: bool = False,
315327
key_path: Optional[list[str]] = None,
316328
schema_file: Optional[Path] = None,
317-
controller: Optional[str] = None,
329+
controller: Optional[list[str]] = None,
318330
):
319331
super().__init__(
320332
config=config,
@@ -339,7 +351,7 @@ def __init__(
339351
key_path: Optional[list[str]] = None,
340352
batch: bool = False,
341353
schema_file: Optional[Path] = None,
342-
controller: Optional[str] = None,
354+
controller: Optional[list[str]] = None,
343355
):
344356
super().__init__(
345357
cycle=cycle,
@@ -351,8 +363,7 @@ def __init__(
351363
controller=controller,
352364
)
353365
self._batch = batch
354-
if controller:
355-
self._config[STR.execution] = self.config_full[controller][STR.execution]
366+
self._delegate(controller, STR.execution)
356367

357368
# Workflow tasks
358369

@@ -541,7 +552,7 @@ def __init__(
541552
key_path: Optional[list[str]] = None,
542553
batch: bool = False,
543554
schema_file: Optional[Path] = None,
544-
controller: Optional[str] = None,
555+
controller: Optional[list[str]] = None,
545556
):
546557
super().__init__(
547558
cycle=cycle,
@@ -576,7 +587,7 @@ def __init__(
576587
key_path: Optional[list[str]] = None,
577588
batch: bool = False,
578589
schema_file: Optional[Path] = None,
579-
controller: Optional[str] = None,
590+
controller: Optional[list[str]] = None,
580591
):
581592
super().__init__(
582593
cycle=cycle,
@@ -618,7 +629,7 @@ def __init__(
618629
key_path: Optional[list[str]] = None,
619630
batch: bool = False,
620631
schema_file: Optional[Path] = None,
621-
controller: Optional[str] = None,
632+
controller: Optional[list[str]] = None,
622633
):
623634
super().__init__(
624635
config=config,
@@ -650,7 +661,7 @@ def _add_docstring(class_: type, omit: Optional[list[str]] = None) -> None:
650661
:param key_path: Keys leading through the config to the driver's configuration block.
651662
:param batch: Run component via the batch system?
652663
:param schema_file: Path to schema file to use to validate an external driver.
653-
:param controller: Name of block in config controlling run-time values.
664+
:param controller: Key(s) leading to block in config controlling run-time values.
654665
"""
655666
setattr(
656667
class_,

src/uwtools/tests/drivers/test_driver.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_Assets_controller(config, controller_schema):
195195
with raises(UWConfigError):
196196
ConcreteAssetsTimeInvariant(config=config, schema_file=controller_schema)
197197
assert ConcreteAssetsTimeInvariant(
198-
config=config, schema_file=controller_schema, controller="controller"
198+
config=config, schema_file=controller_schema, controller=["controller"]
199199
)
200200

201201

@@ -285,6 +285,13 @@ def test_Assets__create_user_updated_config_base_file(
285285
assert updated == expected
286286

287287

288+
def test_Assets__delegate(driverobj):
289+
assert "roses" not in driverobj.config
290+
driverobj._config_intermediate["plants"] = {"flowers": {"roses": "red"}}
291+
driverobj._delegate(["plants", "flowers"], "roses")
292+
assert driverobj.config["roses"] == "red"
293+
294+
288295
def test_Assets__rundir(assetsobj):
289296
assert assetsobj.rundir == Path(assetsobj.config["rundir"])
290297

@@ -342,7 +349,7 @@ def test_Driver_controller(config, controller_schema):
342349
with raises(UWConfigError):
343350
ConcreteDriverTimeInvariant(config=config, schema_file=controller_schema)
344351
assert ConcreteDriverTimeInvariant(
345-
config=config, schema_file=controller_schema, controller="controller"
352+
config=config, schema_file=controller_schema, controller=["controller"]
346353
)
347354

348355

0 commit comments

Comments
 (0)