Skip to content

Commit 4f29529

Browse files
committed
Merge pull request #584 from dhermes/use-default-bucket
Using a default bucket instead of bucket name in storage.
2 parents f854cd1 + 38427df commit 4f29529

File tree

3 files changed

+78
-49
lines changed

3 files changed

+78
-49
lines changed

gcloud/storage/__init__.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
from gcloud import credentials
4343
from gcloud.storage import _implicit_environ
44+
from gcloud.storage.bucket import Bucket
4445
from gcloud.storage.connection import Connection
4546

4647

@@ -52,23 +53,30 @@
5253
_PROJECT_ENV_VAR_NAME = 'GCLOUD_PROJECT'
5354

5455

55-
def set_default_bucket_name(bucket_name=None):
56-
"""Set default bucket name either explicitly or implicitly as fall-back.
56+
def set_default_bucket(bucket=None):
57+
"""Set default bucket either explicitly or implicitly as fall-back.
5758
5859
In implicit case, currently only supports enviroment variable but will
5960
support App Engine, Compute Engine and other environments in the future.
6061
62+
In the implicit case, relies on an implicit connection in addition to the
63+
implicit bucket name.
64+
6165
Local environment variable used is:
6266
- GCLOUD_BUCKET_NAME
6367
64-
:type bucket_name: string
65-
:param bucket_name: Optional. The bucket name to use as default.
68+
:type bucket: :class:`gcloud.storage.bucket.Bucket`
69+
:param bucket: Optional. The bucket to use as default.
6670
"""
67-
if bucket_name is None:
71+
if bucket is None:
6872
bucket_name = os.getenv(_BUCKET_ENV_VAR_NAME)
73+
connection = _implicit_environ.CONNECTION
6974

70-
if bucket_name is not None:
71-
_implicit_environ.BUCKET_NAME = bucket_name
75+
if bucket_name is not None and connection is not None:
76+
bucket = Bucket(connection=connection, name=bucket_name)
77+
78+
if bucket is not None:
79+
_implicit_environ.BUCKET = bucket
7280

7381

7482
def set_default_project(project=None):
@@ -96,7 +104,7 @@ def set_default_connection(project=None, connection=None):
96104
:type project: string
97105
:param project: Optional. The name of the project to connect to.
98106
99-
:type connection: :class:`gcloud.datastore.connection.Connection`
107+
:type connection: :class:`gcloud.storage.connection.Connection`
100108
:param connection: A connection provided to be the default.
101109
"""
102110
if project is None:
@@ -106,25 +114,27 @@ def set_default_connection(project=None, connection=None):
106114
_implicit_environ.CONNECTION = connection
107115

108116

109-
def set_defaults(bucket_name=None, project=None, connection=None):
117+
def set_defaults(bucket=None, project=None, connection=None):
110118
"""Set defaults either explicitly or implicitly as fall-back.
111119
112120
Uses the arguments to call the individual default methods.
113121
114-
:type bucket_name: string
115-
:param bucket_name: Optional. The bucket name to use as default.
122+
:type bucket: :class:`gcloud.storage.bucket.Bucket`
123+
:param bucket: Optional. The bucket to use as default.
116124
117125
:type project: string
118126
:param project: Optional. The name of the project to connect to.
119127
120-
:type connection: :class:`gcloud.datastore.connection.Connection`
121-
:param connection: A connection provided to be the default.
128+
:type connection: :class:`gcloud.storage.connection.Connection`
129+
:param connection: Optional. A connection provided to be the default.
122130
"""
123-
set_default_bucket_name(bucket_name=bucket_name)
124131
# NOTE: `set_default_project` is called before `set_default_connection`
125132
# since `set_default_connection` falls back to implicit project.
126133
set_default_project(project=project)
127134
set_default_connection(project=project, connection=connection)
135+
# NOTE: `set_default_bucket` is called after `set_default_connection`
136+
# since `set_default_bucket` falls back to implicit connection.
137+
set_default_bucket(bucket=bucket)
128138

129139

130140
def get_connection(project):

gcloud/storage/_implicit_environ.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
PROJECT = None
2323
"""Module global to allow persistent implied project from enviroment."""
2424

25-
BUCKET_NAME = None
26-
"""Module global to allow persistent implied bucket name from enviroment."""
25+
BUCKET = None
26+
"""Module global to allow persistent implied bucket from enviroment."""
2727

