Merge pull request 'moved import of optional packages into classes' (#6) from make-optional-parts-independent into main
Reviewed-on: brian/python-repositories#6
This commit was merged in pull request #6.
This commit is contained in:
@@ -3,10 +3,8 @@
|
||||
from __future__ import annotations
|
||||
import os
|
||||
from io import BytesIO
|
||||
|
||||
from importlib.util import find_spec
|
||||
import structlog
|
||||
from minio import Minio, S3Error
|
||||
|
||||
from python_utils import check_env
|
||||
|
||||
from python_repositories.interfaces import (
|
||||
@@ -14,6 +12,10 @@ from python_repositories.interfaces import (
|
||||
ConnectionAwareInterface,
|
||||
)
|
||||
|
||||
# Handle optional dependencies
|
||||
if find_spec("minio") is not None:
|
||||
import minio
|
||||
|
||||
|
||||
class MinioAdapter(
|
||||
ContextAwareInterface,
|
||||
@@ -42,7 +44,7 @@ class MinioAdapter(
|
||||
},
|
||||
)
|
||||
# Prepare internal variables
|
||||
self._client: Minio | None = None
|
||||
self._client: minio.Minio | None = None
|
||||
self._bucket_name: str | None = None
|
||||
|
||||
def __enter__(self) -> MinioAdapter:
|
||||
@@ -77,7 +79,7 @@ class MinioAdapter(
|
||||
secret_key = str(os.getenv(self.secret_key_env_var_name))
|
||||
bucket = str(os.getenv(self.bucket_env_var_name))
|
||||
# Connect client
|
||||
client = Minio(
|
||||
client = minio.Minio(
|
||||
endpoint=endpoint,
|
||||
access_key=access_key,
|
||||
secret_key=secret_key,
|
||||
@@ -107,7 +109,7 @@ class MinioAdapter(
|
||||
@property
|
||||
def is_connected(self) -> bool:
|
||||
"""Check if connected to Minio server."""
|
||||
res = bool(isinstance(self._client, Minio))
|
||||
res = bool(isinstance(self._client, minio.Minio))
|
||||
self.logger.debug(res)
|
||||
return res
|
||||
|
||||
@@ -161,7 +163,7 @@ class MinioAdapter(
|
||||
f"Got object '{object_name}' from bucket '{self._bucket_name}'"
|
||||
)
|
||||
return buffer
|
||||
except S3Error as exc:
|
||||
except minio.S3Error as exc:
|
||||
if exc.code == "NoSuchKey":
|
||||
self.logger.warning(
|
||||
f"Object '{object_name}' not found in bucket '{self._bucket_name}'"
|
||||
|
||||
@@ -2,13 +2,10 @@
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import cast
|
||||
|
||||
from importlib.util import find_spec
|
||||
import os
|
||||
import structlog
|
||||
|
||||
import redis
|
||||
from redis.commands.json.path import Path as RedisPath
|
||||
|
||||
from python_utils import check_env
|
||||
|
||||
from python_repositories.interfaces import (
|
||||
@@ -16,6 +13,11 @@ from python_repositories.interfaces import (
|
||||
ConnectionAwareInterface,
|
||||
)
|
||||
|
||||
# Handle optional dependencies
|
||||
if find_spec("redis") is not None:
|
||||
import redis
|
||||
from redis.commands.json.path import Path as RedisPath
|
||||
|
||||
|
||||
class RedisAdapter(
|
||||
ContextAwareInterface,
|
||||
@@ -24,7 +26,7 @@ class RedisAdapter(
|
||||
"""Redis adapter exposing basic CRUD functionality."""
|
||||
|
||||
uri_env_var_name: str = "REDIS_URI"
|
||||
path: str = RedisPath.root_path()
|
||||
path: str = "." # JSON root path, updated in __init__
|
||||
encoding: str = "UTF-8"
|
||||
|
||||
def __init__(self) -> None:
|
||||
@@ -36,6 +38,7 @@ class RedisAdapter(
|
||||
check_env(self.uri_env_var_name)
|
||||
# Prepare internal variables
|
||||
self._client: redis.Redis | None = None
|
||||
self.path: str = RedisPath.root_path()
|
||||
|
||||
def __enter__(self) -> RedisAdapter:
|
||||
"""Enter the context."""
|
||||
|
||||
Reference in New Issue
Block a user