From ae94cd62fc12f4d37a26ff2a5a3e7da452550b26 Mon Sep 17 00:00:00 2001 From: Stefan Allius Date: Sat, 15 Jun 2024 23:23:57 +0200 Subject: [PATCH] use config validation for healthcheck --- app/src/config.py | 12 +++++++----- app/tests/test_config.py | 5 +++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/src/config.py b/app/src/config.py index e1ef749..115b1fe 100644 --- a/app/src/config.py +++ b/app/src/config.py @@ -3,6 +3,7 @@ import shutil import tomllib import logging +from typing import Tuple from schema import Schema, And, Or, Use, Optional @@ -84,7 +85,7 @@ class Config(): ) @classmethod - def class_init(cls): # pragma: no cover + def class_init(cls) -> None | str: # pragma: no cover try: # make the default config transparaent by copying it # in the config.example file @@ -94,11 +95,12 @@ class Config(): "config/config.example.toml") except Exception: pass - cls.read() + return cls.read() @classmethod - def _read_config_file(cls) -> dict: # pragma: no cover + def _read_config_file(cls) -> Tuple[dict, None | str]: # pragma: no cover usr_config = {} + err = None try: with open("config/config.toml", "rb") as f: @@ -110,7 +112,7 @@ class Config(): '\n To create the missing config.toml file, ' 'you can rename the template config.example.toml\n' ' and customize it for your scenario.\n') - return usr_config + return usr_config, err @classmethod def read(cls, path='') -> None | str: @@ -129,7 +131,7 @@ class Config(): # overwrite the default values, with values from # the config.toml file - usr_config = cls._read_config_file() + usr_config, err = cls._read_config_file() # merge the default and the user config config = def_config.copy() diff --git a/app/tests/test_config.py b/app/tests/test_config.py index 746d1d8..a9d598b 100644 --- a/app/tests/test_config.py +++ b/app/tests/test_config.py @@ -2,6 +2,7 @@ import tomllib from schema import SchemaMissingKeyError from app.src.config import Config +from typing import Tuple class TstConfig(Config): @@ -10,8 +11,8 @@ class TstConfig(Config): cls.config = cnf @classmethod - def _read_config_file(cls) -> dict: - return cls.config + def _read_config_file(cls) -> Tuple[dict, str| None]: + return cls.config, None def test_empty_config():