feat: automatically looking for "database_config" dir

This commit is contained in:
Mose Mueller 2023-08-02 09:03:57 +02:00
parent 9abc444868
commit 404375f251

View File

@ -1,9 +1,31 @@
from typing import Literal
from pathlib import Path
from typing import Literal, Optional
from confz import BaseConfig, EnvSource
from confz import BaseConfig, EnvSource, FileSource
from loguru import logger
from pydantic import AnyUrl, SecretStr
def find_dir_upwards(start_dir: Path, targets: list[str]) -> Path | None:
for parent in start_dir.parents:
for target in targets:
if (parent / target).is_dir():
return parent / target
return None
# we expect the database_config directory in the root directory of any module installing
# this package.
VENV_DIR = find_dir_upwards(Path(__file__).resolve(), [".venv", "venv"])
CONFIG_DIR: Optional[Path] = None
if VENV_DIR is not None:
CONFIG_DIR = VENV_DIR.parent / "database_config"
if not VENV_DIR.exists():
CONFIG_DIR = None
else:
logger.debug(CONFIG_DIR)
class OperationMode(BaseConfig): # type: ignore
environment: Literal["development"] | Literal["production"] = "development"
@ -17,8 +39,16 @@ class PostgreSQLConfig(BaseConfig): # type: ignore
user: str
password: SecretStr
if CONFIG_DIR:
CONFIG_SOURCES = FileSource(
CONFIG_DIR / f"postgres_{OperationMode().environment}.yaml"
)
class InfluxDBConfig(BaseConfig): # type: ignore
url: AnyUrl
org: str
token: SecretStr
if CONFIG_DIR:
CONFIG_SOURCES = FileSource(CONFIG_DIR / "influxdb_config.yaml")