44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
from storage.local import LocalStorage
|
|
from storage.cloud import S3Storage, GoogleCloudStorage
|
|
from storage.fakestorage import FakeStorage
|
|
from storage.distributedstorage import DistributedStorage
|
|
|
|
|
|
STORAGE_DRIVER_CLASSES = {
|
|
'LocalStorage': LocalStorage,
|
|
'S3Storage': S3Storage,
|
|
'GoogleCloudStorage': GoogleCloudStorage,
|
|
}
|
|
|
|
|
|
class Storage(object):
|
|
def __init__(self, app=None):
|
|
self.app = app
|
|
if app is not None:
|
|
self.state = self.init_app(app)
|
|
else:
|
|
self.state = None
|
|
|
|
def init_app(self, app):
|
|
storages = {}
|
|
for location, storage_params in app.config.get('DISTRIBUTED_STORAGE_CONFIG').items():
|
|
driver = storage_params[0]
|
|
parameters = storage_params[1]
|
|
|
|
driver_class = STORAGE_DRIVER_CLASSES.get(driver, FakeStorage)
|
|
storage = driver_class(**parameters)
|
|
storages[location] = storage
|
|
|
|
preference = app.config.get('DISTRIBUTED_STORAGE_PREFERENCE', None)
|
|
if not preference:
|
|
preference = storages.keys()
|
|
|
|
d_storage = DistributedStorage(storages, preference)
|
|
|
|
# register extension with app
|
|
app.extensions = getattr(app, 'extensions', {})
|
|
app.extensions['storage'] = d_storage
|
|
return d_storage
|
|
|
|
def __getattr__(self, name):
|
|
return getattr(self.state, name, None)
|