Skip to content

Commit 9932f0d

Browse files
committed
Moving _require_connection from storage.api to helpers.
This is to blob and bucket can use it without import cycles.
1 parent 05dfb97 commit 9932f0d

File tree

4 files changed

+81
-79
lines changed

4 files changed

+81
-79
lines changed

gcloud/storage/_helpers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
from Crypto.Hash import MD5
2121
import base64
2222

23+
from gcloud.storage._implicit_environ import get_default_connection
24+
from gcloud.storage.batch import Batch
25+
2326

2427
class _PropertyMixin(object):
2528
"""Abstract mixin for cloud storage classes with associated propertties.
@@ -101,6 +104,30 @@ def patch(self):
101104
self._set_properties(api_response)
102105

103106

107+
def _require_connection(connection=None):
108+
"""Infer a connection from the environment, if not passed explicitly.
109+
110+
:type connection: :class:`gcloud.storage.connection.Connection`
111+
:param connection: Optional.
112+
113+
:rtype: :class:`gcloud.storage.connection.Connection`
114+
:returns: A connection based on the current environment.
115+
:raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
116+
cannot be inferred from the environment.
117+
"""
118+
# NOTE: We use current Batch directly since it inherits from Connection.
119+
if connection is None:
120+
connection = Batch.current()
121+
122+
if connection is None:
123+
connection = get_default_connection()
124+
125+
if connection is None:
126+
raise EnvironmentError('Connection could not be inferred.')
127+
128+
return connection
129+
130+
104131
def _scalar_property(fieldname):
105132
"""Create a property descriptor around the :class:`_PropertyMixin` helpers.
106133
"""

gcloud/storage/api.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
from gcloud.exceptions import NotFound
2222
from gcloud._helpers import get_default_project
23-
from gcloud.storage._implicit_environ import get_default_connection
24-
from gcloud.storage.batch import Batch
23+
from gcloud.storage._helpers import _require_connection
2524
from gcloud.storage.bucket import Bucket
2625
from gcloud.storage.iterator import Iterator
2726

@@ -227,27 +226,3 @@ def get_items_from_response(self, response):
227226
bucket = Bucket(name, connection=self.connection)
228227
bucket._set_properties(item)
229228
yield bucket
230-
231-
232-
def _require_connection(connection=None):
233-
"""Infer a connection from the environment, if not passed explicitly.
234-
235-
:type connection: :class:`gcloud.storage.connection.Connection`
236-
:param connection: Optional.
237-
238-
:rtype: :class:`gcloud.storage.connection.Connection`
239-
:returns: A connection based on the current environment.
240-
:raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
241-
cannot be inferred from the environment.
242-
"""
243-
# NOTE: We use current Batch directly since it inherits from Connection.
244-
if connection is None:
245-
connection = Batch.current()
246-
247-
if connection is None:
248-
connection = get_default_connection()
249-
250-
if connection is None:
251-
raise EnvironmentError('Connection could not be inferred.')
252-
253-
return connection

gcloud/storage/test__helpers.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,44 @@ def _patch_property(self, name, value):
122122
self.assertEqual(test._patched, ('solfege', 'Latido'))
123123

124124

125+
class Test__require_connection(unittest2.TestCase):
126+
127+
def _callFUT(self, connection=None):
128+
from gcloud.storage._helpers import _require_connection
129+
return _require_connection(connection=connection)
130+
131+
def _monkey(self, connection):
132+
from gcloud.storage._testing import _monkey_defaults
133+
return _monkey_defaults(connection=connection)
134+
135+
def test_implicit_unset(self):
136+
with self._monkey(None):
137+
with self.assertRaises(EnvironmentError):
138+
self._callFUT()
139+
140+
def test_implicit_unset_w_existing_batch(self):
141+
CONNECTION = object()
142+
with self._monkey(None):
143+
with _NoCommitBatch(connection=CONNECTION):
144+
self.assertEqual(self._callFUT(), CONNECTION)
145+
146+
def test_implicit_unset_passed_explicitly(self):
147+
CONNECTION = object()
148+
with self._monkey(None):
149+
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)
150+
151+
def test_implicit_set(self):
152+
IMPLICIT_CONNECTION = object()
153+
with self._monkey(IMPLICIT_CONNECTION):
154+
self.assertTrue(self._callFUT() is IMPLICIT_CONNECTION)
155+
156+
def test_implicit_set_passed_explicitly(self):
157+
IMPLICIT_CONNECTION = object()
158+
CONNECTION = object()
159+
with self._monkey(IMPLICIT_CONNECTION):
160+
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)
161+
162+
125163
class Test__base64_md5hash(unittest2.TestCase):
126164

127165
def _callFUT(self, bytes_to_sign):
@@ -215,3 +253,18 @@ def __init__(self):
215253
def b64encode(self, value):
216254
self._called_b64encode.append(value)
217255
return value
256+
257+
258+
class _NoCommitBatch(object):
259+
260+
def __init__(self, connection):
261+
self._connection = connection
262+
263+
def __enter__(self):
264+
from gcloud.storage.batch import _BATCHES
265+
_BATCHES.push(self._connection)
266+
return self._connection
267+
268+
def __exit__(self, *args):
269+
from gcloud.storage.batch import _BATCHES
270+
_BATCHES.pop()

gcloud/storage/test_api.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -345,44 +345,6 @@ def test_get_items_from_response_non_empty(self):
345345
self.assertEqual(bucket.name, BLOB_NAME)
346346

347347

348-
class Test__require_connection(unittest2.TestCase):
349-
350-
def _callFUT(self, connection=None):
351-
from gcloud.storage.api import _require_connection
352-
return _require_connection(connection=connection)
353-
354-
def _monkey(self, connection):
355-
from gcloud.storage._testing import _monkey_defaults
356-
return _monkey_defaults(connection=connection)
357-
358-
def test_implicit_unset(self):
359-
with self._monkey(None):
360-
with self.assertRaises(EnvironmentError):
361-
self._callFUT()
362-
363-
def test_implicit_unset_w_existing_batch(self):
364-
CONNECTION = object()
365-
with self._monkey(None):
366-
with _NoCommitBatch(connection=CONNECTION):
367-
self.assertEqual(self._callFUT(), CONNECTION)
368-
369-
def test_implicit_unset_passed_explicitly(self):
370-
CONNECTION = object()
371-
with self._monkey(None):
372-
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)
373-
374-
def test_implicit_set(self):
375-
IMPLICIT_CONNECTION = object()
376-
with self._monkey(IMPLICIT_CONNECTION):
377-
self.assertTrue(self._callFUT() is IMPLICIT_CONNECTION)
378-
379-
def test_implicit_set_passed_explicitly(self):
380-
IMPLICIT_CONNECTION = object()
381-
CONNECTION = object()
382-
with self._monkey(IMPLICIT_CONNECTION):
383-
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)
384-
385-
386348
class Http(object):
387349

388350
_called_with = None
@@ -395,18 +357,3 @@ def __init__(self, headers, content):
395357
def request(self, **kw):
396358
self._called_with = kw
397359
return self._response, self._content
398-
399-
400-
class _NoCommitBatch(object):
401-
402-
def __init__(self, connection):
403-
self._connection = connection
404-
405-
def __enter__(self):
406-
from gcloud.storage.batch import _BATCHES
407-
_BATCHES.push(self._connection)
408-
return self._connection
409-
410-
def __exit__(self, *args):
411-
from gcloud.storage.batch import _BATCHES
412-
_BATCHES.pop()

0 commit comments

Comments
 (0)