From c37753e0cf1e20c693c28feeb671ea520cc502b3 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Sat, 27 Jul 2024 16:23:47 +0200 Subject: [PATCH] refactor: slightly clean up config parsing --- nyacme/config.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/nyacme/config.py b/nyacme/config.py index dd6d7ce..023dd00 100644 --- a/nyacme/config.py +++ b/nyacme/config.py @@ -1,7 +1,7 @@ import logging import os import sys -from typing import Optional +from typing import Any, Optional import tomllib @@ -33,6 +33,20 @@ class Config: return self.secrets[handler] +def config_parse_dict(raw_conf: dict[str, Any], key: str) -> dict[str, str]: + if key not in raw_conf: + log.error(f'missing "{key}" in config') + sys.exit(1) + + for k, v in raw_conf[key].items(): + assert isinstance(k, str), f'"{k}" is not a string' + assert isinstance(v, str), f'"{k}" value "{v}" is not a string' + + result: dict[str, str] = raw_conf[key] + + return result + + def read_config(path: Optional[str]) -> Config: if not path: # should be here only when running from hook @@ -47,25 +61,8 @@ def read_config(path: Optional[str]) -> Config: c = Config() - if 'domains' not in raw_conf: - log.error('missing "domains"') - sys.exit(1) - - for k, v in raw_conf['domains'].items(): - assert isinstance(k, str), f'domain "{k}" is not a string' - assert isinstance(v, str), f'domain "{k}" handler {v} is not a string' - - c.domains = raw_conf['domains'] - - if 'secrets' not in raw_conf: - log.error('missing "secrets"') - sys.exit(1) - - for k, v in raw_conf['secrets'].items(): - assert isinstance(k, str), f'secret key "{k}" is not a string' - assert isinstance(v, str), f'secret "{k}" value {v} is not a string' - - c.secrets = raw_conf['secrets'] + c.domains = config_parse_dict(raw_conf, 'domains') + c.secrets = config_parse_dict(raw_conf, 'secrets') post_acquire = [] if 'post_acquire' in raw_conf: