-
Notifications
You must be signed in to change notification settings - Fork 2
/
yaml.py
34 lines (29 loc) · 1.29 KB
/
yaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import logging
from pathlib import Path
from ruamel.yaml import YAML
from scos_actions.settings import ACTION_DEFINITIONS_DIR
logger = logging.getLogger(__name__)
def load_from_yaml(action_classes, yaml_dir: Path = ACTION_DEFINITIONS_DIR):
"""Load any YAML files in yaml_dir."""
parsed_actions = {}
yaml = YAML(typ="safe")
yaml_path = Path(yaml_dir)
for yaml_file in yaml_path.glob("*.yml"):
definition = yaml.load(yaml_file)
for class_name, parameters in definition.items():
try:
logger.debug(f"Attempting to configure: {class_name}")
action = action_classes[class_name](parameters=parameters)
parsed_actions[action.name] = action
except KeyError as exc:
err = "Nonexistent action class name {!r} referenced in {!r}"
logger.exception(err.format(class_name, yaml_file.name))
raise exc
except TypeError as exc:
err = "Invalid parameter list {!r} referenced in {!r}"
logger.exception(err.format(parameters, yaml_file.name))
raise exc
except Exception as exc:
logger.exception("Unable to load yaml:", class_name, parameters)
raise exc
return parsed_actions