mirror of
https://github.com/tiqi-group/pydase_service_base.git
synced 2025-06-04 03:40:39 +02:00
database config dir is configured using env variable now, defaults to database_config in root folder
This commit is contained in:
parent
6592f92cee
commit
c3eebefeeb
@ -1,54 +1,9 @@
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from confz import BaseConfig, EnvSource, FileSource
|
||||
from confz import BaseConfig, EnvSource
|
||||
from pydantic import AnyUrl, SecretStr
|
||||
|
||||
# Retrieve the name of the current package
|
||||
package_name = Path(__file__).resolve().parent.parent.name
|
||||
|
||||
|
||||
def find_dir_upwards(start_dir: Path, targets: list[str]) -> Path | None:
|
||||
"""
|
||||
Traverse directory path upwards until finding a directory named as one of the
|
||||
targets.
|
||||
|
||||
Args:
|
||||
start_dir: The starting point directory path.
|
||||
targets: List of target directory names to be searched for.
|
||||
|
||||
Returns:
|
||||
The directory path if found, None otherwise.
|
||||
"""
|
||||
for parent in start_dir.parents:
|
||||
for target in targets:
|
||||
if (parent / target).is_dir():
|
||||
return parent / target
|
||||
return None
|
||||
|
||||
|
||||
# Look for ".venv" or "venv" directories starting from the current file's directory.
|
||||
VENV_DIR = find_dir_upwards(Path(__file__).resolve(), [".venv", "venv"])
|
||||
# Look for "deps" directory starting from the current file's directory.
|
||||
DEPS_DIR = find_dir_upwards(Path(__file__).resolve(), ["deps"])
|
||||
|
||||
CONFIG_DIR: Path | None = None
|
||||
|
||||
# If a ".venv" or "venv" directory was found and its parent's name is not the current
|
||||
# package name, check for the "database_config" directory inside the parent directory of
|
||||
# the found directory.
|
||||
if VENV_DIR is not None and VENV_DIR.parent.name != package_name:
|
||||
CONFIG_DIR = VENV_DIR.parent / "database_config"
|
||||
if not CONFIG_DIR.exists():
|
||||
CONFIG_DIR = None
|
||||
# If no ".venv" or "venv" directory was found or its parent's name is the current
|
||||
# package name, but a "deps" directory was found, check for the "database_config"
|
||||
# directory inside the parent directory of the found "deps" directory.
|
||||
elif DEPS_DIR is not None:
|
||||
CONFIG_DIR = DEPS_DIR.parent / "database_config"
|
||||
if not CONFIG_DIR.exists():
|
||||
CONFIG_DIR = None
|
||||
|
||||
|
||||
class OperationMode(BaseConfig): # type: ignore
|
||||
environment: Literal["development", "production"] = "development"
|
||||
@ -56,6 +11,12 @@ class OperationMode(BaseConfig): # type: ignore
|
||||
CONFIG_SOURCES = EnvSource(allow=["ENVIRONMENT"])
|
||||
|
||||
|
||||
class ServiceConfig(BaseConfig): # type: ignore
|
||||
database_config_dir: Path = Path("database_config")
|
||||
|
||||
CONFIG_SOURCES = EnvSource(allow=["SERVICE_DATABASE_CONFIG_DIR"])
|
||||
|
||||
|
||||
class PostgreSQLConfig(BaseConfig): # type: ignore
|
||||
host: AnyUrl
|
||||
port: int
|
||||
@ -63,10 +24,10 @@ class PostgreSQLConfig(BaseConfig): # type: ignore
|
||||
user: str
|
||||
password: SecretStr
|
||||
|
||||
if CONFIG_DIR:
|
||||
CONFIG_SOURCES = FileSource(
|
||||
CONFIG_DIR / f"postgres_{OperationMode().environment}.yaml"
|
||||
)
|
||||
# if CONFIG_DIR:
|
||||
# CONFIG_SOURCES = FileSource(
|
||||
# CONFIG_DIR / f"postgres_{OperationMode().environment}.yaml"
|
||||
# )
|
||||
|
||||
|
||||
class InfluxDBConfig(BaseConfig): # type: ignore
|
||||
@ -74,5 +35,5 @@ class InfluxDBConfig(BaseConfig): # type: ignore
|
||||
org: str
|
||||
token: SecretStr
|
||||
|
||||
if CONFIG_DIR:
|
||||
CONFIG_SOURCES = FileSource(CONFIG_DIR / "influxdb_config.yaml")
|
||||
# if CONFIG_DIR:
|
||||
# CONFIG_SOURCES = FileSource(CONFIG_DIR / "influxdb_config.yaml")
|
||||
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Any, NamedTuple
|
||||
|
||||
from confz import FileSource
|
||||
from influxdb_client import (
|
||||
Bucket,
|
||||
BucketRetentionRules,
|
||||
@ -16,8 +17,7 @@ from influxdb_client.client.write.point import DEFAULT_WRITE_PRECISION
|
||||
from influxdb_client.client.write_api import SYNCHRONOUS
|
||||
from influxdb_client.rest import ApiException
|
||||
|
||||
from icon_service_base.database.config import InfluxDBConfig
|
||||
from icon_service_base.database.create_config import create_config
|
||||
from icon_service_base.database.config import InfluxDBConfig, ServiceConfig
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterable
|
||||
@ -63,11 +63,11 @@ class InfluxDBSession:
|
||||
|
||||
conf_folder: Path | str
|
||||
|
||||
def __init__(self, config_folder: Path | str | None = None) -> None:
|
||||
self._config = create_config(
|
||||
InfluxDBConfig,
|
||||
config_folder=config_folder,
|
||||
config_file="influxdb_config.yaml",
|
||||
def __init__(self) -> None:
|
||||
self._config = InfluxDBConfig(
|
||||
config_sources=FileSource(
|
||||
ServiceConfig().database_config_dir / "influxdb_config.yaml"
|
||||
)
|
||||
)
|
||||
|
||||
self.url = self._config.url
|
||||
|
@ -6,11 +6,15 @@ import logging
|
||||
import re
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from confz import FileSource
|
||||
from dateutil.parser import ParserError, parse # type: ignore
|
||||
from sqlmodel import Session, SQLModel, create_engine
|
||||
|
||||
from icon_service_base.database.config import OperationMode, PostgreSQLConfig
|
||||
from icon_service_base.database.create_config import create_config
|
||||
from icon_service_base.database.config import (
|
||||
OperationMode,
|
||||
PostgreSQLConfig,
|
||||
ServiceConfig,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pathlib import Path
|
||||
@ -134,12 +138,13 @@ class PostgresDatabaseSession(Session):
|
||||
|
||||
conf_folder: Path | str
|
||||
|
||||
def __init__(self, config_folder: Path | str | None = None) -> None:
|
||||
def __init__(self) -> None:
|
||||
"""Initializes a new session bound to the database engine."""
|
||||
self._config = create_config(
|
||||
PostgreSQLConfig,
|
||||
config_folder=config_folder,
|
||||
config_file=f"postgres_{OperationMode().environment}.yaml",
|
||||
self._config = PostgreSQLConfig(
|
||||
config_sources=FileSource(
|
||||
ServiceConfig().database_config_dir
|
||||
/ f"postgres_{OperationMode().environment}.yaml"
|
||||
)
|
||||
)
|
||||
|
||||
super().__init__(
|
||||
|
Loading…
x
Reference in New Issue
Block a user