Skip to content

Commit

Permalink
Merge pull request #588 from dhermes/storage-remove-lookup
Browse files Browse the repository at this point in the history
Removing Connection.lookup in storage.
  • Loading branch information
dhermes committed Feb 6, 2015
2 parents edaa5c8 + c4eb080 commit 204389a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 73 deletions.
37 changes: 6 additions & 31 deletions gcloud/storage/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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::
Expand All @@ -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
<Bucket: my-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.
Expand Down
42 changes: 0 additions & 42 deletions gcloud/storage/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 204389a

Please sign in to comment.