Skip to content

Commit

Permalink
Moving set_default_dataset_id to _implicit_environ module.
Browse files Browse the repository at this point in the history
Also splitting out behavior into a get_default_dataset_id method
and then the set_default_dataset_id around in.
  • Loading branch information
dhermes committed Feb 17, 2015
1 parent f067b1b commit 83e3d9f
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 294 deletions.
34 changes: 1 addition & 33 deletions gcloud/datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@
when race conditions may occur.
"""

import os

from gcloud import credentials
from gcloud.datastore import _implicit_environ
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 @@ -66,37 +65,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'


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 = _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.DEFAULT_ENVIRON.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
65 changes: 59 additions & 6 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module to provide implicit behavior based on enviroment.
"""Module to provide implicit behavior based on environment.
Acts as a mutable namespace to allow the datastore package to
imply the current dataset ID and connection from the enviroment.
imply the current dataset ID and connection from the environment.
"""

import os
import socket

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


class Enviroment(object):
_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'


class Environment(object):
"""Container for environment settings."""

dataset_id = None
"""Attribute to allow persistent implied dataset ID from enviroment."""
"""Attribute to allow persistent implied dataset ID from environment."""

connection = None
"""Attribute to allow persistent implied connection from enviroment."""
"""Attribute to allow persistent implied connection from environment."""


def app_engine_id():
Expand Down Expand Up @@ -84,4 +88,53 @@ def compute_engine_id():
connection.close()


DEFAULT_ENVIRON = Enviroment()
def get_default_dataset_id(dataset_id=None):
"""Get 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.
:rtype: string or ``NoneType``
:returns: The inferred dataset or None.
"""
if dataset_id is None:
dataset_id = os.getenv(_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 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.
"""
dataset_id = get_default_dataset_id(dataset_id=dataset_id)

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


DEFAULT_ENVIRON = Environment()
Loading

0 comments on commit 83e3d9f

Please sign in to comment.