Skip to content

Commit

Permalink
Add option to read configuration from stdin
Browse files Browse the repository at this point in the history
If configuration filename is a hyphen "-", we attempt to parse
the input from stdin. This makes it possible to modify configuration files
with linux command line tools and pipe the results to (for example)
"anemoi-dataset create". Both linux pipes and input redirection is supported.

cat config.yaml | anemoi-datasets create - test.zarr
anemoi-datasets create - test.zarr < config.yaml

We attempt to read the contents using the supported parsers in the following
order: yaml, json, toml.

If all parsers fail, an exception is raised.
  • Loading branch information
mpartio committed Nov 21, 2024
1 parent 2837ecd commit 6981014
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/anemoi/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ def load_any_dict_format(path) -> dict:
if path.endswith(".toml"):
with open(path, "rb") as f:
return tomllib.load(f)

if path == "-":
import sys
config = sys.stdin.read()

parsers = [(yaml.safe_load, "yaml"), (json.loads, "json"), (tomllib.loads, "toml")]

for parser, parser_type in parsers:
try:
LOG.debug(f"Trying {parser_type} parser for stdin")
return parser(config)
except Exception as e:
pass

raise ValueError(f"Failed to parse configuration from stdin")

except (json.JSONDecodeError, yaml.YAMLError, tomllib.TOMLDecodeError) as e:
LOG.warning(f"Failed to parse config file {path}", exc_info=e)
raise ValueError(f"Failed to parse config file {path} [{e}]")
Expand Down

0 comments on commit 6981014

Please sign in to comment.