backup-tool/app/parser.py

38 lines
1.0 KiB
Python
Raw Normal View History

from __future__ import annotations
import yaml
from pathlib import Path
from typing import List, Union
2021-01-15 21:47:09 +01:00
from specs import Spec, DirectorySpec, VolumeSpec
2021-01-15 21:08:56 +01:00
import skeleton
def read_specs_file(path: Union[str, Path]) -> List[Spec]:
with open(path, "r") as yaml_file:
2021-01-15 21:08:56 +01:00
data = yaml.safe_load(yaml_file)
2021-01-15 21:47:09 +01:00
categories = [("directories", DirectorySpec), ("volumes", VolumeSpec)]
specs = []
for key, class_name in categories:
2021-01-15 21:08:56 +01:00
if not data["specs"].get(key):
continue
# Check what defaults are present
2021-01-15 21:08:56 +01:00
defaults = {}
if data.get("defaults"):
if data["defaults"].get("all"):
2021-01-15 21:08:56 +01:00
defaults = skeleton.merge(defaults, data["defaults"]["all"])
if data["defaults"].get(key):
2021-01-15 21:08:56 +01:00
defaults = skeleton.merge(defaults, data["defaults"][key])
specs.extend(
[
2021-01-15 21:08:56 +01:00
class_name.from_dict(name, spec, defaults)
for name, spec in data["specs"][key].items()
]
)
return specs