Skip to content

Commit

Permalink
Merge pull request #979 from tseaver/944-remove__implicit_environ
Browse files Browse the repository at this point in the history
Remove '_implicit_environ'
  • Loading branch information
tseaver committed Jul 13, 2015
2 parents 583d1ce + 8423abd commit 8373227
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 741 deletions.
32 changes: 3 additions & 29 deletions gcloud/datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
>>> from gcloud import datastore
>>> key = datastore.Key('EntityKind', 1234)
>>> client = datastore.Client()
>>> key = client.key('EntityKind', 1234)
>>> entity = datastore.Entity(key)
>>> query = datastore.Query(kind='EntityKind')
>>> query = client.query(kind='EntityKind')
The main concepts with this API are:
Expand Down Expand Up @@ -49,11 +50,6 @@
when race conditions may occur.
"""

from gcloud.datastore._implicit_environ import get_connection
from gcloud.datastore._implicit_environ import get_default_connection
from gcloud.datastore._implicit_environ import get_default_dataset_id
from gcloud.datastore._implicit_environ import set_default_connection
from gcloud.datastore._implicit_environ import set_default_dataset_id
from gcloud.datastore.batch import Batch
from gcloud.datastore.connection import SCOPE
from gcloud.datastore.connection import Connection
Expand All @@ -62,25 +58,3 @@
from gcloud.datastore.key import Key
from gcloud.datastore.query import Query
from gcloud.datastore.transaction import Transaction


def set_defaults(dataset_id=None, connection=None):
"""Set defaults either explicitly or implicitly as fall-back.
Uses the arguments to call the individual default methods
- set_default_dataset_id
- set_default_connection
In the future we will likely enable methods like
- set_default_namespace
:type dataset_id: string
:param dataset_id: Optional. The dataset ID to use as default.
:type connection: :class:`gcloud.datastore.connection.Connection`
:param connection: A connection provided to be the default.
"""
set_default_dataset_id(dataset_id=dataset_id)
set_default_connection(connection=connection)
179 changes: 0 additions & 179 deletions gcloud/datastore/_implicit_environ.py

This file was deleted.

33 changes: 0 additions & 33 deletions gcloud/datastore/_testing.py

This file was deleted.

57 changes: 54 additions & 3 deletions gcloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,71 @@
# limitations under the License.
"""Convenience wrapper for invoking APIs/factories w/ a dataset ID."""

import os

from gcloud._helpers import _LocalStack
from gcloud._helpers import _app_engine_id
from gcloud._helpers import _compute_engine_id
from gcloud.datastore import helpers
from gcloud.datastore.connection import Connection
from gcloud.datastore.batch import Batch
from gcloud.datastore.entity import Entity
from gcloud.datastore.key import Key
from gcloud.datastore.query import Query
from gcloud.datastore.transaction import Transaction
from gcloud.datastore._implicit_environ import _determine_default_dataset_id
from gcloud.datastore._implicit_environ import get_connection


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

_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
"""Environment variable defining default dataset ID."""

_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'
"""Environment variable defining default dataset ID under GCD."""


def _get_production_dataset_id():
"""Gets the production application ID if it can be inferred."""
return os.getenv(_DATASET_ENV_VAR_NAME)


def _get_gcd_dataset_id():
"""Gets the GCD application ID if it can be inferred."""
return os.getenv(_GCD_DATASET_ENV_VAR_NAME)


def _determine_default_dataset_id(dataset_id=None):
"""Determine default dataset ID explicitly or implicitly as fall-back.
In implicit case, supports four environments. In order of precedence, the
implicit environments are:
* GCLOUD_DATASET_ID environment variable
* DATASTORE_DATASET environment variable (for ``gcd`` testing)
* Google App Engine application ID
* Google Compute Engine project ID (from metadata server)
:type dataset_id: string
:param dataset_id: Optional. The dataset ID to use as default.
:rtype: string or ``NoneType``
:returns: Default dataset ID if it can be determined.
"""
if dataset_id is None:
dataset_id = _get_production_dataset_id()

if dataset_id is None:
dataset_id = _get_gcd_dataset_id()

if dataset_id is None:
dataset_id = _app_engine_id()

if dataset_id is None:
dataset_id = _compute_engine_id()

return dataset_id


def _extended_lookup(connection, dataset_id, key_pbs,
missing=None, deferred=None,
Expand Down Expand Up @@ -126,7 +177,7 @@ def __init__(self, dataset_id=None, namespace=None, connection=None):
raise EnvironmentError('Dataset ID could not be inferred.')
self.dataset_id = dataset_id
if connection is None:
connection = get_connection()
connection = Connection.from_environment()
self.connection = connection
self._batch_stack = _LocalStack()
self.namespace = namespace
Expand Down
9 changes: 2 additions & 7 deletions gcloud/datastore/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import copy
import six

from gcloud.datastore import _implicit_environ
from gcloud.datastore import _datastore_v1_pb2 as datastore_pb


Expand Down Expand Up @@ -395,15 +394,11 @@ def _validate_dataset_id(dataset_id, parent):
:rtype: string
:returns: The ``dataset_id`` passed in, or implied from the environment.
:raises: :class:`ValueError` if ``dataset_id`` is ``None`` and no dataset
can be inferred.
can be inferred from the parent.
"""
if parent is None:

if dataset_id is None:

dataset_id = _implicit_environ.get_default_dataset_id()
if dataset_id is None:
raise ValueError("A Key must have a dataset ID set.")
raise ValueError("A Key must have a dataset ID set.")

return dataset_id

Expand Down
4 changes: 2 additions & 2 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def dataset_id(self):
:rtype: str
"""
return self._dataset_id
return self._dataset_id or self._client.dataset_id

@property
def namespace(self):
Expand All @@ -111,7 +111,7 @@ def namespace(self):
:rtype: string or None
:returns: the namespace assigned to this query
"""
return self._namespace
return self._namespace or self._client.namespace

@namespace.setter
def namespace(self, value):
Expand Down
Loading

0 comments on commit 8373227

Please sign in to comment.