Skip to content

Commit

Permalink
Merge pull request #132 from Aiven-Open/rominf-reorganize-tests
Browse files Browse the repository at this point in the history
tests: reorganize
  • Loading branch information
akudiyar authored Aug 18, 2023
2 parents 25175e0 + 4e8f86b commit 2d4a617
Show file tree
Hide file tree
Showing 24 changed files with 163 additions and 170 deletions.
82 changes: 11 additions & 71 deletions rohmu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,18 @@
Copyright (c) 2022 Aiven, Helsinki, Finland. https://aiven.io/
See LICENSE for details
"""
from .common.constants import IO_BLOCK_SIZE
from .common.models import StorageModel
from .errors import InvalidConfigurationError
from .factory import (
Config,
get_class_for_notifier,
get_class_for_transfer,
get_notifier,
get_transfer,
get_transfer_model,
NOTIFIER_TYPE,
STORAGE_TYPE,
)
from .notifier.interface import Notifier
from .object_storage.base import BaseTransfer
from typing import Any, cast, Dict, Type

IO_BLOCK_SIZE = 2**20 # 1 MiB
STORAGE_TYPE = "storage_type"
NOTIFIER_TYPE = "notifier_type"
Config = Dict[str, Any]


def get_class_for_transfer(obj_store: Config) -> Type[BaseTransfer[StorageModel]]:
storage_type = obj_store[STORAGE_TYPE]
if storage_type == "azure":
from .object_storage.azure import AzureTransfer

return cast(Type[BaseTransfer[StorageModel]], AzureTransfer)
elif storage_type == "google":
from .object_storage.google import GoogleTransfer

return cast(Type[BaseTransfer[StorageModel]], GoogleTransfer)
elif storage_type == "sftp":
from .object_storage.sftp import SFTPTransfer

return cast(Type[BaseTransfer[StorageModel]], SFTPTransfer)
elif storage_type == "local":
from .object_storage.local import LocalTransfer

return cast(Type[BaseTransfer[StorageModel]], LocalTransfer)
elif storage_type == "s3":
from .object_storage.s3 import S3Transfer

return cast(Type[BaseTransfer[StorageModel]], S3Transfer)
elif storage_type == "swift":
from .object_storage.swift import SwiftTransfer

return cast(Type[BaseTransfer[StorageModel]], SwiftTransfer)

raise InvalidConfigurationError("unsupported storage type {0!r}".format(storage_type))


def get_class_for_notifier(notifier_config: Config) -> Type[Notifier]:
notifier_type = notifier_config[NOTIFIER_TYPE]
if notifier_type == "http":
from .notifier.http import BackgroundHTTPNotifier

return BackgroundHTTPNotifier
raise InvalidConfigurationError("unsupported storage type {0!r}".format(notifier_type))


def get_notifier(notifier_config: Config) -> Notifier:
notificer_class = get_class_for_notifier(notifier_config)
notifier_config = notifier_config.copy()
notifier_config.pop(NOTIFIER_TYPE)
return notificer_class(**notifier_config)


def get_transfer_model(storage_config: Config) -> StorageModel:
storage_class = get_class_for_transfer(storage_config)
storage_config = dict(storage_config)
storage_config.pop(STORAGE_TYPE)
notifier_config = storage_config.pop("notifier", None)
notifier = None
if notifier_config is not None:
notifier = get_notifier(notifier_config)

model = storage_class.config_model(**storage_config, notifier=notifier)
return model


def get_transfer(storage_config: Config) -> BaseTransfer[StorageModel]:
storage_class = get_class_for_transfer(storage_config)
model = get_transfer_model(storage_config)
return storage_class.from_model(model)
3 changes: 3 additions & 0 deletions rohmu/common/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright (c) 2023 Aiven, Helsinki, Finland. https://aiven.io/

IO_BLOCK_SIZE = 2**20 # 1 MiB
2 changes: 1 addition & 1 deletion rohmu/encryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Copyright (c) 2016 Ohmu Ltd
See LICENSE for details
"""
from . import IO_BLOCK_SIZE
from .common.constants import IO_BLOCK_SIZE
from .errors import UninitializedError
from .filewrap import FileWrap, Sink, Stream
from .typing import BinaryData, FileLike, HasRead, HasWrite
Expand Down
76 changes: 76 additions & 0 deletions rohmu/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright (c) 2023 Aiven, Helsinki, Finland. https://aiven.io/

from .common.models import StorageModel
from .errors import InvalidConfigurationError
from .notifier.interface import Notifier
from .object_storage.base import BaseTransfer
from typing import Any, cast, Dict, Type

