server-personification/pers/load.py

30 lines
829 B
Python

import sys
from pathlib import Path
import yaml
def load_config(config_path: Path, required_keys: list = None) -> dict:
if not config_path.exists():
print(f'Config file does not exist: "{config_path}"')
sys.exit(1)
with open(config_path, 'r') as file:
try:
config = yaml.safe_load(file)
except Exception as e:
print(f'Failed to load config file "{config_path.name}" - {e.__class__.__name__} - ', e)
sys.exit(1)
if not config:
print(f'Config file "{config_path.name}" is empty.')
sys.exit(1)
keys_present = list(config.keys())
for key in required_keys:
if key not in keys_present:
print(f'Config file "{config_path.name}" is missing required key: "{key}"')
sys.exit(1)
return config