2828
CONNECTION = None
2929
"""Module global to allow persistent implied connection from enviroment."""

gcloud/storage/test___init__.py

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,68 +71,87 @@ def get_connection(*args, **kw):
7171
self.assertEqual(connection._called_with, BUCKET)
7272

7373

74-
class Test_set_default_bucket_name(unittest2.TestCase):
74+
class Test_set_default_bucket(unittest2.TestCase):
7575

7676
def setUp(self):
7777
from gcloud.storage import _implicit_environ
78-
self._replaced_bucket_name = _implicit_environ.BUCKET_NAME
79-
_implicit_environ.BUCKET_NAME = None
78+
self._replaced_bucket = _implicit_environ.BUCKET
79+
_implicit_environ.BUCKET = None
8080

8181
def tearDown(self):
8282
from gcloud.storage import _implicit_environ
83-
_implicit_environ.BUCKET_NAME = self._replaced_bucket_name
83+
_implicit_environ.BUCKET = self._replaced_bucket
8484

85-
def _callFUT(self, bucket_name=None):
86-
from gcloud.storage import set_default_bucket_name
87-
return set_default_bucket_name(bucket_name=bucket_name)
85+
def _callFUT(self, bucket=None):
86+
from gcloud.storage import set_default_bucket
87+
return set_default_bucket(bucket=bucket)
8888

89-
def _monkey(self, implicit_bucket_name):
89+
def _monkey(self, implicit_bucket_name, connection=None):
90+
from contextlib import nested
9091
import os
91-
from gcloud.storage import _BUCKET_ENV_VAR_NAME
92+
9293
from gcloud._testing import _Monkey
94+
from gcloud.storage import _BUCKET_ENV_VAR_NAME
95+
from gcloud.storage import _implicit_environ
96+
9397
environ = {_BUCKET_ENV_VAR_NAME: implicit_bucket_name}
94-
return _Monkey(os, getenv=environ.get)
98+
return nested(_Monkey(os, getenv=environ.get),
99+
_Monkey(_implicit_environ, CONNECTION=connection))
95100

96101
def test_no_env_var_set(self):
97102
from gcloud.storage import _implicit_environ
98103
with self._monkey(None):
99104
self._callFUT()
100-
self.assertEqual(_implicit_environ.BUCKET_NAME, None)
105+
self.assertEqual(_implicit_environ.BUCKET, None)
101106

102107
def test_set_from_env_var(self):
103108
from gcloud.storage import _implicit_environ
104109
IMPLICIT_BUCKET_NAME = 'IMPLICIT'
105-
with self._monkey(IMPLICIT_BUCKET_NAME):
110+
CONNECTION = object()
111+
with self._monkey(IMPLICIT_BUCKET_NAME, connection=CONNECTION):
106112
self._callFUT()
107-
self.assertEqual(_implicit_environ.BUCKET_NAME, IMPLICIT_BUCKET_NAME)
113+
114+
self.assertEqual(_implicit_environ.BUCKET.name, IMPLICIT_BUCKET_NAME)
115+
self.assertEqual(_implicit_environ.BUCKET.connection, CONNECTION)
108116

109117
def test_set_explicit_w_env_var_set(self):
110118
from gcloud.storage import _implicit_environ
111-
EXPLICIT_BUCKET_NAME = 'EXPLICIT'
119+
EXPLICIT_BUCKET = object()
112120
with self._monkey(None):
113-
self._callFUT(EXPLICIT_BUCKET_NAME)
114-
self.assertEqual(_implicit_environ.BUCKET_NAME, EXPLICIT_BUCKET_NAME)
121+
self._callFUT(EXPLICIT_BUCKET)
122+
self.assertEqual(_implicit_environ.BUCKET, EXPLICIT_BUCKET)
115123

116124
def test_set_explicit_no_env_var_set(self):
117125
from gcloud.storage import _implicit_environ
118126
IMPLICIT_BUCKET_NAME = 'IMPLICIT'
119-
EXPLICIT_BUCKET_NAME = 'EXPLICIT'
120-
with self._monkey(IMPLICIT_BUCKET_NAME):
121-
self._callFUT(EXPLICIT_BUCKET_NAME)
122-
self.assertEqual(_implicit_environ.BUCKET_NAME, EXPLICIT_BUCKET_NAME)
127+
CONNECTION = object()
128+
EXPLICIT_BUCKET = object()
129+
with self._monkey(IMPLICIT_BUCKET_NAME, connection=CONNECTION):
130+
self._callFUT(EXPLICIT_BUCKET)
131+
self.assertEqual(_implicit_environ.BUCKET, EXPLICIT_BUCKET)
123132

