2014-04-03 21:31:46 +00:00
|
|
|
from storage.local import LocalStorage
|
2014-09-09 19:54:03 +00:00
|
|
|
from storage.cloud import S3Storage, GoogleCloudStorage, RadosGWStorage
|
2014-04-03 21:31:46 +00:00
|
|
|
from storage.fakestorage import FakeStorage
|
2014-06-17 20:03:43 +00:00
|
|
|
from storage.distributedstorage import DistributedStorage
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
|
2014-08-12 06:06:44 +00:00
|
|
|
STORAGE_DRIVER_CLASSES = {
|
|
|
|
'LocalStorage': LocalStorage,
|
|
|
|
'S3Storage': S3Storage,
|
|
|
|
'GoogleCloudStorage': GoogleCloudStorage,
|
2014-09-09 19:54:03 +00:00
|
|
|
'RadosGWStorage': RadosGWStorage,
|
2014-08-12 06:06:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 21:20:51 +00:00
|
|
|
def get_storage_driver(storage_params):
|
|
|
|
""" Returns a storage driver class for the given storage configuration
|
|
|
|
(a pair of string name and a dict of parameters). """
|
|
|
|
driver = storage_params[0]
|
|
|
|
parameters = storage_params[1]
|
|
|
|
driver_class = STORAGE_DRIVER_CLASSES.get(driver, FakeStorage)
|
|
|
|
return driver_class(**parameters)
|
|
|
|
|
2014-08-12 06:06:44 +00:00
|
|
|
|
2014-04-03 21:31:46 +00:00
|
|
|
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):
|
2014-06-17 20:03:43 +00:00
|
|
|
storages = {}
|
|
|
|
for location, storage_params in app.config.get('DISTRIBUTED_STORAGE_CONFIG').items():
|
2015-01-07 21:20:51 +00:00
|
|
|
storages[location] = get_storage_driver(storage_params)
|
2014-06-17 20:03:43 +00:00
|
|
|
|
|
|
|
preference = app.config.get('DISTRIBUTED_STORAGE_PREFERENCE', None)
|
|
|
|
if not preference:
|
|
|
|
preference = storages.keys()
|
|
|
|
|
|
|
|
d_storage = DistributedStorage(storages, preference)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
# register extension with app
|
|
|
|
app.extensions = getattr(app, 'extensions', {})
|
2014-06-17 20:03:43 +00:00
|
|
|
app.extensions['storage'] = d_storage
|
|
|
|
return d_storage
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
2014-06-17 20:03:43 +00:00
|
|
|
return getattr(self.state, name, None)
|