Skip to content

Commit

Permalink
Feature request: Add option to read configuration from stdin (#59)
Browse files Browse the repository at this point in the history
* Add option to read configuration from stdin

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
mpartio and pre-commit-ci[bot] authored Dec 13, 2024
1 parent 5a7c196 commit a31d762
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/anemoi/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,23 @@ 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:
pass

raise ValueError("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 a31d762

Please sign in to comment.