Skip to content

Commit b0c8e39

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
sambacc: add support for reading YAML formatted configs
The YAML format is extremely popular and much easier to write by hand than JSON. Add support for YAML based configuration files to sambacc. Signed-off-by: John Mulligan <[email protected]>
1 parent b4fafb5 commit b0c8e39

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

sambacc/config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
class ConfigFormat(enum.Enum):
5454
JSON = "json"
5555
TOML = "toml"
56+
YAML = "yaml"
5657

5758

5859
class ValidationUnsupported(Exception):
@@ -82,9 +83,19 @@ def _load_toml(source: typing.IO) -> JSONData:
8283
raise ConfigFormatUnsupported(ConfigFormat.TOML)
8384

8485

86+
def _load_yaml(source: typing.IO) -> JSONData:
87+
try:
88+
import yaml
89+
except ImportError:
90+
raise ConfigFormatUnsupported(ConfigFormat.YAML)
91+
return yaml.safe_load(source) or {}
92+
93+
8594
def _detect_format(fname: str) -> ConfigFormat:
8695
if fname.endswith(".toml"):
8796
return ConfigFormat.TOML
97+
if fname.endswith((".yaml", ".yml")):
98+
return ConfigFormat.YAML
8899
return ConfigFormat.JSON
89100

90101

@@ -198,6 +209,8 @@ def load(
198209
config_format = config_format or ConfigFormat.JSON
199210
if config_format == ConfigFormat.TOML:
200211
data = _load_toml(source)
212+
elif config_format == ConfigFormat.YAML:
213+
data = _load_yaml(source)
201214
else:
202215
data = json.load(source)
203216
_check_config_valid(

0 commit comments

Comments
 (0)