Skip to content

Commit b53155d

Browse files
committed
Move config loading from CLI into ConfigLoader
1 parent fc472d9 commit b53155d

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/blueapi/cli/cli.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,23 @@ def is_str_dict(val: Any) -> TypeGuard[TaskParameters]:
8383
"-c", "--config", type=Path, help="Path to configuration YAML file", multiple=True
8484
)
8585
@click.pass_context
86-
def main(ctx: click.Context, config: Path | None | tuple[Path, ...]) -> None:
86+
def main(ctx: click.Context, config: tuple[Path, ...]) -> None:
8787
# if no command is supplied, run with the options passed
8888

8989
# Set umask to DLS standard
9090
os.umask(stat.S_IWOTH)
9191

9292
config_loader = ConfigLoader(ApplicationConfig)
93-
if config is not None:
94-
configs = (config,) if isinstance(config, Path) else config
95-
for path in configs:
96-
if path.exists():
97-
config_loader.use_values_from_yaml(path)
98-
else:
99-
raise FileNotFoundError(f"Cannot find file: {path}")
93+
try:
94+
config_loader.use_values_from_yaml(*config)
95+
except FileNotFoundError as fnfe:
96+
raise ClickException(f"Config file not found: {fnfe.filename}") from fnfe
10097

101-
ctx.ensure_object(dict)
10298
loaded_config: ApplicationConfig = config_loader.load()
10399

104100
set_up_logging(loaded_config.logging)
105101

102+
ctx.ensure_object(dict)
106103
ctx.obj["config"] = loaded_config
107104

108105
if ctx.invoked_subcommand is None:

src/blueapi/config.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,19 +358,25 @@ def recursively_update_map(old: dict[str, Any], new: Mapping[str, Any]) -> None:
358358

359359
recursively_update_map(self._values, values)
360360

361-
def use_values_from_yaml(self, path: Path) -> None:
361+
def use_values_from_yaml(self, *paths: Path) -> None:
362362
"""
363-
Use all values provided in a YAML/JSON file in the
363+
Use all values provided in a YAML/JSON files in the
364364
config, override any defaults and values set by
365365
previous calls into this class.
366366
367367
Args:
368368
path (Path): Path to YAML/JSON file
369369
"""
370370

371-
with path.open("r") as stream:
372-
values = yaml.load(stream, yaml.Loader)
373-
self.use_values(values)
371+
# Split reading and loading so that a missing file does not leave
372+
# config in partially loaded state
373+
configs = []
374+
for path in paths:
375+
with path.open("r") as stream:
376+
configs.append(yaml.load(stream, yaml.Loader))
377+
378+
for values in configs:
379+
self.use_values(values)
374380

375381
def load(self) -> C:
376382
"""

0 commit comments

Comments
 (0)