From 44909f52d9744bc23fe90709c2705332fa80f257 Mon Sep 17 00:00:00 2001 From: Mose Mueller Date: Tue, 13 Feb 2024 11:18:22 +0100 Subject: [PATCH] fix: optional database dependencies work without each other now --- pydase_service_base/database/__init__.py | 33 ++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/pydase_service_base/database/__init__.py b/pydase_service_base/database/__init__.py index 5476106..a540e40 100644 --- a/pydase_service_base/database/__init__.py +++ b/pydase_service_base/database/__init__.py @@ -1,4 +1,33 @@ -from .influxdb_session import InfluxDBSession -from .postgres_session import PostgresDatabaseSession +class OptionalDependencyError(Exception): + """Exception raised when an optional dependency is not installed.""" + + +try: + import influxdb_client # type: ignore # noqa + + from .influxdb_session import InfluxDBSession # type: ignore +except ImportError: + + class InfluxDBSession: # type: ignore + def __init__(self) -> None: + raise OptionalDependencyError( + "InfluxDBSession requires the 'influxdbv2' extra. " + "Please refer to https://gitlab.phys.ethz.ch/tiqi-projects/qchub/icon-services/pydase_service_base." + ) + + +try: + import sqlmodel # noqa + + from .postgres_session import PostgresDatabaseSession # type: ignore +except ImportError: + + class PostgresDatabaseSession: # type: ignore + def __init__(self) -> None: + raise OptionalDependencyError( + "PostgresDatabaseSession requires the 'postgresql' extra. " + "Please refer to https://gitlab.phys.ethz.ch/tiqi-projects/qchub/icon-services/pydase_service_base." + ) + __all__ = ["InfluxDBSession", "PostgresDatabaseSession"]