Skip to content

Commit

Permalink
Merge pull request #666 from dhermes/lazy-loading-attempt-3
Browse files Browse the repository at this point in the history
Moving set_default_dataset_id into _implicit_environ.
  • Loading branch information
dhermes committed Feb 19, 2015
2 parents 6ff634a + 7d75636 commit 26dba83
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 347 deletions.
38 changes: 1 addition & 37 deletions gcloud/datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@
when race conditions may occur.
"""

import os

from gcloud import credentials
from gcloud.datastore import _implicit_environ
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_dataset_id
from gcloud.datastore.api import allocate_ids
from gcloud.datastore.api import delete
from gcloud.datastore.api import get
Expand All @@ -68,41 +67,6 @@
'https://www.googleapis.com/auth/userinfo.email')
"""The scopes required for authenticating as a Cloud Datastore consumer."""

_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'


def set_default_dataset_id(dataset_id=None):
"""Set default dataset ID either explicitly or implicitly as fall-back.
In implicit case, supports three cases. In order of precedence, the
implicit cases are:
- GCLOUD_DATASET_ID environment variable
- 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.
:raises: :class:`EnvironmentError` if no dataset ID was implied.
"""
if dataset_id is None:
dataset_id = os.getenv(_DATASET_ENV_VAR_NAME)

if dataset_id is None:
dataset_id = os.getenv(_GCD_DATASET_ENV_VAR_NAME)

if dataset_id is None:
dataset_id = _implicit_environ.app_engine_id()

if dataset_id is None:
dataset_id = _implicit_environ.compute_engine_id()

if dataset_id is not None:
_implicit_environ._DEFAULTS.dataset_id = dataset_id
else:
raise EnvironmentError('No dataset ID could be inferred.')


def set_default_connection(connection=None):
"""Set default connection either explicitly or implicitly as fall-back.
Expand Down
60 changes: 60 additions & 0 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
imply the current dataset ID and connection from the enviroment.
"""

import os
import socket

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


_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'


class _DefaultsContainer(object):
"""Container for defaults.
Expand Down Expand Up @@ -90,6 +95,61 @@ def compute_engine_id():
connection.close()


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 = os.getenv(_DATASET_ENV_VAR_NAME)

if dataset_id is None:
dataset_id = os.getenv(_GCD_DATASET_ENV_VAR_NAME)

if dataset_id is None:
dataset_id = app_engine_id()

if dataset_id is None:
dataset_id = compute_engine_id()

return dataset_id


def set_default_dataset_id(dataset_id=None):
"""Set default dataset ID either 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.
:raises: :class:`EnvironmentError` if no dataset ID was implied.
"""
dataset_id = _determine_default_dataset_id(dataset_id=dataset_id)
if dataset_id is not None:
_DEFAULTS.dataset_id = dataset_id
else:
raise EnvironmentError('No dataset ID could be inferred.')


def get_default_connection():
"""Get default connection.
Expand Down
Loading

0 comments on commit 26dba83

Please sign in to comment.