Skip to content

Commit 26dba83

Browse files
committed
Merge pull request #666 from dhermes/lazy-loading-attempt-3
Moving set_default_dataset_id into _implicit_environ.
2 parents 6ff634a + 7d75636 commit 26dba83

4 files changed

Lines changed: 371 additions & 347 deletions

File tree

gcloud/datastore/__init__.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@
4646
when race conditions may occur.
4747
"""
4848

49-
import os
50-
5149
from gcloud import credentials
5250
from gcloud.datastore import _implicit_environ
5351
from gcloud.datastore._implicit_environ import get_default_connection
5452
from gcloud.datastore._implicit_environ import get_default_dataset_id
53+
from gcloud.datastore._implicit_environ import set_default_dataset_id
5554
from gcloud.datastore.api import allocate_ids
5655
from gcloud.datastore.api import delete
5756
from gcloud.datastore.api import get
@@ -68,41 +67,6 @@
6867
'https://www.googleapis.com/auth/userinfo.email')
6968
"""The scopes required for authenticating as a Cloud Datastore consumer."""
7069

71-
_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
72-
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'
73-
74-
75-
def set_default_dataset_id(dataset_id=None):
76-
"""Set default dataset ID either explicitly or implicitly as fall-back.
77-
78-
In implicit case, supports three cases. In order of precedence, the
79-
implicit cases are:
80-
- GCLOUD_DATASET_ID environment variable
81-
- Google App Engine application ID
82-
- Google Compute Engine project ID (from metadata server)
83-
84-
:type dataset_id: string
85-
:param dataset_id: Optional. The dataset ID to use as default.
86-
87-
:raises: :class:`EnvironmentError` if no dataset ID was implied.
88-
"""
89-
if dataset_id is None:
90-
dataset_id = os.getenv(_DATASET_ENV_VAR_NAME)
91-
92-
if dataset_id is None:
93-
dataset_id = os.getenv(_GCD_DATASET_ENV_VAR_NAME)
94-
95-
if dataset_id is None:
96-
dataset_id = _implicit_environ.app_engine_id()
97-
98-
if dataset_id is None:
99-
dataset_id = _implicit_environ.compute_engine_id()
100-
101-
if dataset_id is not None:
102-
_implicit_environ._DEFAULTS.dataset_id = dataset_id
103-
else:
104-
raise EnvironmentError('No dataset ID could be inferred.')
105-
10670

10771
def set_default_connection(connection=None):
10872
"""Set default connection either explicitly or implicitly as fall-back.

gcloud/datastore/_implicit_environ.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
imply the current dataset ID and connection from the enviroment.
1919
"""
2020

21+
import os
2122
import socket
2223

2324
from six.moves.http_client import HTTPConnection # pylint: disable=F0401
@@ -28,6 +29,10 @@
2829
app_identity = None
2930

3031

32+
_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
33+
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'
34+
35+
3136
class _DefaultsContainer(object):
3237
"""Container for defaults.
3338
@@ -90,6 +95,61 @@ def compute_engine_id():
9095
connection.close()
9196

9297

98+
def _determine_default_dataset_id(dataset_id=None):
99+
"""Determine default dataset ID explicitly or implicitly as fall-back.
100+
101+
In implicit case, supports four environments. In order of precedence, the
102+
implicit environments are:
103+
104+
* GCLOUD_DATASET_ID environment variable
105+
* DATASTORE_DATASET environment variable (for ``gcd`` testing)
106+
* Google App Engine application ID
107+
* Google Compute Engine project ID (from metadata server)
108+
109+
:type dataset_id: string
110+
:param dataset_id: Optional. The dataset ID to use as default.
111+
112+
:rtype: string or ``NoneType``
113+
:returns: Default dataset ID if it can be determined.
114+
"""
115+
if dataset_id is None:
116+
dataset_id = os.getenv(_DATASET_ENV_VAR_NAME)
117+
118+
if dataset_id is None:
119+
dataset_id = os.getenv(_GCD_DATASET_ENV_VAR_NAME)
120+
121+
if dataset_id is None:
122+
dataset_id = app_engine_id()
123+
124+
if dataset_id is None:
125+
dataset_id = compute_engine_id()
126+
127+
return dataset_id
128+
129+
130+
def set_default_dataset_id(dataset_id=None):
131+
"""Set default dataset ID either explicitly or implicitly as fall-back.
132+
133+
In implicit case, supports four environments. In order of precedence, the
134+
implicit environments are:
135+
136+
* GCLOUD_DATASET_ID environment variable
137+
* DATASTORE_DATASET environment variable (for ``gcd`` testing)
138+
* Google App Engine application ID
139+
* Google Compute Engine project ID (from metadata server)
140+
141+
:type dataset_id: string
142+
:param dataset_id: Optional. The dataset ID to use as default.
143+
144+
:raises: :class:`EnvironmentError` if no dataset ID was implied.
145+
"""
146+
dataset_id = _determine_default_dataset_id(dataset_id=dataset_id)
147+
if dataset_id is not None:
148+
_DEFAULTS.dataset_id = dataset_id
149+
else:
150+
raise EnvironmentError('No dataset ID could be inferred.')
151+
152+
93153
def get_default_connection():
94154
"""Get default connection.
95155

0 commit comments

Comments
 (0)