Skip to content

Commit 05dfb97

Browse files
committed
Adding connection lazy loading in storage.
1 parent 3f8d433 commit 05dfb97

File tree

8 files changed

+47
-10
lines changed

8 files changed

+47
-10
lines changed

README.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ to Cloud Storage using this Client Library.
9999
.. code:: python
100100
101101
from gcloud import storage
102-
storage.set_defaults()
103102
bucket = storage.get_bucket('bucket-id-here')
104103
# Then do other things...
105104
blob = bucket.get_blob('/remote/path/to/file.txt')

docs/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ Cloud Storage
5151
.. code-block:: python
5252
5353
from gcloud import storage
54-
storage.set_defaults()
5554
bucket = storage.get_bucket('<your-bucket-name>')
5655
blob = storage.Blob('my-test-file.txt', bucket=bucket)
5756
blob = blob.upload_contents_from_string('this is test content!')

gcloud/pubsub/test__implicit_environ.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class Test_get_default_connection(unittest2.TestCase):
1919

2020
def _callFUT(self):
21-
from gcloud.storage._implicit_environ import get_default_connection
21+
from gcloud.pubsub._implicit_environ import get_default_connection
2222
return get_default_connection()
2323

2424
def test_wo_override(self):

gcloud/storage/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
You'll typically use these to get started with the API:
1818
1919
>>> from gcloud import storage
20-
>>> storage.set_defaults()
2120
>>> bucket = storage.get_bucket('bucket-id-here')
2221
>>> # Then do other things...
2322
>>> blob = bucket.get_blob('/remote/path/to/file.txt')

gcloud/storage/_implicit_environ.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""
2020

2121

22+
from gcloud._helpers import _lazy_property_deco
2223
from gcloud.connection import get_scoped_connection
2324
from gcloud.storage.connection import Connection
2425

@@ -38,9 +39,16 @@ class _DefaultsContainer(object):
3839
:param connection: Persistent implied connection from environment.
3940
"""
4041

41-
def __init__(self, bucket=None, connection=None):
42+
@_lazy_property_deco
43+
@staticmethod
44+
def connection():
45+
"""Return the implicit default connection.."""
46+
return get_connection()
47+
48+
def __init__(self, bucket=None, connection=None, implicit=False):
4249
self.bucket = bucket
43-
self.connection = connection
50+
if connection is not None or not implicit:
51+
self.connection = connection
4452

4553

4654
def get_default_bucket():
@@ -88,4 +96,4 @@ def set_default_connection(connection=None):
8896
_DEFAULTS.connection = connection
8997

9098

91-
_DEFAULTS = _DefaultsContainer()
99+
_DEFAULTS = _DefaultsContainer(implicit=True)

gcloud/storage/api.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def lookup_bucket(bucket_name, connection=None):
3333
than catching an exception::
3434
3535
>>> from gcloud import storage
36-
>>> storage.set_defaults()
3736
>>> bucket = storage.lookup_bucket('doesnt-exist')
3837
>>> print bucket
3938
None
@@ -172,7 +171,6 @@ def create_bucket(bucket_name, project=None, connection=None):
172171
For example::
173172
174173
>>> from gcloud import storage
175-
>>> storage.set_defaults()
176174
>>> bucket = storage.create_bucket('my-bucket')
177175
>>> print bucket
178176
<Bucket: my-bucket>

gcloud/storage/test__implicit_environ.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ def test_wo_override(self):
2727

2828
class Test_get_default_connection(unittest2.TestCase):
2929

30+
def setUp(self):
31+
from gcloud.storage._testing import _setup_defaults
32+
_setup_defaults(self)
33+
34+
def tearDown(self):
35+
from gcloud.storage._testing import _tear_down_defaults
36+
_tear_down_defaults(self)
37+
3038
def _callFUT(self):
3139
from gcloud.storage._implicit_environ import get_default_connection
3240
return get_default_connection()
@@ -99,3 +107,30 @@ def mock_get_connection(*args, **kwargs):
99107
self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn)
100108
self.assertEqual(_called_args, [()])
101109
self.assertEqual(_called_kwargs, [{}])
110+
111+
112+
class Test_lazy_loading(unittest2.TestCase):
113+
114+
def setUp(self):
115+
from gcloud.storage._testing import _setup_defaults
116+
_setup_defaults(self, implicit=True)
117+
118+
def tearDown(self):
119+
from gcloud.storage._testing import _tear_down_defaults
120+
_tear_down_defaults(self)
121+
122+
def test_descriptor_for_connection(self):
123+
from gcloud._testing import _Monkey
124+
from gcloud.storage import _implicit_environ
125+
126+
self.assertFalse(
127+
'connection' in _implicit_environ._DEFAULTS.__dict__)
128+
129+
DEFAULT = object()
130+
131+
with _Monkey(_implicit_environ, get_connection=lambda: DEFAULT):
132+
lazy_loaded = _implicit_environ._DEFAULTS.connection
133+
134+
self.assertEqual(lazy_loaded, DEFAULT)
135+
self.assertTrue(
136+
'connection' in _implicit_environ._DEFAULTS.__dict__)

regression/storage.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
SHARED_BUCKETS = {}
2929

3030
_helpers._PROJECT_ENV_VAR_NAME = 'GCLOUD_TESTS_PROJECT_ID'
31-
storage.set_defaults()
3231

3332

3433
def setUpModule():

0 commit comments

Comments
 (0)