Skip to content

Commit 4512eff

Browse files
committed
Merge pull request #748 from tseaver/741-hoist_default_project
#741: hoist project detection/setup into 'gcloud._helpers'.
2 parents 9c09ced + c590c56 commit 4512eff

File tree

9 files changed

+270
-221
lines changed

9 files changed

+270
-221
lines changed

gcloud/_helpers.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
1616
This module is not part of the public API surface of `gcloud`.
1717
"""
18+
import os
1819
import socket
1920

2021
try:
@@ -156,3 +157,82 @@ def _compute_engine_id():
156157
pass
157158
finally:
158159
connection.close()
160+
161+
162+
_PROJECT_ENV_VAR_NAME = 'GCLOUD_PROJECT'
163+
164+
165+
def _get_production_project():
166+
"""Gets the production project if it can be inferred."""
167+
return os.getenv(_PROJECT_ENV_VAR_NAME)
168+
169+
170+
def _determine_default_project(project=None):
171+
"""Determine default project ID explicitly or implicitly as fall-back.
172+
173+
In implicit case, currently only supports enviroment variable but will
174+
support App Engine, Compute Engine and other environments in the future.
175+
176+
Local environment variable used is:
177+
- GCLOUD_PROJECT
178+
179+
:type project: string
180+
:param project: Optional. The project name to use as default.
181+
182+
:rtype: string or ``NoneType``
183+
:returns: Default project if it can be determined.
184+
"""
185+
if project is None:
186+
project = _get_production_project()
187+
188+
return project
189+
190+
191+
def set_default_project(project=None):
192+
"""Set default project either explicitly or implicitly as fall-back.
193+
194+
:type project: string
195+
:param project: Optional. The project name to use as default.
196+
197+
:raises: :class:`EnvironmentError` if no project was found.
198+
"""
199+
project = _determine_default_project(project=project)
200+
if project is not None:
201+
_DEFAULTS.project = project
202+
else:
203+
raise EnvironmentError('No project could be inferred.')
204+
205+
206+
def get_default_project():
207+
"""Get default project.
208+
209+
:rtype: string or ``NoneType``
210+
:returns: The default project if one has been set.
211+
"""
212+
return _DEFAULTS.project
213+
214+
215+
class _DefaultsContainer(object):
216+
"""Container for defaults.
217+
218+
:type project: string
219+
:param project: Persistent implied project from environment.
220+
221+
:type implicit: boolean
222+
:param implicit: if False, assign the instance's ``project`` attribute
223+
unconditionally; otherwise, assign it only if the
224+
value is not None.
225+
"""
226+
227+
@_lazy_property_deco
228+
@staticmethod
229+
def project():
230+
"""Return the implicit default project."""
231+
return _determine_default_project()
232+
233+
def __init__(self, project=None, implicit=False):
234+
if project is not None or not implicit:
235+
self.project = project
236+
237+
238+
_DEFAULTS = _DefaultsContainer(implicit=True)

gcloud/_testing.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
"""Shared testing utilities."""
1616

17+
from gcloud import _helpers
18+
from gcloud._helpers import _DefaultsContainer
19+
1720

1821
class _Monkey(object):
1922
# context-manager for replacing module names in the scope of a test.
@@ -30,3 +33,17 @@ def __enter__(self):
3033
def __exit__(self, exc_type, exc_val, exc_tb):
3134
for key, value in self.to_restore.items():
3235
setattr(self.module, key, value)
36+
37+
38+
def _monkey_defaults(*args, **kwargs):
39+
mock_defaults = _DefaultsContainer(*args, **kwargs)
40+
return _Monkey(_helpers, _DEFAULTS=mock_defaults)
41+
42+
43+
def _setup_defaults(test_case, *args, **kwargs):
44+
test_case._replaced_defaults = _helpers._DEFAULTS
45+
_helpers._DEFAULTS = _DefaultsContainer(*args, **kwargs)
46+
47+
48+
def _tear_down_defaults(test_case):
49+
_helpers._DEFAULTS = test_case._replaced_defaults

gcloud/storage/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
import os
4242

4343
from gcloud import credentials
44+
from gcloud._helpers import get_default_project
45+
from gcloud._helpers import set_default_project
4446
from gcloud.storage import _implicit_environ
4547
from gcloud.storage._implicit_environ import get_default_bucket
4648
from gcloud.storage._implicit_environ import get_default_connection
47-
from gcloud.storage._implicit_environ import get_default_project
48-
from gcloud.storage._implicit_environ import set_default_project
4949
from gcloud.storage.api import create_bucket
5050
from gcloud.storage.api import get_all_buckets
5151
from gcloud.storage.api import get_bucket

gcloud/storage/_implicit_environ.py

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,96 +14,26 @@
1414

1515
"""Module to provide implicit behavior based on enviroment.
1616
17-
Allows the storage package to infer the current project, default bucket
18-
and connection from the enviroment.
17+
Allows the storage package to infer the default bucket and connection
18+
from the enviroment.
1919
"""
2020

2121

22-
import os
23-
24-
from gcloud._helpers import _lazy_property_deco
25-
26-
27-
_PROJECT_ENV_VAR_NAME = 'GCLOUD_PROJECT'
28-
29-
30-
def _get_production_project():
31-
"""Gets the production project if it can be inferred."""
32-
return os.getenv(_PROJECT_ENV_VAR_NAME)
33-
34-
35-
def _determine_default_project(project=None):
36-
"""Determine default project ID explicitly or implicitly as fall-back.
37-
38-
In implicit case, currently only supports enviroment variable but will
39-
support App Engine, Compute Engine and other environments in the future.
40-
41-
Local environment variable used is:
42-
- GCLOUD_PROJECT
43-
44-
:type project: string
45-
:param project: Optional. The project name to use as default.
46-
47-
:rtype: string or ``NoneType``
48-
:returns: Default project if it can be determined.
49-
"""
50-
if project is None:
51-
project = _get_production_project()
52-
53-
return project
54-
55-
56-
def set_default_project(project=None):
57-
"""Set default project either explicitly or implicitly as fall-back.
58-
59-
:type project: string
60-
:param project: Optional. The project name to use as default.
61-
62-
:raises: :class:`EnvironmentError` if no project was found.
63-
"""
64-
project = _determine_default_project(project=project)
65-
if project is not None:
66-
_DEFAULTS.project = project
67-
else:
68-
raise EnvironmentError('No project could be inferred.')
69-
70-
7122
class _DefaultsContainer(object):
7223
"""Container for defaults.
7324
74-
:type project: string
75-
:param project: Persistent implied project from environment.
76-
7725
:type bucket: :class:`gcloud.storage.bucket.Bucket`
7826
:param bucket: Persistent implied default bucket from environment.
7927
8028
:type connection: :class:`gcloud.storage.connection.Connection`
8129
:param connection: Persistent implied connection from environment.
8230
"""
8331

84-
@_lazy_property_deco
85-
@staticmethod
86-
def project():
87-
"""Return the implicit default project."""
88-
return _determine_default_project()
89-
90-
def __init__(self, project=None, bucket=None, connection=None,
91-
implicit=False):
92-
if project is not None or not implicit:
93-
self.project = project
32+
def __init__(self, bucket=None, connection=None):
9433
self.bucket = bucket
9534
self.connection = connection
9635

9736

98-
def get_default_project():
99-
"""Get default project.
100-
101-
:rtype: string or ``NoneType``
102-
:returns: The default project if one has been set.
103-
"""
104-
return _DEFAULTS.project
105-
106-
10737
def get_default_bucket():
10838
"""Get default bucket.
10939
@@ -122,4 +52,4 @@ def get_default_connection():
12252
return _DEFAULTS.connection
12353

12454

125-
_DEFAULTS = _DefaultsContainer(implicit=True)
55+
_DEFAULTS = _DefaultsContainer()

gcloud/storage/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"""
2020

2121
from gcloud.exceptions import NotFound
22+
from gcloud._helpers import get_default_project
2223
from gcloud.storage._implicit_environ import get_default_connection
23-
from gcloud.storage._implicit_environ import get_default_project
2424
from gcloud.storage.bucket import Bucket
2525
from gcloud.storage.iterator import Iterator
2626

0 commit comments

Comments
 (0)