|
1 | 1 | import io
|
2 | 2 | import pathlib
|
3 | 3 | import re
|
| 4 | +import sys |
4 | 5 | from typing import Any, Dict, Union
|
5 | 6 |
|
6 | 7 | from expandvars import UnboundVariable, expandvars
|
@@ -54,26 +55,32 @@ def load_config_file(
|
54 | 55 | config_file: Union[pathlib.Path, str],
|
55 | 56 | squirrel_original_config: bool = False,
|
56 | 57 | squirrel_field: str = "__orig_config",
|
| 58 | + allow_stdin: bool = False, |
57 | 59 | ) -> dict:
|
58 |
| - if isinstance(config_file, str): |
59 |
| - config_file = pathlib.Path(config_file) |
60 |
| - if not config_file.is_file(): |
61 |
| - raise ConfigurationError(f"Cannot open config file {config_file}") |
62 |
| - |
63 | 60 | config_mech: ConfigurationMechanism
|
64 |
| - if config_file.suffix in [".yaml", ".yml"]: |
| 61 | + if allow_stdin and config_file == "-": |
| 62 | + # If we're reading from stdin, we assume that the input is a YAML file. |
65 | 63 | config_mech = YamlConfigurationMechanism()
|
66 |
| - elif config_file.suffix == ".toml": |
67 |
| - config_mech = TomlConfigurationMechanism() |
| 64 | + raw_config_file = sys.stdin.read() |
68 | 65 | else:
|
69 |
| - raise ConfigurationError( |
70 |
| - "Only .toml and .yml are supported. Cannot process file type {}".format( |
71 |
| - config_file.suffix |
| 66 | + if isinstance(config_file, str): |
| 67 | + config_file = pathlib.Path(config_file) |
| 68 | + if not config_file.is_file(): |
| 69 | + raise ConfigurationError(f"Cannot open config file {config_file}") |
| 70 | + |
| 71 | + if config_file.suffix in {".yaml", ".yml"}: |
| 72 | + config_mech = YamlConfigurationMechanism() |
| 73 | + elif config_file.suffix == ".toml": |
| 74 | + config_mech = TomlConfigurationMechanism() |
| 75 | + else: |
| 76 | + raise ConfigurationError( |
| 77 | + "Only .toml and .yml are supported. Cannot process file type {}".format( |
| 78 | + config_file.suffix |
| 79 | + ) |
72 | 80 | )
|
73 |
| - ) |
74 | 81 |
|
75 |
| - with config_file.open() as raw_config_fp: |
76 |
| - raw_config_file = raw_config_fp.read() |
| 82 | + raw_config_file = config_file.read_text() |
| 83 | + |
77 | 84 | config_fp = io.StringIO(raw_config_file)
|
78 | 85 | raw_config = config_mech.load_config(config_fp)
|
79 | 86 | config = resolve_env_variables(raw_config)
|
|
0 commit comments