from storage.local import LocalStorage from storage.s3 import S3Storage from storage.fakestorage import FakeStorage 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): storage_type = app.config.get('STORAGE_TYPE', 'LocalStorage') path = app.config.get('STORAGE_PATH', '') if storage_type == 'LocalStorage': storage = LocalStorage(path) elif storage_type == 'S3Storage': access_key = app.config.get('STORAGE_AWS_ACCESS_KEY', '') secret_key = app.config.get('STORAGE_AWS_SECRET_KEY', '') bucket = app.config.get('STORAGE_S3_BUCKET', '') storage = S3Storage(path, access_key, secret_key, bucket) else: storage = FakeStorage() # register extension with app app.extensions = getattr(app, 'extensions', {}) app.extensions['storage'] = storage return storage def __getattr__(self, name): return getattr(self.state, name, None)