From 317bd83605a2c11e16507f5a4e81ebc6d636f957 Mon Sep 17 00:00:00 2001 From: Mardix Date: Wed, 3 Jun 2015 02:36:53 -0400 Subject: [PATCH] 0.5.0 - Object path returns the full path on local. GObject.get_url() can return local short url. Rename Storage:get_object to Storage:object --- CHANGELOG | 4 ++++ flask_cloudstorage.py | 38 +++++++++++++++++++++++++++----------- setup.py | 4 ++-- tests/test_cloudstorage.py | 14 +++++++------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f6736d6..dd55eef 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +0.5.0 + - object_path return the full path of the object when on local + - get_url() can return short url for local file instead of the full domain one + - rename Storage:get_object to Storage:object 0.4.0 - Added: provider_name, container_name, and local_path, object_path to object diff --git a/flask_cloudstorage.py b/flask_cloudstorage.py index 71bc561..429925c 100644 --- a/flask_cloudstorage.py +++ b/flask_cloudstorage.py @@ -98,7 +98,7 @@ def get_provider_name(driver): class InvalidExtensionError(Exception): pass - +class LocalPathUndefinedError(Exception): pass class Storage(object): _container_name = None @@ -180,7 +180,7 @@ class Storage(object): if provider and provider.upper() == "LOCAL": if not local_path: - raise ValueError("For 'LOCAL' provider, Storage requires CLOUDSTORAGE_LOCAL_PATH") + raise LocalPathUndefinedError("For 'LOCAL' provider, Storage requires CLOUDSTORAGE_LOCAL_PATH") else: key = local_path secret = None @@ -223,15 +223,26 @@ class Storage(object): secure_url=self.secure_url, local_path=self.local_path) - def get_object(self, object_name, secure_url=None, validate=True, **kwargs): + def __len__(self): + """ + Return the total objects in the container + :return: int + """ + return len(self.container.list_objects()) + + def object(self, object_name, secure_url=None, validate=True, **kwargs): """ Get the object :param object_name: :param secure_url: To secure url, when get_url :param validate: When False, it will build the object without validating it. the object file may not exist in the container - :param kwargs: When validate is False, these args will be pass + :param kwargs: When validate is False, these args will be used to build the object to the object builder + - size + - hash + - extra + - meta_data :return: Object """ if validate: @@ -389,13 +400,15 @@ class Object(object): def __len__(self): return self.size - def get_url(self, secure_url=None): + def get_url(self, secure=None, short=True): """ Return the url - :param secure_url: - :return: + :param secure: bool - To use https + :param short: bool - On local, reference the local path without the domain + ie: http://site.com/files/object.png -> /files/object.png + :return: str """ - secure = secure_url or self._kwargs.get("secure_url", False) + secure = secure or self._kwargs.get("secure_url", False) driver_name = self.driver.name.lower() try: # Currently only Cloudfiles and Local supports it @@ -403,7 +416,7 @@ class Object(object): if "local" in driver_name: url = url_for(FILE_SERVER_ENDPOINT, object_name=self.name, - _external=True) + _external=False if short else True) except NotImplementedError as e: object_path = '%s/%s' % (self.container.name, self.name) if 's3' in driver_name: @@ -473,7 +486,7 @@ class Object(object): def local_path(self): """ Return the local path for Local storage - :return: + :return: str """ return self._kwargs.get("local_path", None) @@ -483,5 +496,8 @@ class Object(object): Return the object path :return: str """ - return '%s/%s' % (self.container.name, self.name) + path = "%s/%s" % (self.container.name, self.name) + if "local" in self.driver.name.lower(): + path = "%s/%s" % (self.local_path, path) + return path diff --git a/setup.py b/setup.py index 04a75f1..5c17170 100644 --- a/setup.py +++ b/setup.py @@ -19,8 +19,8 @@ Supported storage: from setuptools import setup, find_packages -__NAME__ = "flask-CloudStorage" -__version__ = "0.4.0" +__NAME__ = "Flask-CloudStorage" +__version__ = "0.5.0" __author__ = "Mardix" __license__ = "MIT" __copyright__ = "2015" diff --git a/tests/test_cloudstorage.py b/tests/test_cloudstorage.py index 6b2cdf6..3262edb 100644 --- a/tests/test_cloudstorage.py +++ b/tests/test_cloudstorage.py @@ -91,13 +91,13 @@ def test_storage_object_not_exists(): def test_storage_object(): object_name = "hello.txt" storage = app_storage() - o = storage.get_object(object_name, validate=False) + o = storage.object(object_name, validate=False) assert isinstance(o, Object) def test_object_type_extension(): object_name = "hello.jpg" storage = app_storage() - o = storage.get_object(object_name, validate=False) + o = storage.object(object_name, validate=False) assert o.type == "IMAGE" assert o.extension == "jpg" @@ -109,26 +109,26 @@ def test_object_not_exists(): def test_object_provider_name(): object_name = "hello.jpg" storage = app_storage() - o = storage.get_object(object_name, validate=False) + o = storage.object(object_name, validate=False) assert o.provider_name == config.PROVIDER.lower() def test_object_container_name(): object_name = "hello.jpg" storage = app_storage() - o = storage.get_object(object_name, validate=False) + o = storage.object(object_name, validate=False) assert o.container_name == config.CONTAINER def test_object_object_path(): object_name = "hello.jpg" storage = app_storage() - o = storage.get_object(object_name, validate=False) + o = storage.object(object_name, validate=False) p = "%s/%s" % (o.container.name, o.name) - assert o.object_path == p + assert o.object_path.endswith(p) def test_object_local_path(): object_name = "hello.jpg" storage = app_storage() - o = storage.get_object(object_name, validate=False) + o = storage.object(object_name, validate=False) if "local" in o.container.name.lower(): assert o.local_path == CWD