updating config to new confz version, db connections take config folder as argument

This commit is contained in:
Mose Mueller 2023-07-31 16:16:03 +02:00
parent 007f1156f7
commit 9abc444868
3 changed files with 27 additions and 27 deletions

View File

@ -1,37 +1,24 @@
from pathlib import Path
from typing import Literal from typing import Literal
from confz import ConfZ, ConfZEnvSource, ConfZFileSource from confz import BaseConfig, EnvSource
from pydantic import AnyUrl, SecretStr from pydantic import AnyUrl, SecretStr
CONFIG_DIR = Path(__file__).parent.parent.parent.resolve() / "config"
class OperationMode(BaseConfig): # type: ignore
class OperationMode(ConfZ): # type: ignore
environment: Literal["development"] | Literal["production"] = "development" 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 host: AnyUrl
port: int port: int
database: str database: str
user: str user: str
password: SecretStr password: SecretStr
CONFIG_SOURCES = [
ConfZFileSource(f"{CONFIG_DIR}/postgres_{OperationMode().environment}.yaml"),
ConfZEnvSource(prefix="POSTGRES_", allow=["user", "password"], file=".env"),
]
class InfluxDBConfig(BaseConfig): # type: ignore
class InfluxDBConfig(ConfZ): # type: ignore
url: AnyUrl url: AnyUrl
org: str org: str
token: SecretStr token: SecretStr
CONFIG_SOURCES = [
ConfZFileSource(f"{CONFIG_DIR}/influxdb_config.yaml"),
ConfZEnvSource(prefix="INFLUXDB_V2_", allow=["token"], file=".env"),
]

View File

@ -1,7 +1,9 @@
from __future__ import annotations from __future__ import annotations
from pathlib import Path
from types import TracebackType from types import TracebackType
from confz import FileSource
from influxdb_client import ( # type: ignore from influxdb_client import ( # type: ignore
Bucket, Bucket,
BucketRetentionRules, BucketRetentionRules,
@ -47,10 +49,13 @@ class InfluxDBConnection:
``` ```
""" """
def __init__(self) -> None: def __init__(self, config_folder: Path | str) -> None:
self.url = InfluxDBConfig().url self._config = InfluxDBConfig(
self.token = str(InfluxDBConfig().token) config_sources=FileSource(Path(config_folder) / "influxdb_config.yaml")
self.org = InfluxDBConfig().org )
self.url = self._config.url
self.token = str(self._config.token)
self.org = self._config.org
self.client: InfluxDBClient self.client: InfluxDBClient
self.write_api: WriteApi self.write_api: WriteApi
self.buckets_api: BucketsApi | None = None self.buckets_api: BucketsApi | None = None

View File

@ -3,13 +3,16 @@ from __future__ import annotations
import datetime import datetime
import json import json
import re import re
from pathlib import Path
from types import TracebackType from types import TracebackType
from typing import Any from typing import Any
from confz import FileSource
from dateutil.parser import ParserError, parse # type: ignore from dateutil.parser import ParserError, parse # type: ignore
from loguru import logger
from sqlmodel import Session, SQLModel, create_engine 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: 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.""" """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__( super().__init__(
bind=create_engine( bind=create_engine(
f"postgresql://{PostgreSQLConfig().user}:{PostgreSQLConfig().password}@" f"postgresql://{self._config.user}:{self._config.password}@"
f"{PostgreSQLConfig().host.host}:{PostgreSQLConfig().port}/" f"{self._config.host.host}:{self._config.port}/"
f"{PostgreSQLConfig().database}", f"{self._config.database}",
echo=False, echo=False,
json_serializer=json_dumps, json_serializer=json_dumps,
json_deserializer=deserialize_json_dict, json_deserializer=deserialize_json_dict,