From 41223d0c0f8a39ebaf0675c1a9c3ab5b2358ee12 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Sat, 31 Jan 2015 15:21:00 -0800 Subject: [PATCH 1/2] Removing Connection.new_bucket. --- gcloud/storage/connection.py | 54 +++++++------------------------ gcloud/storage/test_connection.py | 33 ------------------- 2 files changed, 11 insertions(+), 76 deletions(-) diff --git a/gcloud/storage/connection.py b/gcloud/storage/connection.py index 1a20711e6fbe..6bd7a93fdd86 100644 --- a/gcloud/storage/connection.py +++ b/gcloud/storage/connection.py @@ -296,7 +296,7 @@ def get_bucket(self, bucket_name): :returns: The bucket matching the name provided. :raises: :class:`gcloud.exceptions.NotFound` """ - bucket = self.new_bucket(bucket_name) + bucket = Bucket(connection=self, name=bucket_name) response = self.api_request(method='GET', path=bucket.path) return Bucket(properties=response, connection=self) @@ -326,7 +326,7 @@ def lookup(self, bucket_name): except NotFound: return None - def create_bucket(self, bucket): + def create_bucket(self, bucket_name): """Create a new bucket. For example:: @@ -337,34 +337,27 @@ def create_bucket(self, bucket): >>> print bucket - :type bucket: string or :class:`gcloud.storage.bucket.Bucket` - :param bucket: The bucket name (or bucket object) to create. + :type bucket_name: string + :param bucket_name: The bucket name to create. :rtype: :class:`gcloud.storage.bucket.Bucket` :returns: The newly created bucket. :raises: :class:`gcloud.exceptions.Conflict` if there is a confict (bucket already exists, invalid name, etc.) """ - bucket = self.new_bucket(bucket) response = self.api_request(method='POST', path='/b', - data={'name': bucket.name}) + data={'name': bucket_name}) return Bucket(properties=response, connection=self) - def delete_bucket(self, bucket): + def delete_bucket(self, bucket_name): """Delete a bucket. - You can use this method to delete a bucket by name, or to delete - a bucket object:: + You can use this method to delete a bucket by name. >>> from gcloud import storage >>> connection = storage.get_connection(project) >>> connection.delete_bucket('my-bucket') - You can also delete pass in the bucket object:: - - >>> bucket = connection.get_bucket('other-bucket') - >>> connection.delete_bucket(bucket) - If the bucket doesn't exist, this will raise a :class:`gcloud.exceptions.NotFound`:: @@ -383,36 +376,11 @@ def delete_bucket(self, bucket): >>> except Conflict: >>> print 'That bucket is not empty!' - :type bucket: string or :class:`gcloud.storage.bucket.Bucket` - :param bucket: The bucket name (or bucket object) to delete. - """ - bucket = self.new_bucket(bucket) - self.api_request(method='DELETE', path=bucket.path) - - def new_bucket(self, bucket): - """Factory method for creating a new (unsaved) bucket object. - - This method is really useful when you're not sure whether you - have an actual :class:`gcloud.storage.bucket.Bucket` object or - just a name of a bucket. It always returns the object:: - - >>> bucket = connection.new_bucket('bucket') - >>> print bucket - - >>> bucket = connection.new_bucket(bucket) - >>> print bucket - - - :type bucket: string or :class:`gcloud.storage.bucket.Bucket` - :param bucket: A name of a bucket or an existing Bucket object. + :type bucket_name: string + :param bucket_name: The bucket name to delete. """ - if isinstance(bucket, Bucket): - return bucket - - if isinstance(bucket, six.string_types): - return Bucket(connection=self, name=bucket) - - raise TypeError('Invalid bucket: %s' % bucket) + bucket_path = '/b/' + bucket_name + self.api_request(method='DELETE', path=bucket_path) class _BucketIterator(Iterator): diff --git a/gcloud/storage/test_connection.py b/gcloud/storage/test_connection.py index a7c4ddf85148..74c5d9dfc005 100644 --- a/gcloud/storage/test_connection.py +++ b/gcloud/storage/test_connection.py @@ -505,12 +505,6 @@ def test_create_bucket_ok(self): def test_delete_bucket_defaults_miss(self): _deleted_blobs = [] - class _Bucket(object): - - def __init__(self, name): - self._name = name - self.path = '/b/' + name - PROJECT = 'project' BLOB_NAME = 'blob-name' conn = self._makeOne(PROJECT) @@ -526,38 +520,11 @@ def __init__(self, name): '{}', ) - def _new_bucket(name): - return _Bucket(name) - - conn.new_bucket = _new_bucket self.assertEqual(conn.delete_bucket(BLOB_NAME), None) self.assertEqual(_deleted_blobs, []) self.assertEqual(http._called_with['method'], 'DELETE') self.assertEqual(http._called_with['uri'], URI) - def test_new_bucket_w_existing(self): - from gcloud.storage.bucket import Bucket - PROJECT = 'project' - BLOB_NAME = 'blob-name' - conn = self._makeOne(PROJECT) - existing = Bucket(self, BLOB_NAME) - self.assertTrue(conn.new_bucket(existing) is existing) - - def test_new_bucket_w_blob(self): - from gcloud.storage.bucket import Bucket - PROJECT = 'project' - BLOB_NAME = 'blob-name' - conn = self._makeOne(PROJECT) - bucket = conn.new_bucket(BLOB_NAME) - self.assertTrue(isinstance(bucket, Bucket)) - self.assertTrue(bucket.connection is conn) - self.assertEqual(bucket.name, BLOB_NAME) - - def test_new_bucket_w_invalid(self): - PROJECT = 'project' - conn = self._makeOne(PROJECT) - self.assertRaises(TypeError, conn.new_bucket, object()) - class Test__BucketIterator(unittest2.TestCase): From 4fec0fbf6587f2edf3595a12b6ca9578ce23c138 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Tue, 3 Feb 2015 12:59:48 -0800 Subject: [PATCH 2/2] Adding Bucket.path_helper static method for external use. This covers callers which want a bucket path but don't want to create a full-flegded Bucket object. See #586 for context. --- gcloud/storage/bucket.py | 14 +++++++++++++- gcloud/storage/connection.py | 3 +-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index edb0e7e4b577..f2def041e549 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -128,13 +128,25 @@ def connection(self): """ return self._connection + @staticmethod + def path_helper(bucket_name): + """Relative URL path for a bucket. + + :type bucket_name: string + :param bucket_name: The bucket name in the path. + + :rtype: string + :returns: The relative URL path for ``bucket_name``. + """ + return '/b/' + bucket_name + @property def path(self): """The URL path to this bucket.""" if not self.name: raise ValueError('Cannot determine path without bucket name.') - return '/b/' + self.name + return self.path_helper(self.name) def get_blob(self, blob): """Get a blob object by name. diff --git a/gcloud/storage/connection.py b/gcloud/storage/connection.py index 6bd7a93fdd86..dd51ccc9c9c2 100644 --- a/gcloud/storage/connection.py +++ b/gcloud/storage/connection.py @@ -22,7 +22,6 @@ from gcloud.exceptions import NotFound from gcloud.storage.bucket import Bucket from gcloud.storage.iterator import Iterator -import six class Connection(_Base): @@ -379,7 +378,7 @@ def delete_bucket(self, bucket_name): :type bucket_name: string :param bucket_name: The bucket name to delete. """ - bucket_path = '/b/' + bucket_name + bucket_path = Bucket.path_helper(bucket_name) self.api_request(method='DELETE', path=bucket_path)