import yaml from pathlib import Path class BadConfig(Exception): """Raised when the configuration is incorrect""" class Configuration: """Parses the configuration file, and caches it. The configuration is a YAML file. """ BASE_CONFIG_PATH = Path.home() / "config.yml" def __init__(self): try: with self.BASE_CONFIG_PATH.open("r") as handle: self.raw = yaml.safe_load(handle) except yaml.YAMLError as exn: raise BadConfig("Cannot parse yaml") from exn except FileNotFoundError as exn: raise BadConfig("Configuration file not found") from exn except PermissionError as exn: raise BadConfig("Configuration file not readable") from exn self._healthcheck() def _healthcheck(self): """Checks that the configuration is ok""" if "fifo_hooks" not in self.raw: raise BadConfig("No `fifo_hooks`") for hook, data in self.raw["fifo_hooks"].items(): for mandatory in ("secret", "fifo_path"): if mandatory not in data: raise BadConfig("Fifo hook {}: no `{}`".format(hook, mandatory))