Skip to content

Commit a6317d1

Browse files
committed
Merge pull request #602 from dhermes/fix-590
Removing __iter__ and __contains__ from Connection in storage.
2 parents ab0a587 + 45fd420 commit a6317d1

File tree

5 files changed

+55
-106
lines changed

5 files changed

+55
-106
lines changed

docs/_components/storage-getting-started.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ The :class:`Connection <gcloud.storage.connection.Connection>` object
190190
itself is iterable, so you can loop over it, or call ``list`` on it to get
191191
a list object::
192192

193-
>>> for bucket in connection:
193+
>>> for bucket in connection.get_all_buckets():
194194
... print bucket.name
195195
>>> print list(connection)
196196

gcloud/storage/bucket.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ def __iter__(self):
105105
def __contains__(self, blob):
106106
return self.get_blob(blob) is not None
107107

108+
def exists(self):
109+
"""Determines whether or not this bucket exists.
110+
111+
:rtype: boolean
112+
:returns: True if the bucket exists in Cloud Storage.
113+
"""
114+
try:
115+
self.connection.get_bucket(self.name)
116+
return True
117+
except NotFound:
118+
return False
119+
108120
@property
109121
def acl(self):
110122
"""Create our ACL on demand."""

gcloud/storage/connection.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from gcloud.connection import Connection as _Base
2222
from gcloud.exceptions import make_exception
23-
from gcloud.exceptions import NotFound
2423
from gcloud.storage.bucket import Bucket
2524
from gcloud.storage.iterator import Iterator
2625

@@ -57,15 +56,9 @@ class Connection(_Base):
5756
A :class:`Connection` is actually iterable and will return the
5857
:class:`gcloud.storage.bucket.Bucket` objects inside the project::
5958
60-
>>> for bucket in connection:
59+
>>> for bucket in connection.get_all_buckets():
6160
>>> print bucket
6261
<Bucket: my-bucket-name>
63-
64-
In that same way, you can check for whether a bucket exists inside
65-
the project using Python's ``in`` operator::
66-
67-
>>> print 'my-bucket-name' in connection
68-
True
6962
"""
7063

7164
API_VERSION = 'v1'
@@ -82,16 +75,6 @@ def __init__(self, project, *args, **kwargs):
8275
super(Connection, self).__init__(*args, **kwargs)
8376
self.project = project
8477

85-
def __iter__(self):
86-
return iter(_BucketIterator(connection=self))
87-
88-
def __contains__(self, bucket_name):
89-
try:
90-
self.get_bucket(bucket_name)
91-
return True
92-
except NotFound:
93-
return False
94-
9578
def build_api_url(self, path, query_params=None, api_base_url=None,
9679
api_version=None, upload=False):
9780
"""Construct an API url given a few components, some optional.
@@ -265,14 +248,11 @@ def get_all_buckets(self):
265248
>>> connection = storage.get_connection(project)
266249
>>> for bucket in connection.get_all_buckets():
267250
>>> print bucket
268-
>>> # ... is the same as ...
269-
>>> for bucket in connection:
270-
>>> print bucket
271251
272252
:rtype: list of :class:`gcloud.storage.bucket.Bucket` objects.
273253
:returns: All buckets belonging to this project.
274254
"""
275-
return list(self)
255+
return iter(_BucketIterator(connection=self))
276256

277257
def get_bucket(self, bucket_name):
278258
"""Get a bucket by name.

gcloud/storage/test_bucket.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,39 @@ def test___contains___hit(self):
151151
self.assertEqual(kw['method'], 'GET')
152152
self.assertEqual(kw['path'], '/b/%s/o/%s' % (NAME, BLOB_NAME))
153153

154+
def test_exists_miss(self):
155+
from gcloud.exceptions import NotFound
156+
157+
class _FakeConnection(object):
158+
159+
_called_with = []
160+
161+
@classmethod
162+
def get_bucket(cls, bucket_name):
163+
cls._called_with.append(bucket_name)
164+
raise NotFound(bucket_name)
165+
166+
NAME = 'name'
167+
bucket = self._makeOne(connection=_FakeConnection, name=NAME)
168+
self.assertFalse(bucket.exists())
169+
self.assertEqual(_FakeConnection._called_with, [NAME])
170+
171+
def test_exists_hit(self):
172+
class _FakeConnection(object):
173+
174+
_called_with = []
175+
176+
@classmethod
177+
def get_bucket(cls, bucket_name):
178+
cls._called_with.append(bucket_name)
179+
# exists() does not use the return value
180+
return object()
181+
182+
NAME = 'name'
183+
bucket = self._makeOne(connection=_FakeConnection, name=NAME)
184+
self.assertTrue(bucket.exists())
185+
self.assertEqual(_FakeConnection._called_with, [NAME])
186+
154187
def test_acl_property(self):
155188
from gcloud.storage.acl import BucketACL
156189
bucket = self._makeOne()

