Skip to content

Commit

Permalink
Simplify backend import process/missing dependency handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Nov 15, 2020
1 parent 45517a5 commit a51846b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
55 changes: 22 additions & 33 deletions aiohttp_client_cache/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
from logging import getLogger
from importlib import import_module
from typing import Optional
from typing import Optional, Type

from aiohttp_client_cache.backends.base import BaseCache

PICKLE_PROTOCOL = 4
BACKEND_QUALNAMES = {
'dynamodb': 'aiohttp_client_cache.backends.dynamodb.DynamoDbCache',
'gridfs': 'aiohttp_client_cache.backends.gridfs.GridFSCache',
'memory': 'aiohttp_client_cache.backends.base.BaseCache',
'mongodb': 'aiohttp_client_cache.backends.mongo.MongoCache',
'redis': 'aiohttp_client_cache.backends.redis.RedisCache',
'sqlite': 'aiohttp_client_cache.backends.sqlite.DbCache',
}
logger = getLogger(__name__)


def import_member(qualname: str) -> Optional[Type]:
"""Attempt to import a class or other module member by qualified name"""
try:
module, member = qualname.rsplit('.', 1)
return getattr(import_module(module), member)
except (AttributeError, ImportError) as e:
logger.debug(f'Could not load {qualname}: {str(e)}')
return None


# Import all backends for which dependencies have been installed
try:
# Heroku doesn't allow the SQLite3 module to be installed
from aiohttp_client_cache.backends.sqlite import DbCache
except ImportError:
DbCache = None
try:
from aiohttp_client_cache.backends.mongo import MongoCache, MongoDict
except ImportError:
MongoCache, MongoDict = None, None
try:
from aiohttp_client_cache.backends.gridfs import GridFSCache
except ImportError:
GridFSCache = None
try:
from aiohttp_client_cache.backends.redis import RedisCache
except ImportError:
RedisCache = None
try:
from aiohttp_client_cache.backends.dynamodb import DynamoDbCache
except ImportError:
DynamoDbCache = None

BACKEND_CLASSES = {
'dynamodb': DynamoDbCache,
'gridfs': GridFSCache,
'memory': BaseCache,
'mongo': MongoCache,
'mongodb': MongoCache,
'redis': RedisCache,
'sqlite': DbCache,
}
BACKEND_CLASSES = {name: import_member(qualname) for name, qualname in BACKEND_QUALNAMES.items()}


def create_backend(backend_name: Optional[str], *args, **kwargs):
def create_backend(backend_name: Optional[str], *args, **kwargs) -> BaseCache:
"""Initialize a backend by name"""
logger.info(f'Initializing backend: {backend_name}')
if isinstance(backend_name, BaseCache):
Expand Down
3 changes: 2 additions & 1 deletion aiohttp_client_cache/backends/gridfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from gridfs import GridFS
from pymongo import MongoClient

from aiohttp_client_cache.backends import PICKLE_PROTOCOL, BaseCache, MongoDict
from aiohttp_client_cache.backends import PICKLE_PROTOCOL, BaseCache
from aiohttp_client_cache.backends.mongo import MongoDict


class GridFSCache(BaseCache):
Expand Down

0 comments on commit a51846b

Please sign in to comment.