124133
def test_set_explicit_None_wo_env_var_set(self):
125134
from gcloud.storage import _implicit_environ
126-
with self._monkey(None):
135+
CONNECTION = object()
136+
with self._monkey(None, connection=CONNECTION):
137+
self._callFUT(None)
138+
self.assertEqual(_implicit_environ.BUCKET, None)
139+
140+
def test_set_explicit_None_wo_connection_set(self):
141+
from gcloud.storage import _implicit_environ
142+
IMPLICIT_BUCKET_NAME = 'IMPLICIT'
143+
with self._monkey(IMPLICIT_BUCKET_NAME, connection=None):
127144
self._callFUT(None)
128-
self.assertEqual(_implicit_environ.BUCKET_NAME, None)
145+
self.assertEqual(_implicit_environ.BUCKET, None)
129146

130147
def test_set_explicit_None_w_env_var_set(self):
131148
from gcloud.storage import _implicit_environ
132149
IMPLICIT_BUCKET_NAME = 'IMPLICIT'
133-
with self._monkey(IMPLICIT_BUCKET_NAME):
150+
CONNECTION = object()
151+
with self._monkey(IMPLICIT_BUCKET_NAME, connection=CONNECTION):
134152
self._callFUT(None)
135-
self.assertEqual(_implicit_environ.BUCKET_NAME, IMPLICIT_BUCKET_NAME)
153+
self.assertEqual(_implicit_environ.BUCKET.name, IMPLICIT_BUCKET_NAME)
154+
self.assertEqual(_implicit_environ.BUCKET.connection, CONNECTION)
136155

137156

138157
class Test_set_default_project(unittest2.TestCase):
@@ -299,23 +318,23 @@ def mock_get_connection(*args, **kwargs):
299318

300319
class Test_set_defaults(unittest2.TestCase):
301320

302-
def _callFUT(self, bucket_name=None, project=None, connection=None):
321+
def _callFUT(self, bucket=None, project=None, connection=None):
303322
from gcloud.storage import set_defaults
304-
return set_defaults(bucket_name=bucket_name, project=project,
323+
return set_defaults(bucket=bucket, project=project,
305324
connection=connection)
306325

307326
def test_it(self):
308327
from gcloud._testing import _Monkey
309328
from gcloud import storage
310329

311-
BUCKET_NAME = object()
330+
BUCKET = object()
312331
PROJECT = object()
313332
CONNECTION = object()
314333

315-
SET_BUCKET_NAME_CALLED = []
334+
SET_BUCKET_CALLED = []
316335

317-
def call_set_bucket_name(bucket_name=None):
318-
SET_BUCKET_NAME_CALLED.append(bucket_name)
336+
def call_set_bucket(bucket=None):
337+
SET_BUCKET_CALLED.append(bucket)
319338

320339
SET_PROJECT_CALLED = []
321340

@@ -327,12 +346,12 @@ def call_set_project(project=None):
327346
def call_set_connection(project=None, connection=None):
328347
SET_CONNECTION_CALLED.append((project, connection))
329348

330-
with _Monkey(storage, set_default_bucket_name=call_set_bucket_name,
349+
with _Monkey(storage, set_default_bucket=call_set_bucket,
331350
set_default_connection=call_set_connection,
332351
set_default_project=call_set_project):
333-
self._callFUT(bucket_name=BUCKET_NAME, project=PROJECT,
352+
self._callFUT(bucket=BUCKET, project=PROJECT,
334353
connection=CONNECTION)
335354

336-
self.assertEqual(SET_BUCKET_NAME_CALLED, [BUCKET_NAME])
337355
self.assertEqual(SET_PROJECT_CALLED, [PROJECT])
338356
self.assertEqual(SET_CONNECTION_CALLED, [(PROJECT, CONNECTION)])
357+
self.assertEqual(SET_BUCKET_CALLED, [BUCKET])

0 commit comments

Comments
 (0)