STORAGE_TYPE = "storage_type"
NOTIFIER_TYPE = "notifier_type"
Config = Dict[str, Any]


def get_class_for_transfer(obj_store: Config) -> Type[BaseTransfer[StorageModel]]:
storage_type = obj_store[STORAGE_TYPE]
if storage_type == "azure":
from .object_storage.azure import AzureTransfer

return cast(Type[BaseTransfer[StorageModel]], AzureTransfer)
elif storage_type == "google":
from .object_storage.google import GoogleTransfer

return cast(Type[BaseTransfer[StorageModel]], GoogleTransfer)
elif storage_type == "sftp":
from .object_storage.sftp import SFTPTransfer

return cast(Type[BaseTransfer[StorageModel]], SFTPTransfer)
elif storage_type == "local":
from .object_storage.local import LocalTransfer

return cast(Type[BaseTransfer[StorageModel]], LocalTransfer)
elif storage_type == "s3":
from .object_storage.s3 import S3Transfer

return cast(Type[BaseTransfer[StorageModel]], S3Transfer)
elif storage_type == "swift":
from .object_storage.swift import SwiftTransfer

return cast(Type[BaseTransfer[StorageModel]], SwiftTransfer)

raise InvalidConfigurationError("unsupported storage type {0!r}".format(storage_type))


def get_class_for_notifier(notifier_config: Config) -> Type[Notifier]:
notifier_type = notifier_config[NOTIFIER_TYPE]
if notifier_type == "http":
from .notifier.http import BackgroundHTTPNotifier

return BackgroundHTTPNotifier
raise InvalidConfigurationError("unsupported storage type {0!r}".format(notifier_type))


def get_notifier(notifier_config: Config) -> Notifier:
notificer_class = get_class_for_notifier(notifier_config)
notifier_config = notifier_config.copy()
notifier_config.pop(NOTIFIER_TYPE)
return notificer_class(**notifier_config)


def get_transfer_model(storage_config: Config) -> StorageModel:
storage_class = get_class_for_transfer(storage_config)
storage_config = dict(storage_config)
storage_config.pop(STORAGE_TYPE)
notifier_config = storage_config.pop("notifier", None)
notifier = None
if notifier_config is not None:
notifier = get_notifier(notifier_config)

model = storage_class.config_model(**storage_config, notifier=notifier)
return model


def get_transfer(storage_config: Config) -> BaseTransfer[StorageModel]:
storage_class = get_class_for_transfer(storage_config)
model = get_transfer_model(storage_config)
return storage_class.from_model(model)
2 changes: 1 addition & 1 deletion rohmu/rohmufile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from __future__ import annotations

from . import IO_BLOCK_SIZE
from .common.constants import IO_BLOCK_SIZE
from .compressor import CompressionFile, DecompressionFile, DecompressSink
from .encryptor import DecryptorFile, DecryptSink, EncryptorFile
from .errors import InvalidConfigurationError
Expand Down
2 changes: 1 addition & 1 deletion rohmu/snappyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Copyright (c) 2016 Ohmu Ltd
See LICENSE for details
"""
from . import IO_BLOCK_SIZE
from .common.constants import IO_BLOCK_SIZE
from .filewrap import FileWrap
from .typing import BinaryData, FileLike
from typing import Optional
Expand Down
2 changes: 1 addition & 1 deletion rohmu/transfer_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

from __future__ import annotations

from . import get_transfer as rohmu_get_transfer
from .errors import InvalidTransferError
from .factory import get_transfer as rohmu_get_transfer
from .object_storage.base import BaseTransfer, StorageModel
from contextlib import contextmanager
from typing import Any, Callable, Generator, Optional
Expand Down
2 changes: 1 addition & 1 deletion rohmu/zstdfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Copyright (c) 2016 Ohmu Ltd
See LICENSE for details
"""
from . import IO_BLOCK_SIZE
from .common.constants import IO_BLOCK_SIZE
from .filewrap import FileWrap
from .typing import BinaryData, FileLike
from typing import Optional
Expand Down
30 changes: 0 additions & 30 deletions test/base.py

This file was deleted.

Empty file added test/common/__init__.py
Empty file.
File renamed without changes.
Empty file added test/notifier/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions test/notifier/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def __enter__(self) -> None:
pass

def __exit__(
self, type: Type[BaseException], value: BaseException, traceback: TracebackType
) -> None: # pylint: disable=redefined-builtin
self, type: Type[BaseException], value: BaseException, traceback: TracebackType # pylint: disable=redefined-builtin
) -> None:
pass


Expand Down
Empty file added test/object_storage/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 2d4a617

Please sign in to comment.