From c4eb0804e8473a152e072877830a0187361dd43d Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Sat, 31 Jan 2015 16:01:02 -0800 Subject: [PATCH] Removing Connection.lookup in storage. --- gcloud/storage/connection.py | 37 +++++---------------------- gcloud/storage/test_connection.py | 42 ------------------------------- 2 files changed, 6 insertions(+), 73 deletions(-) diff --git a/gcloud/storage/connection.py b/gcloud/storage/connection.py index 666f07e6acd7..cb7376a25168 100644 --- a/gcloud/storage/connection.py +++ b/gcloud/storage/connection.py @@ -86,7 +86,11 @@ def __iter__(self): return iter(_BucketIterator(connection=self)) def __contains__(self, bucket_name): - return self.lookup(bucket_name) is not None + try: + self.get_bucket(bucket_name) + return True + except NotFound: + return False def build_api_url(self, path, query_params=None, api_base_url=None, api_version=None, upload=False): @@ -274,10 +278,7 @@ def get_bucket(self, bucket_name): """Get a bucket by name. If the bucket isn't found, this will raise a - :class:`gcloud.exceptions.NotFound`. If you would - rather get a bucket by name, and return ``None`` if the bucket - isn't found (like ``{}.get('...')``) then use - :func:`Connection.lookup`. + :class:`gcloud.storage.exceptions.NotFound`. For example:: @@ -300,32 +301,6 @@ def get_bucket(self, bucket_name): response = self.api_request(method='GET', path=bucket.path) return Bucket(properties=response, connection=self) - def lookup(self, bucket_name): - """Get a bucket by name, returning None if not found. - - You can use this if you would rather checking for a None value - than catching an exception:: - - >>> from gcloud import storage - >>> connection = storage.get_connection(project) - >>> bucket = connection.get_bucket('doesnt-exist') - >>> print bucket - None - >>> bucket = connection.get_bucket('my-bucket') - >>> print bucket - - - :type bucket_name: string - :param bucket_name: The name of the bucket to get. - - :rtype: :class:`gcloud.storage.bucket.Bucket` - :returns: The bucket matching the name provided or None if not found. - """ - try: - return self.get_bucket(bucket_name) - except NotFound: - return None - def create_bucket(self, bucket_name): """Create a new bucket. diff --git a/gcloud/storage/test_connection.py b/gcloud/storage/test_connection.py index 4666a9abfa84..d261dfa2423e 100644 --- a/gcloud/storage/test_connection.py +++ b/gcloud/storage/test_connection.py @@ -438,48 +438,6 @@ def test_get_bucket_hit(self): self.assertEqual(http._called_with['method'], 'GET') self.assertEqual(http._called_with['uri'], URI) - def test_lookup_miss(self): - PROJECT = 'project' - NONESUCH = 'nonesuch' - conn = self._makeOne(PROJECT) - URI = '/'.join([ - conn.API_BASE_URL, - 'storage', - conn.API_VERSION, - 'b', - 'nonesuch?project=%s' % PROJECT, - ]) - http = conn._http = Http( - {'status': '404', 'content-type': 'application/json'}, - '{}', - ) - self.assertEqual(conn.lookup(NONESUCH), None) - self.assertEqual(http._called_with['method'], 'GET') - self.assertEqual(http._called_with['uri'], URI) - - def test_lookup_hit(self): - from gcloud.storage.bucket import Bucket - PROJECT = 'project' - BLOB_NAME = 'blob-name' - conn = self._makeOne(PROJECT) - URI = '/'.join([ - conn.API_BASE_URL, - 'storage', - conn.API_VERSION, - 'b', - '%s?project=%s' % (BLOB_NAME, PROJECT), - ]) - http = conn._http = Http( - {'status': '200', 'content-type': 'application/json'}, - '{"name": "%s"}' % BLOB_NAME, - ) - bucket = conn.lookup(BLOB_NAME) - self.assertTrue(isinstance(bucket, Bucket)) - self.assertTrue(bucket.connection is conn) - self.assertEqual(bucket.name, BLOB_NAME) - self.assertEqual(http._called_with['method'], 'GET') - self.assertEqual(http._called_with['uri'], URI) - def test_create_bucket_ok(self): from gcloud.storage.bucket import Bucket PROJECT = 'project'