From 404375f251d7bdd22023eeaece6889ace3b386dd Mon Sep 17 00:00:00 2001 From: Mose Mueller Date: Wed, 2 Aug 2023 09:03:57 +0200 Subject: [PATCH] feat: automatically looking for "database_config" dir --- icon_service_base/database/config.py | 34 ++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/icon_service_base/database/config.py b/icon_service_base/database/config.py index 6ef8560..c675444 100644 --- a/icon_service_base/database/config.py +++ b/icon_service_base/database/config.py @@ -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")