gitea-hooks/gitea_hooks/conf.py

34 lines
1.1 KiB
Python

import yaml
class BadConfig(Exception):
"""Raised when the configuration is incorrect"""
class Configuration:
"""Parses the configuration file, and caches it.
The configuration is a YAML file.
"""
def __init__(self):
try:
with open("./config.yml", "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))