Просмотр исходного кода

Renamed download to save_content and add iter_content method.

Nuwan Goonasekera 10 лет назад
Родитель
Сommit
da9cad34bf

+ 9 - 0
cloudbridge/cloud/base/resources.py

@@ -5,7 +5,9 @@ import inspect
 import itertools
 import json
 import logging
+import shutil
 import time
+
 import six
 
 from cloudbridge.cloud.interfaces.resources \
@@ -595,6 +597,13 @@ class BaseBucketObject(BucketObject, BaseCloudResource):
     def __init__(self, provider):
         super(BaseBucketObject, self).__init__(provider)
 
+    def save_content(self, target_stream):
+        """
+        Download this object and write its
+        contents to the target_stream.
+        """
+        shutil.copyfileobj(self.iter_content(), target_stream)
+
     def __eq__(self, other):
         return (isinstance(other, BucketObject) and
                 # pylint:disable=protected-access

+ 13 - 4
cloudbridge/cloud/interfaces/resources.py

@@ -1765,12 +1765,21 @@ class BucketObject(CloudResource):
         pass
 
     @abstractmethod
-    def download(self, target_stream):
+    def iter_content(self):
         """
-        Download this object and write its contents to the ``target_stream``.
+        Returns this object's content as an iterable.
+
+        :rtype: Iterable
+        :return: An iterable of the file contents
+
+        """
+        pass
+
+    @abstractmethod
+    def save_content(self, target_stream):
+        """
+        Save this object and write its contents to the ``target_stream``.
 
-        :rtype: bool
-        :return: ``True`` if successful.
         """
         pass
 

+ 4 - 5
cloudbridge/cloud/providers/aws/resources.py

@@ -3,7 +3,6 @@ DataTypes used by this provider
 """
 import inspect
 import json
-import shutil
 
 from boto.exception import EC2ResponseError
 from boto.s3.key import Key
@@ -704,12 +703,12 @@ class AWSBucketObject(BaseBucketObject):
         """
         return self._key.name
 
-    def download(self, target_stream):
+    def iter_content(self):
         """
-        Download this object and write its
-        contents to the target_stream.
+        Returns this object's content as an
+        iterable.
         """
-        shutil.copyfileobj(self._key, target_stream)
+        return self._key
 
     def upload(self, data):
         """

+ 5 - 5
cloudbridge/cloud/providers/openstack/resources.py

@@ -3,7 +3,7 @@ DataTypes used by this provider
 """
 import inspect
 import json
-import shutil
+
 
 import ipaddress
 
@@ -868,14 +868,14 @@ class OpenStackBucketObject(BaseBucketObject):
         """
         return self._obj.get("name")
 
-    def download(self, target_stream):
+    def iter_content(self):
         """
-        Download this object and write its
-        contents to the target_stream.
+        Returns this object's content as an
+        iterable.
         """
         _, content = self._provider.swift.get_object(
             self.cbcontainer.name, self.name, resp_chunk_size=65536)
-        shutil.copyfileobj(content, target_stream)
+        return content
 
     def upload(self, data):
         """

+ 5 - 1
test/test_object_store_service.py

@@ -133,5 +133,9 @@ class CloudObjectStoreServiceTestCase(ProviderTestBase):
                 # multiple methods like upload_from_file, from_stream etc.
                 obj.upload(content)
                 target_stream = BytesIO()
-                obj.download(target_stream)
+                obj.save_content(target_stream)
                 self.assertEqual(target_stream.getvalue(), content)
+                target_stream2 = BytesIO()
+                for data in obj.iter_content():
+                    target_stream2.write(data)
+                self.assertEqual(target_stream2.getvalue(), content)