From 9abc444868b83d39d9c1c46904b45ebf35a8f24c Mon Sep 17 00:00:00 2001 From: Mose Mueller Date: Mon, 31 Jul 2023 16:16:03 +0200 Subject: [PATCH] updating config to new confz version, db connections take config folder as argument --- icon_service_base/database/config.py | 23 ++++--------------- .../database/influxdb_connection.py | 13 +++++++---- .../database/postgres_connection.py | 18 +++++++++++---- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/icon_service_base/database/config.py b/icon_service_base/database/config.py index 9c15bf4..6ef8560 100644 --- a/icon_service_base/database/config.py +++ b/icon_service_base/database/config.py @@ -1,37 +1,24 @@ -from pathlib import Path from typing import Literal -from confz import ConfZ, ConfZEnvSource, ConfZFileSource +from confz import BaseConfig, EnvSource from pydantic import AnyUrl, SecretStr -CONFIG_DIR = Path(__file__).parent.parent.parent.resolve() / "config" - -class OperationMode(ConfZ): # type: ignore +class OperationMode(BaseConfig): # type: ignore environment: Literal["development"] | Literal["production"] = "development" - CONFIG_SOURCES = ConfZEnvSource(allow=["ENVIRONMENT"]) + CONFIG_SOURCES = EnvSource(allow=["ENVIRONMENT"]) -class PostgreSQLConfig(ConfZ): # type: ignore +class PostgreSQLConfig(BaseConfig): # type: ignore host: AnyUrl port: int database: str user: str password: SecretStr - CONFIG_SOURCES = [ - ConfZFileSource(f"{CONFIG_DIR}/postgres_{OperationMode().environment}.yaml"), - ConfZEnvSource(prefix="POSTGRES_", allow=["user", "password"], file=".env"), - ] - -class InfluxDBConfig(ConfZ): # type: ignore +class InfluxDBConfig(BaseConfig): # type: ignore url: AnyUrl org: str token: SecretStr - - CONFIG_SOURCES = [ - ConfZFileSource(f"{CONFIG_DIR}/influxdb_config.yaml"), - ConfZEnvSource(prefix="INFLUXDB_V2_", allow=["token"], file=".env"), - ] diff --git a/icon_service_base/database/influxdb_connection.py b/icon_service_base/database/influxdb_connection.py index 73ae4de..73c1da2 100644 --- a/icon_service_base/database/influxdb_connection.py +++ b/icon_service_base/database/influxdb_connection.py @@ -1,7 +1,9 @@ from __future__ import annotations +from pathlib import Path from types import TracebackType +from confz import FileSource from influxdb_client import ( # type: ignore Bucket, BucketRetentionRules, @@ -47,10 +49,13 @@ class InfluxDBConnection: ``` """ - def __init__(self) -> None: - self.url = InfluxDBConfig().url - self.token = str(InfluxDBConfig().token) - self.org = InfluxDBConfig().org + def __init__(self, config_folder: Path | str) -> None: + self._config = InfluxDBConfig( + config_sources=FileSource(Path(config_folder) / "influxdb_config.yaml") + ) + self.url = self._config.url + self.token = str(self._config.token) + self.org = self._config.org self.client: InfluxDBClient self.write_api: WriteApi self.buckets_api: BucketsApi | None = None diff --git a/icon_service_base/database/postgres_connection.py b/icon_service_base/database/postgres_connection.py index 09feb7b..01ce670 100644 --- a/icon_service_base/database/postgres_connection.py +++ b/icon_service_base/database/postgres_connection.py @@ -3,13 +3,16 @@ from __future__ import annotations import datetime import json import re +from pathlib import Path from types import TracebackType from typing import Any +from confz import FileSource from dateutil.parser import ParserError, parse # type: ignore +from loguru import logger from sqlmodel import Session, SQLModel, create_engine -from icon_service_base.database.config import PostgreSQLConfig +from icon_service_base.database.config import OperationMode, PostgreSQLConfig def json_loads_or_return_input(input_string: str) -> dict[str, Any] | Any: @@ -125,14 +128,19 @@ class PostgresDatabaseSession(Session): ``` """ - def __init__(self) -> None: + def __init__(self, config_folder: Path | str) -> None: """Initializes a new session bound to the database engine.""" + self._config = PostgreSQLConfig( + config_sources=FileSource( + Path(config_folder) / f"postgres_{OperationMode().environment}.yaml" + ) + ) super().__init__( bind=create_engine( - f"postgresql://{PostgreSQLConfig().user}:{PostgreSQLConfig().password}@" - f"{PostgreSQLConfig().host.host}:{PostgreSQLConfig().port}/" - f"{PostgreSQLConfig().database}", + f"postgresql://{self._config.user}:{self._config.password}@" + f"{self._config.host.host}:{self._config.port}/" + f"{self._config.database}", echo=False, json_serializer=json_dumps, json_deserializer=deserialize_json_dict,