enhanced config handling
This commit is contained in:
parent
214659c7f1
commit
a1057fc78b
@ -1,11 +1,19 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import enum
|
import enum
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Final
|
from typing import Final
|
||||||
|
|
||||||
|
import psutil
|
||||||
|
|
||||||
|
import delta_barth._env
|
||||||
from delta_barth.types import DualDict, HttpContentHeaders
|
from delta_barth.types import DualDict, HttpContentHeaders
|
||||||
|
|
||||||
# ** config
|
# ** config
|
||||||
CFG_FILENAME: Final[str] = "dopt-cfg.toml"
|
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
|
||||||
lib_path = Path(__file__).parent
|
lib_path = Path(__file__).parent
|
||||||
@ -14,12 +22,13 @@ LIB_PATH: Final[Path] = lib_path
|
|||||||
dummy_data_pth = LIB_PATH / "_dummy_data"
|
dummy_data_pth = LIB_PATH / "_dummy_data"
|
||||||
assert dummy_data_pth.exists(), f"dummy data path not found: {dummy_data_pth}"
|
assert dummy_data_pth.exists(), f"dummy data path not found: {dummy_data_pth}"
|
||||||
DUMMY_DATA_PATH: Final[Path] = 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
|
# ** databases
|
||||||
DB_ECHO: Final[bool] = True
|
DB_ECHO: Final[bool] = True
|
||||||
|
|||||||
@ -6,14 +6,13 @@ from pathlib import Path
|
|||||||
from time import gmtime
|
from time import gmtime
|
||||||
from typing import Final
|
from typing import Final
|
||||||
|
|
||||||
from delta_barth.constants import (
|
|
||||||
ENABLE_LOGGING,
|
|
||||||
LOG_FILENAME,
|
|
||||||
LOGGING_TO_FILE,
|
|
||||||
LOGGING_TO_STDERR,
|
|
||||||
)
|
|
||||||
|
|
||||||
# ** config
|
# ** 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
|
logging.Formatter.converter = gmtime
|
||||||
LOG_FMT: Final[str] = "%(asctime)s | lang_main:%(module)s:%(levelname)s | %(message)s"
|
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"
|
LOG_DATE_FMT: Final[str] = "%Y-%m-%d %H:%M:%S +0000"
|
||||||
|
|||||||
@ -19,6 +19,7 @@ from delta_barth.config import LazyCfgLoader
|
|||||||
from delta_barth.constants import (
|
from delta_barth.constants import (
|
||||||
API_CON_TIMEOUT,
|
API_CON_TIMEOUT,
|
||||||
CFG_FILENAME,
|
CFG_FILENAME,
|
||||||
|
CFG_HOT_RELOAD,
|
||||||
DB_ECHO,
|
DB_ECHO,
|
||||||
LIB_PATH,
|
LIB_PATH,
|
||||||
)
|
)
|
||||||
@ -97,6 +98,8 @@ class Session:
|
|||||||
@property
|
@property
|
||||||
def cfg(self) -> Config:
|
def cfg(self) -> Config:
|
||||||
assert self._cfg is not None, "tried to access not set config from session"
|
assert self._cfg is not None, "tried to access not set config from session"
|
||||||
|
if CFG_HOT_RELOAD:
|
||||||
|
self.reload_cfg()
|
||||||
return self._cfg
|
return self._cfg
|
||||||
|
|
||||||
def _setup_config(self) -> None:
|
def _setup_config(self) -> None:
|
||||||
@ -108,6 +111,11 @@ class Session:
|
|||||||
self._cfg = self._cfg_loader.get()
|
self._cfg = self._cfg_loader.get()
|
||||||
logger.info("[SESSION] Successfully read and setup config")
|
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
|
@property
|
||||||
def db_engine(self) -> sql.Engine:
|
def db_engine(self) -> sql.Engine:
|
||||||
assert self._db_engine is not None, "accessed database engine not set"
|
assert self._db_engine is not None, "accessed database engine not set"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user