Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/factory.py
# Copyright (C) 2016-2020 The Software Heritage developers | # Copyright (C) 2016-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from typing import Callable, Dict, Union | from typing import Callable, Dict, Union | ||||
import warnings | |||||
from swh.objstorage.api.client import RemoteObjStorage | from swh.objstorage.api.client import RemoteObjStorage | ||||
from swh.objstorage.backends.generator import RandomGeneratorObjStorage | from swh.objstorage.backends.generator import RandomGeneratorObjStorage | ||||
from swh.objstorage.backends.in_memory import InMemoryObjStorage | from swh.objstorage.backends.in_memory import InMemoryObjStorage | ||||
from swh.objstorage.backends.pathslicing import PathSlicingObjStorage | from swh.objstorage.backends.pathslicing import PathSlicingObjStorage | ||||
from swh.objstorage.backends.seaweed import WeedObjStorage | from swh.objstorage.backends.seaweed import WeedObjStorage | ||||
from swh.objstorage.multiplexer import MultiplexerObjStorage, StripingObjStorage | from swh.objstorage.multiplexer import MultiplexerObjStorage, StripingObjStorage | ||||
from swh.objstorage.multiplexer.filter import add_filters | from swh.objstorage.multiplexer.filter import add_filters | ||||
Show All 39 Lines | try: | ||||
_STORAGE_CLASSES["s3"] = AwsCloudObjStorage | _STORAGE_CLASSES["s3"] = AwsCloudObjStorage | ||||
_STORAGE_CLASSES["swift"] = OpenStackCloudObjStorage | _STORAGE_CLASSES["swift"] = OpenStackCloudObjStorage | ||||
except ImportError as e: | except ImportError as e: | ||||
_STORAGE_CLASSES_MISSING["s3"] = e.args[0] | _STORAGE_CLASSES_MISSING["s3"] = e.args[0] | ||||
_STORAGE_CLASSES_MISSING["swift"] = e.args[0] | _STORAGE_CLASSES_MISSING["swift"] = e.args[0] | ||||
def get_objstorage(cls, args): | def get_objstorage(cls: str, args=None, **kwargs): | ||||
""" Create an ObjStorage using the given implementation class. | """ Create an ObjStorage using the given implementation class. | ||||
Args: | Args: | ||||
cls (str): objstorage class unique key contained in the | cls: objstorage class unique key contained in the | ||||
_STORAGE_CLASSES dict. | _STORAGE_CLASSES dict. | ||||
args (dict): arguments for the required class of objstorage | kwargs: arguments for the required class of objstorage | ||||
that must match exactly the one in the `__init__` method of the | that must match exactly the one in the `__init__` method of the | ||||
class. | class. | ||||
Returns: | Returns: | ||||
subclass of ObjStorage that match the given `storage_class` argument. | subclass of ObjStorage that match the given `storage_class` argument. | ||||
Raises: | Raises: | ||||
ValueError: if the given storage class is not a valid objstorage | ValueError: if the given storage class is not a valid objstorage | ||||
key. | key. | ||||
""" | """ | ||||
if cls in _STORAGE_CLASSES: | if cls in _STORAGE_CLASSES: | ||||
return _STORAGE_CLASSES[cls](**args) | if args is not None: | ||||
warnings.warn( | |||||
'Explicit "args" key is deprecated for objstorage initialization, ' | |||||
"use class arguments keys directly instead.", | |||||
DeprecationWarning, | |||||
) | |||||
# TODO: when removing this, drop the "args" backwards compatibility | |||||
# from swh.objstorage.api.server configuration checker | |||||
kwargs = args | |||||
return _STORAGE_CLASSES[cls](**kwargs) | |||||
else: | else: | ||||
raise ValueError( | raise ValueError( | ||||
"Storage class {} is not available: {}".format( | "Storage class {} is not available: {}".format( | ||||
cls, _STORAGE_CLASSES_MISSING.get(cls, "unknown name") | cls, _STORAGE_CLASSES_MISSING.get(cls, "unknown name") | ||||
) | ) | ||||
) | ) | ||||
Show All 21 Lines |