Skip to content

Commit 8373227

Browse files
committed
Merge pull request #979 from tseaver/944-remove__implicit_environ
Remove '_implicit_environ'
2 parents 583d1ce + 8423abd commit 8373227

16 files changed

+233
-741
lines changed

gcloud/datastore/__init__.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
1919
>>> from gcloud import datastore
2020
21-
>>> key = datastore.Key('EntityKind', 1234)
21+
>>> client = datastore.Client()
22+
>>> key = client.key('EntityKind', 1234)
2223
>>> entity = datastore.Entity(key)
23-
>>> query = datastore.Query(kind='EntityKind')
24+
>>> query = client.query(kind='EntityKind')
2425
2526
The main concepts with this API are:
2627
@@ -49,11 +50,6 @@
4950
when race conditions may occur.
5051
"""
5152

52-
from gcloud.datastore._implicit_environ import get_connection
53-
from gcloud.datastore._implicit_environ import get_default_connection
54-
from gcloud.datastore._implicit_environ import get_default_dataset_id
55-
from gcloud.datastore._implicit_environ import set_default_connection
56-
from gcloud.datastore._implicit_environ import set_default_dataset_id
5753
from gcloud.datastore.batch import Batch
5854
from gcloud.datastore.connection import SCOPE
5955
from gcloud.datastore.connection import Connection
@@ -62,25 +58,3 @@
6258
from gcloud.datastore.key import Key
6359
from gcloud.datastore.query import Query
6460
from gcloud.datastore.transaction import Transaction
65-
66-
67-
def set_defaults(dataset_id=None, connection=None):
68-
"""Set defaults either explicitly or implicitly as fall-back.
69-
70-
Uses the arguments to call the individual default methods
71-
72-
- set_default_dataset_id
73-
- set_default_connection
74-
75-
In the future we will likely enable methods like
76-
77-
- set_default_namespace
78-
79-
:type dataset_id: string
80-
:param dataset_id: Optional. The dataset ID to use as default.
81-
82-
:type connection: :class:`gcloud.datastore.connection.Connection`
83-
:param connection: A connection provided to be the default.
84-
"""
85-
set_default_dataset_id(dataset_id=dataset_id)
86-
set_default_connection(connection=connection)

gcloud/datastore/_implicit_environ.py

Lines changed: 0 additions & 179 deletions
This file was deleted.

gcloud/datastore/_testing.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

gcloud/datastore/client.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,71 @@
1313
# limitations under the License.
1414
"""Convenience wrapper for invoking APIs/factories w/ a dataset ID."""
1515

16+
import os
17+
1618
from gcloud._helpers import _LocalStack
19+
from gcloud._helpers import _app_engine_id
20+
from gcloud._helpers import _compute_engine_id
1721
from gcloud.datastore import helpers
22+
from gcloud.datastore.connection import Connection
1823
from gcloud.datastore.batch import Batch
1924
from gcloud.datastore.entity import Entity
2025
from gcloud.datastore.key import Key
2126
from gcloud.datastore.query import Query
2227
from gcloud.datastore.transaction import Transaction
23-
from gcloud.datastore._implicit_environ import _determine_default_dataset_id
24-
from gcloud.datastore._implicit_environ import get_connection
2528

2629

2730
_MAX_LOOPS = 128
2831
"""Maximum number of iterations to wait for deferred keys."""
2932

33+
_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
34+
"""Environment variable defining default dataset ID."""
35+
36+
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'
37+
"""Environment variable defining default dataset ID under GCD."""
38+
39+
40+
def _get_production_dataset_id():
41+
"""Gets the production application ID if it can be inferred."""
42+
return os.getenv(_DATASET_ENV_VAR_NAME)
43+
44+
45+
def _get_gcd_dataset_id():
46+
"""Gets the GCD application ID if it can be inferred."""
47+
return os.getenv(_GCD_DATASET_ENV_VAR_NAME)
48+
49+
50+
def _determine_default_dataset_id(dataset_id=None):
51+
"""Determine default dataset ID explicitly or implicitly as fall-back.
52+
53+
In implicit case, supports four environments. In order of precedence, the
54+
implicit environments are:
55+
56+
* GCLOUD_DATASET_ID environment variable
57+
* DATASTORE_DATASET environment variable (for ``gcd`` testing)
58+
* Google App Engine application ID
59+
* Google Compute Engine project ID (from metadata server)
60+
61+
:type dataset_id: string
62+
:param dataset_id: Optional. The dataset ID to use as default.
63+
64+
:rtype: string or ``NoneType``
65+
:returns: Default dataset ID if it can be determined.
66+
"""
67+
if dataset_id is None:
68+
dataset_id = _get_production_dataset_id()
69+
70+
if dataset_id is None:
71+
dataset_id = _get_gcd_dataset_id()
72+
73+
if dataset_id is None:
74+
dataset_id = _app_engine_id()
75+
76+
if dataset_id is None:
77+
dataset_id = _compute_engine_id()
78+
79+
return dataset_id
80+
3081

3182
def _extended_lookup(connection, dataset_id, key_pbs,
3283
missing=None, deferred=None,
@@ -126,7 +177,7 @@ def __init__(self, dataset_id=None, namespace=None, connection=None):
126177
raise EnvironmentError('Dataset ID could not be inferred.')
127178
self.dataset_id = dataset_id
128179
if connection is None:
129-
connection = get_connection()
180+
connection = Connection.from_environment()
130181
self.connection = connection
131182
self._batch_stack = _LocalStack()
132183
self.namespace = namespace

gcloud/datastore/key.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import copy
1818
import six
1919

20-
from gcloud.datastore import _implicit_environ
2120
from gcloud.datastore import _datastore_v1_pb2 as datastore_pb
2221

2322

@@ -395,15 +394,11 @@ def _validate_dataset_id(dataset_id, parent):
395394
:rtype: string
396395
:returns: The ``dataset_id`` passed in, or implied from the environment.
397396
:raises: :class:`ValueError` if ``dataset_id`` is ``None`` and no dataset
398-
can be inferred.
397+
can be inferred from the parent.
399398
"""
400399
if parent is None:
401-
402400
if dataset_id is None:
403-
404-
dataset_id = _implicit_environ.get_default_dataset_id()
405-
if dataset_id is None:
406-
raise ValueError("A Key must have a dataset ID set.")
401+
raise ValueError("A Key must have a dataset ID set.")
407402

408403
return dataset_id
409404

gcloud/datastore/query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def dataset_id(self):
102102
103103
:rtype: str
104104
"""
105-
return self._dataset_id
105+
return self._dataset_id or self._client.dataset_id
106106

107107
@property
108108
def namespace(self):
@@ -111,7 +111,7 @@ def namespace(self):
111111
:rtype: string or None
112112
:returns: the namespace assigned to this query
113113
"""
114-
return self._namespace
114+
return self._namespace or self._client.namespace
115115

116116
@namespace.setter
117117
def namespace(self, value):

0 commit comments

Comments
 (0)