2021-04-25 18:27:57 +02:00
|
|
|
"""Handles parsing a config file from disk."""
|
2021-01-15 17:58:29 +01:00
|
|
|
import yaml
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import List, Union
|
2021-01-15 22:00:56 +01:00
|
|
|
from specs import Spec, DirectorySpec, VolumeSpec, ContainerSpec
|
2021-01-15 21:08:56 +01:00
|
|
|
import skeleton
|
2021-01-15 17:58:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def read_specs_file(path: Union[str, Path]) -> List[Spec]:
|
2021-04-26 17:45:19 +02:00
|
|
|
"""Read a config file and merge it with the skeleton.
|
2021-04-25 18:27:57 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
path: path to the yaml config file
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of specs, parsed from the config.
|
|
|
|
"""
|
2021-01-15 17:58:29 +01:00
|
|
|
with open(path, "r") as yaml_file:
|
2021-01-15 21:08:56 +01:00
|
|
|
data = yaml.safe_load(yaml_file)
|
2021-01-15 17:58:29 +01:00
|
|
|
|
2021-04-25 18:27:57 +02:00
|
|
|
# NOTE shouldn't this be defined as a top-level variable?
|
2021-01-15 22:00:56 +01:00
|
|
|
categories = [
|
|
|
|
("directories", DirectorySpec),
|
|
|
|
("volumes", VolumeSpec),
|
|
|
|
("containers", ContainerSpec),
|
|
|
|
]
|
2021-01-15 17:58:29 +01:00
|
|
|
|
|
|
|
specs = []
|
|
|
|
|
|
|
|
for key, class_name in categories:
|
2021-01-15 21:08:56 +01:00
|
|
|
if not data["specs"].get(key):
|
2021-01-15 17:58:29 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Check what defaults are present
|
2021-01-15 21:08:56 +01:00
|
|
|
defaults = {}
|
2021-04-25 18:27:57 +02:00
|
|
|
|
2021-01-15 17:58:29 +01:00
|
|
|
if data.get("defaults"):
|
|
|
|
if data["defaults"].get("all"):
|
2021-01-15 21:08:56 +01:00
|
|
|
defaults = skeleton.merge(defaults, data["defaults"]["all"])
|
2021-01-15 17:58:29 +01:00
|
|
|
|
|
|
|
if data["defaults"].get(key):
|
2021-01-15 21:08:56 +01:00
|
|
|
defaults = skeleton.merge(defaults, data["defaults"][key])
|
2021-01-15 17:58:29 +01:00
|
|
|
|
|
|
|
specs.extend(
|
|
|
|
[
|
2021-01-15 21:08:56 +01:00
|
|
|
class_name.from_dict(name, spec, defaults)
|
2021-01-15 17:58:29 +01:00
|
|
|
for name, spec in data["specs"][key].items()
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
return specs
|