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 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"),
]

View File

@ -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

View File

@ -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,