enhanced config handling

This commit is contained in:
Florian Förster 2025-04-17 11:53:59 +02:00
parent 214659c7f1
commit a1057fc78b
3 changed files with 28 additions and 12 deletions

View File

@ -1,11 +1,19 @@
from __future__ import annotations
import enum
from pathlib import Path
from typing import Final
import psutil
import delta_barth._env
from delta_barth.types import DualDict, HttpContentHeaders
# ** config
CFG_FILENAME: Final[str] = "dopt-cfg.toml"
CFG_HOT_RELOAD: Final[bool] = True
cpu_count = psutil.cpu_count(logical=False)
MAX_NUM_WORKERS: Final[int] = (cpu_count - 1) if cpu_count is not None else 3
# ** lib path
lib_path = Path(__file__).parent
@ -14,12 +22,13 @@ LIB_PATH: Final[Path] = lib_path
dummy_data_pth = LIB_PATH / "_dummy_data"
assert dummy_data_pth.exists(), f"dummy data path not found: {dummy_data_pth}"
DUMMY_DATA_PATH: Final[Path] = dummy_data_pth
# ** runtime and deployment status
RUNTIME_PATH: Final[Path | None] = delta_barth._env.prepare_env(LIB_PATH)
deployment_status: bool = False
if RUNTIME_PATH is not None:
deployment_status = True
DEPLOYMENT_STATUS: Final[bool] = deployment_status
# ** logging
ENABLE_LOGGING: Final[bool] = True
LOGGING_TO_FILE: Final[bool] = True
LOGGING_TO_STDERR: Final[bool] = False
LOG_FILENAME: Final[str] = "dopt-delbar.log"
# ** databases
DB_ECHO: Final[bool] = True

View File

@ -6,14 +6,13 @@ from pathlib import Path
from time import gmtime
from typing import Final
from delta_barth.constants import (
ENABLE_LOGGING,
LOG_FILENAME,
LOGGING_TO_FILE,
LOGGING_TO_STDERR,
)
# ** config
# ** logging
ENABLE_LOGGING: Final[bool] = True
LOGGING_TO_FILE: Final[bool] = True
LOGGING_TO_STDERR: Final[bool] = False
LOG_FILENAME: Final[str] = "dopt-delbar.log"
logging.Formatter.converter = gmtime
LOG_FMT: Final[str] = "%(asctime)s | lang_main:%(module)s:%(levelname)s | %(message)s"
LOG_DATE_FMT: Final[str] = "%Y-%m-%d %H:%M:%S +0000"

View File

@ -19,6 +19,7 @@ from delta_barth.config import LazyCfgLoader
from delta_barth.constants import (
API_CON_TIMEOUT,
CFG_FILENAME,
CFG_HOT_RELOAD,
DB_ECHO,
LIB_PATH,
)
@ -97,6 +98,8 @@ class Session:
@property
def cfg(self) -> Config:
assert self._cfg is not None, "tried to access not set config from session"
if CFG_HOT_RELOAD:
self.reload_cfg()
return self._cfg
def _setup_config(self) -> None:
@ -108,6 +111,11 @@ class Session:
self._cfg = self._cfg_loader.get()
logger.info("[SESSION] Successfully read and setup config")
def reload_cfg(self) -> None:
assert self._cfg_loader is not None, "tried reloading with no CFG loader intialised"
self._cfg_loader.reload()
self._cfg = self._cfg_loader.get()
@property
def db_engine(self) -> sql.Engine:
assert self._db_engine is not None, "accessed database engine not set"