From 647e819cd338a00a863ef6cc8400f5ee55d5ba95 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Mon, 26 Jan 2015 15:02:31 -0800 Subject: [PATCH] Folding Blob.from_dict and Bucket.from_dict into constructors. See #545 for context. --- gcloud/storage/blob.py | 20 ++------------------ gcloud/storage/bucket.py | 21 +++++---------------- gcloud/storage/connection.py | 6 +++--- gcloud/storage/iterator.py | 2 +- gcloud/storage/test_blob.py | 20 ++++++++------------ gcloud/storage/test_bucket.py | 17 ++++++----------- 6 files changed, 25 insertions(+), 61 deletions(-) diff --git a/gcloud/storage/blob.py b/gcloud/storage/blob.py index 0658d88cf272..fb16fe58fa9f 100644 --- a/gcloud/storage/blob.py +++ b/gcloud/storage/blob.py @@ -77,6 +77,8 @@ def __init__(self, bucket=None, name=None, properties=None): :type properties: dict :param properties: All the other data provided by Cloud Storage. """ + if name is None and properties is not None: + name = properties.get('name') super(Blob, self).__init__(name=name, properties=properties) self.bucket = bucket @@ -87,24 +89,6 @@ def acl(self): self._acl = ObjectACL(self) return self._acl - @classmethod - def from_dict(cls, blob_dict, bucket=None): - """Instantiate a :class:`Blob` from data returned by the JSON API. - - :type blob_dict: dict - :param blob_dict: A dictionary of data returned from getting an - Cloud Storage object. - - :type bucket: :class:`gcloud.storage.bucket.Bucket` - :param bucket: The bucket to which this blob belongs (and by - proxy, which connection to use). - - :rtype: :class:`Blob` - :returns: A blob based on the data provided. - """ - - return cls(bucket=bucket, name=blob_dict['name'], properties=blob_dict) - def __repr__(self): if self.bucket: bucket_name = self.bucket.name diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index bbc8ca197d05..077e423e763c 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -15,6 +15,7 @@ """Create / interact with gcloud storage buckets.""" import os +import six from gcloud.storage._helpers import _PropertyMixin from gcloud.storage._helpers import _scalar_property @@ -23,7 +24,6 @@ from gcloud.storage.acl import DefaultObjectACL from gcloud.storage.iterator import Iterator from gcloud.storage.blob import Blob -import six class _BlobIterator(Iterator): @@ -50,7 +50,7 @@ def get_items_from_response(self, response): """ self.prefixes = tuple(response.get('prefixes', ())) for item in response.get('items', []): - yield Blob.from_dict(item, bucket=self.bucket) + yield Blob(properties=item, bucket=self.bucket) class Bucket(_PropertyMixin): @@ -88,22 +88,11 @@ class Bucket(_PropertyMixin): _acl = _default_object_acl = None def __init__(self, connection=None, name=None, properties=None): + if name is None and properties is not None: + name = properties.get('name') super(Bucket, self).__init__(name=name, properties=properties) self._connection = connection - @classmethod - def from_dict(cls, bucket_dict, connection=None): - """Construct a new bucket from a dictionary of data from Cloud Storage. - - :type bucket_dict: dict - :param bucket_dict: The dictionary of data to construct a bucket from. - - :rtype: :class:`Bucket` - :returns: A bucket constructed from the data provided. - """ - return cls(connection=connection, name=bucket_dict['name'], - properties=bucket_dict) - def __repr__(self): return '' % self.name @@ -169,7 +158,7 @@ def get_blob(self, blob): try: response = self.connection.api_request(method='GET', path=blob.path) - return Blob.from_dict(response, bucket=self) + return Blob(properties=response, bucket=self) except exceptions.NotFound: return None diff --git a/gcloud/storage/connection.py b/gcloud/storage/connection.py index 9820f54538e7..4d7072cbd470 100644 --- a/gcloud/storage/connection.py +++ b/gcloud/storage/connection.py @@ -380,7 +380,7 @@ def get_bucket(self, bucket_name): """ bucket = self.new_bucket(bucket_name) response = self.api_request(method='GET', path=bucket.path) - return Bucket.from_dict(response, connection=self) + return Bucket(properties=response, connection=self) def lookup(self, bucket_name): """Get a bucket by name, returning None if not found. @@ -430,7 +430,7 @@ def create_bucket(self, bucket): bucket = self.new_bucket(bucket) response = self.api_request(method='POST', path='/b', data={'name': bucket.name}) - return Bucket.from_dict(response, connection=self) + return Bucket(properties=response, connection=self) def delete_bucket(self, bucket, force=False): """Delete a bucket. @@ -575,7 +575,7 @@ def get_items_from_response(self, response): :param response: The JSON API response for a page of buckets. """ for item in response.get('items', []): - yield Bucket.from_dict(item, connection=self.connection) + yield Bucket(properties=item, connection=self.connection) def _get_expiration_seconds(expiration): diff --git a/gcloud/storage/iterator.py b/gcloud/storage/iterator.py index a1c68e32b1b3..bb47490bd368 100644 --- a/gcloud/storage/iterator.py +++ b/gcloud/storage/iterator.py @@ -25,7 +25,7 @@ class MyIterator(Iterator): def get_items_from_response(self, response): items = response.get('items', []) for item in items: - yield MyItemClass.from_dict(item, other_arg=True) + yield MyItemClass(properties=item, other_arg=True) You then can use this to get **all** the results from a resource:: diff --git a/gcloud/storage/test_blob.py b/gcloud/storage/test_blob.py index 94b8faab24b2..bbb7147bebd5 100644 --- a/gcloud/storage/test_blob.py +++ b/gcloud/storage/test_blob.py @@ -17,12 +17,9 @@ class Test_Blob(unittest2.TestCase): - def _getTargetClass(self): - from gcloud.storage.blob import Blob - return Blob - def _makeOne(self, *args, **kw): - return self._getTargetClass()(*args, **kw) + from gcloud.storage.blob import Blob + return Blob(*args, **kw) def test_ctor_defaults(self): blob = self._makeOne() @@ -44,24 +41,22 @@ def test_ctor_explicit(self): self.assertEqual(blob.properties, properties) self.assertTrue(blob._acl is None) - def test_from_dict_defaults(self): + def test_ctor_no_name_defaults(self): BLOB_NAME = 'blob-name' properties = {'key': 'value', 'name': BLOB_NAME} - klass = self._getTargetClass() - blob = klass.from_dict(properties) + blob = self._makeOne(properties=properties) self.assertEqual(blob.bucket, None) self.assertEqual(blob.connection, None) self.assertEqual(blob.name, BLOB_NAME) self.assertEqual(blob.properties, properties) self.assertTrue(blob._acl is None) - def test_from_dict_explicit(self): + def test_ctor_no_name_explicit(self): BLOB_NAME = 'blob-name' connection = _Connection() bucket = _Bucket(connection) properties = {'key': 'value', 'name': BLOB_NAME} - klass = self._getTargetClass() - blob = klass.from_dict(properties, bucket) + blob = self._makeOne(properties=properties, bucket=bucket) self.assertTrue(blob.bucket is bucket) self.assertTrue(blob.connection is connection) self.assertEqual(blob.name, BLOB_NAME) @@ -878,7 +873,8 @@ def get_blob(self, blob): def copy_blob(self, blob, destination_bucket, new_name): destination_bucket._blobs[new_name] = self._blobs[blob.name] - return blob.from_dict({'name': new_name}, bucket=destination_bucket) + return blob.__class__(properties={'name': new_name}, + bucket=destination_bucket) def delete_blob(self, blob): del self._blobs[blob.name] diff --git a/gcloud/storage/test_bucket.py b/gcloud/storage/test_bucket.py index 9fd90d4c3ef5..08f000667370 100644 --- a/gcloud/storage/test_bucket.py +++ b/gcloud/storage/test_bucket.py @@ -62,12 +62,9 @@ def test_get_items_from_response_non_empty(self): class Test_Bucket(unittest2.TestCase): - def _getTargetClass(self): - from gcloud.storage.bucket import Bucket - return Bucket - def _makeOne(self, *args, **kw): - return self._getTargetClass()(*args, **kw) + from gcloud.storage.bucket import Bucket + return Bucket(*args, **kw) def test_ctor_defaults(self): bucket = self._makeOne() @@ -88,23 +85,21 @@ def test_ctor_explicit(self): self.assertTrue(bucket._acl is None) self.assertTrue(bucket._default_object_acl is None) - def test_from_dict_defaults(self): + def test_ctor_no_name_defaults(self): NAME = 'name' properties = {'key': 'value', 'name': NAME} - klass = self._getTargetClass() - bucket = klass.from_dict(properties) + bucket = self._makeOne(properties=properties) self.assertEqual(bucket.connection, None) self.assertEqual(bucket.name, NAME) self.assertEqual(bucket.properties, properties) self.assertTrue(bucket._acl is None) self.assertTrue(bucket._default_object_acl is None) - def test_from_dict_explicit(self): + def test_ctor_no_name_explicit(self): NAME = 'name' connection = _Connection() properties = {'key': 'value', 'name': NAME} - klass = self._getTargetClass() - bucket = klass.from_dict(properties, connection) + bucket = self._makeOne(connection=connection, properties=properties) self.assertTrue(bucket.connection is connection) self.assertEqual(bucket.name, NAME) self.assertEqual(bucket.properties, properties)