basic structure for lazy config loading
This commit is contained in:
43
src/delta_barth/config.py
Normal file
43
src/delta_barth/config.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import dopt_basics.configs
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Config(BaseModel):
|
||||
forecast: CfgForecast
|
||||
|
||||
|
||||
class CfgForecast(BaseModel):
|
||||
threshold_month_data_points: int
|
||||
|
||||
|
||||
class LazyCfgLoader:
|
||||
def __init__(
|
||||
self,
|
||||
cfg_path: Path,
|
||||
) -> None:
|
||||
cfg_path = cfg_path.resolve()
|
||||
assert cfg_path.exists(), f"config path {cfg_path} seems not to exist"
|
||||
assert cfg_path.is_file(), f"config path {cfg_path} seems not to be a file"
|
||||
self._path = cfg_path
|
||||
self._cfg: Config | None = None
|
||||
|
||||
@property
|
||||
def path(self) -> Path:
|
||||
return self._path
|
||||
|
||||
def _load(self) -> Config:
|
||||
cfg = dopt_basics.configs.load_toml(self.path)
|
||||
|
||||
return Config(**cfg)
|
||||
|
||||
def reload(self) -> None:
|
||||
self._cfg = self._load()
|
||||
|
||||
def get(self) -> Config:
|
||||
if self._cfg is None:
|
||||
self._cfg = self._load()
|
||||
return self._cfg
|
||||
2
src/delta_barth/dopt-cfg.toml
Normal file
2
src/delta_barth/dopt-cfg.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[forecast]
|
||||
threshold_month_data_points = 28
|
||||
@@ -31,6 +31,8 @@ logger_status = logging.getLogger("delta_barth.status")
|
||||
logger_status.setLevel(logging.DEBUG)
|
||||
logger_session = logging.getLogger("delta_barth.session")
|
||||
logger_session.setLevel(logging.DEBUG)
|
||||
logger_config = logging.getLogger("delta_barth.config")
|
||||
logger_config.setLevel(logging.DEBUG)
|
||||
logger_management = logging.getLogger("delta_barth.management")
|
||||
logger_management.setLevel(logging.DEBUG)
|
||||
logger_wrapped_results = logging.getLogger("delta_barth.wrapped_results")
|
||||
|
||||
Reference in New Issue
Block a user