Skip to content

Commit

Permalink
Moving _require_connection from storage.api to helpers.
Browse files Browse the repository at this point in the history
This is to blob and bucket can use it without import cycles.
  • Loading branch information
dhermes committed Apr 14, 2015
1 parent 05dfb97 commit 9932f0d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 79 deletions.
27 changes: 27 additions & 0 deletions gcloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from Crypto.Hash import MD5
import base64

from gcloud.storage._implicit_environ import get_default_connection
from gcloud.storage.batch import Batch


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


def _require_connection(connection=None):
"""Infer a connection from the environment, if not passed explicitly.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: Optional.
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: A connection based on the current environment.
:raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
cannot be inferred from the environment.
"""
# NOTE: We use current Batch directly since it inherits from Connection.
if connection is None:
connection = Batch.current()

if connection is None:
connection = get_default_connection()

if connection is None:
raise EnvironmentError('Connection could not be inferred.')

return connection


def _scalar_property(fieldname):
"""Create a property descriptor around the :class:`_PropertyMixin` helpers.
"""
Expand Down
27 changes: 1 addition & 26 deletions gcloud/storage/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

from gcloud.exceptions import NotFound
from gcloud._helpers import get_default_project
from gcloud.storage._implicit_environ import get_default_connection
from gcloud.storage.batch import Batch
from gcloud.storage._helpers import _require_connection
from gcloud.storage.bucket import Bucket
from gcloud.storage.iterator import Iterator

Expand Down Expand Up @@ -227,27 +226,3 @@ def get_items_from_response(self, response):
bucket = Bucket(name, connection=self.connection)
bucket._set_properties(item)
yield bucket


def _require_connection(connection=None):
"""Infer a connection from the environment, if not passed explicitly.
:type connection: :class:`gcloud.storage.connection.Connection`
:param connection: Optional.
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: A connection based on the current environment.
:raises: :class:`EnvironmentError` if ``connection`` is ``None``, and
cannot be inferred from the environment.
"""
# NOTE: We use current Batch directly since it inherits from Connection.
if connection is None:
connection = Batch.current()

if connection is None:
connection = get_default_connection()

if connection is None:
raise EnvironmentError('Connection could not be inferred.')

return connection
53 changes: 53 additions & 0 deletions gcloud/storage/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,44 @@ def _patch_property(self, name, value):
self.assertEqual(test._patched, ('solfege', 'Latido'))


class Test__require_connection(unittest2.TestCase):

def _callFUT(self, connection=None):
from gcloud.storage._helpers import _require_connection
return _require_connection(connection=connection)

def _monkey(self, connection):
from gcloud.storage._testing import _monkey_defaults
return _monkey_defaults(connection=connection)

def test_implicit_unset(self):
with self._monkey(None):
with self.assertRaises(EnvironmentError):
self._callFUT()

def test_implicit_unset_w_existing_batch(self):
CONNECTION = object()
with self._monkey(None):
with _NoCommitBatch(connection=CONNECTION):
self.assertEqual(self._callFUT(), CONNECTION)

def test_implicit_unset_passed_explicitly(self):
CONNECTION = object()
with self._monkey(None):
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)

def test_implicit_set(self):
IMPLICIT_CONNECTION = object()
with self._monkey(IMPLICIT_CONNECTION):
self.assertTrue(self._callFUT() is IMPLICIT_CONNECTION)

def test_implicit_set_passed_explicitly(self):
IMPLICIT_CONNECTION = object()
CONNECTION = object()
with self._monkey(IMPLICIT_CONNECTION):
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)


class Test__base64_md5hash(unittest2.TestCase):

def _callFUT(self, bytes_to_sign):
Expand Down Expand Up @@ -215,3 +253,18 @@ def __init__(self):
def b64encode(self, value):
self._called_b64encode.append(value)
return value


class _NoCommitBatch(object):

def __init__(self, connection):
self._connection = connection

def __enter__(self):
from gcloud.storage.batch import _BATCHES
_BATCHES.push(self._connection)
return self._connection

def __exit__(self, *args):
from gcloud.storage.batch import _BATCHES
_BATCHES.pop()
53 changes: 0 additions & 53 deletions gcloud/storage/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,44 +345,6 @@ def test_get_items_from_response_non_empty(self):
self.assertEqual(bucket.name, BLOB_NAME)


class Test__require_connection(unittest2.TestCase):

def _callFUT(self, connection=None):
from gcloud.storage.api import _require_connection
return _require_connection(connection=connection)

def _monkey(self, connection):
from gcloud.storage._testing import _monkey_defaults
return _monkey_defaults(connection=connection)

def test_implicit_unset(self):
with self._monkey(None):
with self.assertRaises(EnvironmentError):
self._callFUT()

def test_implicit_unset_w_existing_batch(self):
CONNECTION = object()
with self._monkey(None):
with _NoCommitBatch(connection=CONNECTION):
self.assertEqual(self._callFUT(), CONNECTION)

def test_implicit_unset_passed_explicitly(self):
CONNECTION = object()
with self._monkey(None):
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)

def test_implicit_set(self):
IMPLICIT_CONNECTION = object()
with self._monkey(IMPLICIT_CONNECTION):
self.assertTrue(self._callFUT() is IMPLICIT_CONNECTION)

def test_implicit_set_passed_explicitly(self):
IMPLICIT_CONNECTION = object()
CONNECTION = object()
with self._monkey(IMPLICIT_CONNECTION):
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)


class Http(object):

_called_with = None
Expand All @@ -395,18 +357,3 @@ def __init__(self, headers, content):
def request(self, **kw):
self._called_with = kw
return self._response, self._content


class _NoCommitBatch(object):

def __init__(self, connection):
self._connection = connection

def __enter__(self):
from gcloud.storage.batch import _BATCHES
_BATCHES.push(self._connection)
return self._connection

def __exit__(self, *args):
from gcloud.storage.batch import _BATCHES
_BATCHES.pop()

0 comments on commit 9932f0d

Please sign in to comment.