Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/__init__.py
# Copyright (C) 2015-2016 The Software Heritage developers | # Copyright (C) 2015-2016 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 . import storage | from . import storage | ||||
Storage = storage.Storage | Storage = storage.Storage | ||||
class HashCollision(Exception): | class HashCollision(Exception): | ||||
pass | pass | ||||
STORAGE_IMPLEMENTATION = {'local', 'remote', 'memory'} | STORAGE_IMPLEMENTATION = {'local', 'remote', 'memory', 'filter'} | ||||
def get_storage(cls, args): | def get_storage(cls, args): | ||||
"""Get a storage object of class `storage_class` with arguments | """Get a storage object of class `storage_class` with arguments | ||||
`storage_args`. | `storage_args`. | ||||
Args: | Args: | ||||
storage (dict): dictionary with keys: | storage (dict): dictionary with keys: | ||||
- cls (str): storage's class, either local, remote, memory | - cls (str): storage's class, either local, remote, memory, filter | ||||
- args (dict): dictionary with keys | - args (dict): dictionary with keys | ||||
Returns: | Returns: | ||||
an instance of swh.storage.Storage (either local or remote) | an instance of swh.storage.Storage (either local or remote) | ||||
Raises: | Raises: | ||||
ValueError if passed an unknown storage class. | ValueError if passed an unknown storage class. | ||||
""" | """ | ||||
if cls not in STORAGE_IMPLEMENTATION: | if cls not in STORAGE_IMPLEMENTATION: | ||||
raise ValueError('Unknown storage class `%s`. Supported: %s' % ( | raise ValueError('Unknown storage class `%s`. Supported: %s' % ( | ||||
cls, ', '.join(STORAGE_IMPLEMENTATION))) | cls, ', '.join(STORAGE_IMPLEMENTATION))) | ||||
if cls == 'remote': | if cls == 'remote': | ||||
from .api.client import RemoteStorage as Storage | from .api.client import RemoteStorage as Storage | ||||
elif cls == 'local': | elif cls == 'local': | ||||
from .storage import Storage | from .storage import Storage | ||||
elif cls == 'memory': | elif cls == 'memory': | ||||
from .in_memory import Storage | from .in_memory import Storage | ||||
elif cls == 'filter': | |||||
from .filter import FilteringProxyStorage as Storage | |||||
return Storage(**args) | return Storage(**args) |