use config validation for healthcheck

This commit is contained in:
Stefan Allius
2024-06-15 23:23:57 +02:00
parent a16a19cc2c
commit ae94cd62fc
2 changed files with 10 additions and 7 deletions

View File

@@ -3,6 +3,7 @@
import shutil import shutil
import tomllib import tomllib
import logging import logging
from typing import Tuple
from schema import Schema, And, Or, Use, Optional from schema import Schema, And, Or, Use, Optional
@@ -84,7 +85,7 @@ class Config():
) )
@classmethod @classmethod
def class_init(cls): # pragma: no cover def class_init(cls) -> None | str: # pragma: no cover
try: try:
# make the default config transparaent by copying it # make the default config transparaent by copying it
# in the config.example file # in the config.example file
@@ -94,11 +95,12 @@ class Config():
"config/config.example.toml") "config/config.example.toml")
except Exception: except Exception:
pass pass
cls.read() return cls.read()
@classmethod @classmethod
def _read_config_file(cls) -> dict: # pragma: no cover def _read_config_file(cls) -> Tuple[dict, None | str]: # pragma: no cover
usr_config = {} usr_config = {}
err = None
try: try:
with open("config/config.toml", "rb") as f: with open("config/config.toml", "rb") as f:
@@ -110,7 +112,7 @@ class Config():
'\n To create the missing config.toml file, ' '\n To create the missing config.toml file, '
'you can rename the template config.example.toml\n' 'you can rename the template config.example.toml\n'
' and customize it for your scenario.\n') ' and customize it for your scenario.\n')
return usr_config return usr_config, err
@classmethod @classmethod
def read(cls, path='') -> None | str: def read(cls, path='') -> None | str:
@@ -129,7 +131,7 @@ class Config():
# overwrite the default values, with values from # overwrite the default values, with values from
# the config.toml file # the config.toml file
usr_config = cls._read_config_file() usr_config, err = cls._read_config_file()
# merge the default and the user config # merge the default and the user config
config = def_config.copy() config = def_config.copy()

View File

@@ -2,6 +2,7 @@
import tomllib import tomllib
from schema import SchemaMissingKeyError from schema import SchemaMissingKeyError
from app.src.config import Config from app.src.config import Config
from typing import Tuple
class TstConfig(Config): class TstConfig(Config):
@@ -10,8 +11,8 @@ class TstConfig(Config):
cls.config = cnf cls.config = cnf
@classmethod @classmethod
def _read_config_file(cls) -> dict: def _read_config_file(cls) -> Tuple[dict, str| None]:
return cls.config return cls.config, None
def test_empty_config(): def test_empty_config():