gcloud/storage/test_connection.py

Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -63,82 +63,6 @@ def authorize(self, http):
6363
self.assertTrue(conn.http is authorized)
6464
self.assertTrue(isinstance(creds._called_with, httplib2.Http))
6565

66-
def test___iter___empty(self):
67-
PROJECT = 'project'
68-
conn = self._makeOne(PROJECT)
69-
URI = '/'.join([
70-
conn.API_BASE_URL,
71-
'storage',
72-
conn.API_VERSION,
73-
'b?project=%s' % PROJECT,
74-
])
75-
http = conn._http = Http(
76-
{'status': '200', 'content-type': 'application/json'},
77-
'{}',
78-
)
79-
blobs = list(conn)
80-
self.assertEqual(len(blobs), 0)
81-
self.assertEqual(http._called_with['method'], 'GET')
82-
self.assertEqual(http._called_with['uri'], URI)
83-
84-
def test___iter___non_empty(self):
85-
PROJECT = 'project'
86-
BLOB_NAME = 'blob-name'
87-
conn = self._makeOne(PROJECT)
88-
URI = '/'.join([
89-
conn.API_BASE_URL,
90-
'storage',
91-
conn.API_VERSION,
92-
'b?project=%s' % PROJECT,
93-
])
94-
http = conn._http = Http(
95-
{'status': '200', 'content-type': 'application/json'},
96-
'{"items": [{"name": "%s"}]}' % BLOB_NAME,
97-
)
98-
blobs = list(conn)
99-
self.assertEqual(len(blobs), 1)
100-
self.assertEqual(blobs[0].name, BLOB_NAME)
101-
self.assertEqual(http._called_with['method'], 'GET')
102-
self.assertEqual(http._called_with['uri'], URI)
103-
104-
def test___contains___miss(self):
105-
PROJECT = 'project'
106-
NONESUCH = 'nonesuch'
107-
conn = self._makeOne(PROJECT)
108-
URI = '/'.join([
109-
conn.API_BASE_URL,
110-
'storage',
111-
conn.API_VERSION,
112-
'b',
113-
'nonesuch?project=%s' % PROJECT,
114-
])
115-
http = conn._http = Http(
116-
{'status': '404', 'content-type': 'application/json'},
117-
'{}',
118-
)
119-
self.assertFalse(NONESUCH in conn)
120-
self.assertEqual(http._called_with['method'], 'GET')
121-
self.assertEqual(http._called_with['uri'], URI)
122-
123-
def test___contains___hit(self):
124-
PROJECT = 'project'
125-
BLOB_NAME = 'blob-name'
126-
conn = self._makeOne(PROJECT)
127-
URI = '/'.join([
128-
conn.API_BASE_URL,
129-
'storage',
130-
conn.API_VERSION,
131-
'b',
132-
'%s?project=%s' % (BLOB_NAME, PROJECT),
133-
])
134-
http = conn._http = Http(
135-
{'status': '200', 'content-type': 'application/json'},
136-
'{"name": "%s"}' % BLOB_NAME,
137-
)
138-
self.assertTrue(BLOB_NAME in conn)
139-
self.assertEqual(http._called_with['method'], 'GET')
140-
self.assertEqual(http._called_with['uri'], URI)
141-
14266
def test_build_api_url_no_extra_query_params(self):
14367
PROJECT = 'project'
14468
conn = self._makeOne(PROJECT)
@@ -370,14 +294,14 @@ def test_get_all_buckets_empty(self):
370294
{'status': '200', 'content-type': 'application/json'},
371295
'{}',
372296
)
373-
blobs = conn.get_all_buckets()
374-
self.assertEqual(len(blobs), 0)
297+
buckets = list(conn.get_all_buckets())
298+
self.assertEqual(len(buckets), 0)
375299
self.assertEqual(http._called_with['method'], 'GET')
376300
self.assertEqual(http._called_with['uri'], URI)
377301

378302
def test_get_all_buckets_non_empty(self):
379303
PROJECT = 'project'
380-
BLOB_NAME = 'blob-name'
304+
BUCKET_NAME = 'bucket-name'
381305
conn = self._makeOne(PROJECT)
382306
URI = '/'.join([
383307
conn.API_BASE_URL,
@@ -387,11 +311,11 @@ def test_get_all_buckets_non_empty(self):
387311
])
388312
http = conn._http = Http(
389313
{'status': '200', 'content-type': 'application/json'},
390-
'{"items": [{"name": "%s"}]}' % BLOB_NAME,
314+
'{"items": [{"name": "%s"}]}' % BUCKET_NAME,
391315
)
392-
blobs = conn.get_all_buckets()
393-
self.assertEqual(len(blobs), 1)
394-
self.assertEqual(blobs[0].name, BLOB_NAME)
316+
buckets = list(conn.get_all_buckets())
317+
self.assertEqual(len(buckets), 1)
318+
self.assertEqual(buckets[0].name, BUCKET_NAME)
395319
self.assertEqual(http._called_with['method'], 'GET')
396320
self.assertEqual(http._called_with['uri'], URI)
397321

0 commit comments

Comments
 (0)