Skip to content
Draft
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
15 changes: 6 additions & 9 deletions src/blueapi/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,23 @@
"-c", "--config", type=Path, help="Path to configuration YAML file", multiple=True
)
@click.pass_context
def main(ctx: click.Context, config: Path | None | tuple[Path, ...]) -> None:
def main(ctx: click.Context, config: tuple[Path, ...]) -> None:
# if no command is supplied, run with the options passed

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

config_loader = ConfigLoader(ApplicationConfig)
if config is not None:
configs = (config,) if isinstance(config, Path) else config
for path in configs:
if path.exists():
config_loader.use_values_from_yaml(path)
else:
raise FileNotFoundError(f"Cannot find file: {path}")
try:
config_loader.use_values_from_yaml(*config)
except FileNotFoundError as fnfe:
raise ClickException(f"Config file not found: {fnfe.filename}") from fnfe

ctx.ensure_object(dict)
loaded_config: ApplicationConfig = config_loader.load()

set_up_logging(loaded_config.logging)

ctx.ensure_object(dict)
ctx.obj["config"] = loaded_config

if ctx.invoked_subcommand is None:
Expand Down
16 changes: 11 additions & 5 deletions src/blueapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,19 +358,25 @@ def recursively_update_map(old: dict[str, Any], new: Mapping[str, Any]) -> None:

recursively_update_map(self._values, values)

def use_values_from_yaml(self, path: Path) -> None:
def use_values_from_yaml(self, *paths: Path) -> None:
"""
Use all values provided in a YAML/JSON file in the
Use all values provided in a YAML/JSON files in the
config, override any defaults and values set by
previous calls into this class.

Args:
path (Path): Path to YAML/JSON file
"""

with path.open("r") as stream:
values = yaml.load(stream, yaml.Loader)
self.use_values(values)
# Split reading and loading so that a missing file does not leave
# config in partially loaded state
configs = []
for path in paths:
with path.open("r") as stream:
configs.append(yaml.load(stream, yaml.Loader))

for values in configs:
self.use_values(values)

def load(self) -> C:
"""
Expand Down
Loading