Skip to content

Commit

Permalink
Merge pull request #663 from dhermes/add-get-default-methods
Browse files Browse the repository at this point in the history
Using getters in _implicit_environ instead of direct access.
  • Loading branch information
dhermes committed Feb 18, 2015
2 parents f56abdf + 80fa002 commit 72b6359
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 11 deletions.
2 changes: 2 additions & 0 deletions gcloud/datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

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.api import allocate_ids
from gcloud.datastore.api import delete
from gcloud.datastore.api import get
Expand Down
18 changes: 18 additions & 0 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,21 @@ def compute_engine_id():
pass
finally:
connection.close()


def get_default_connection():
"""Get default connection.
:rtype: :class:`gcloud.datastore.connection.Connection` or ``NoneType``
:returns: The default connection if one has been set.
"""
return CONNECTION


def get_default_dataset_id():
"""Get default dataset ID.
:rtype: string or ``NoneType``
:returns: The default dataset ID if one has been set.
"""
return DATASET_ID
10 changes: 6 additions & 4 deletions gcloud/datastore/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ def _require_dataset_id(dataset_id=None, first_key=None):
return top.dataset_id
if first_key is not None:
return first_key.dataset_id
if _implicit_environ.DATASET_ID is None:

dataset_id = _implicit_environ.get_default_dataset_id()
if dataset_id is None:
raise EnvironmentError('Dataset ID could not be inferred.')
return _implicit_environ.DATASET_ID
return dataset_id


def _require_connection(connection=None):
Expand All @@ -78,9 +80,9 @@ def _require_connection(connection=None):
if top is not None:
connection = top.connection
else:
if _implicit_environ.CONNECTION is None:
connection = _implicit_environ.get_default_connection()
if connection is None:
raise EnvironmentError('Connection could not be inferred.')
connection = _implicit_environ.CONNECTION
return connection


Expand Down
6 changes: 4 additions & 2 deletions gcloud/datastore/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ def __init__(self, dataset_id=None, connection=None):
:raises: :class:`ValueError` if either a connection or dataset ID
are not set.
"""
self._connection = connection or _implicit_environ.CONNECTION
self._dataset_id = dataset_id or _implicit_environ.DATASET_ID
self._connection = (connection or
_implicit_environ.get_default_connection())
self._dataset_id = (dataset_id or
_implicit_environ.get_default_dataset_id())

if self._connection is None or self._dataset_id is None:
raise ValueError('A batch must have a connection and '
Expand Down
5 changes: 2 additions & 3 deletions gcloud/datastore/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,10 @@ def _validate_dataset_id(dataset_id, parent):

if dataset_id is None:

if _implicit_environ.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.")

dataset_id = _implicit_environ.DATASET_ID

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 @@ -80,7 +80,7 @@ def __init__(self,
group_by=()):

if dataset_id is None:
dataset_id = _implicit_environ.DATASET_ID
dataset_id = _implicit_environ.get_default_dataset_id()

if dataset_id is None:
raise ValueError("No dataset ID supplied, and no default set.")
Expand Down Expand Up @@ -326,7 +326,7 @@ def fetch(self, limit=None, offset=0, start_cursor=None, end_cursor=None,
default has been set.
"""
if connection is None:
connection = _implicit_environ.CONNECTION
connection = _implicit_environ.get_default_connection()

if connection is None:
raise ValueError("No connection passed, and no default set")
Expand Down
51 changes: 51 additions & 0 deletions gcloud/datastore/test__implicit_environ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest2


class Test_get_default_connection(unittest2.TestCase):

def _callFUT(self):
from gcloud.datastore._implicit_environ import get_default_connection
return get_default_connection()

def test_default(self):
self.assertEqual(self._callFUT(), None)

def test_preset(self):
from gcloud._testing import _Monkey
from gcloud.datastore import _implicit_environ

SENTINEL = object()
with _Monkey(_implicit_environ, CONNECTION=SENTINEL):
self.assertEqual(self._callFUT(), SENTINEL)


class Test_get_default_dataset_id(unittest2.TestCase):

def _callFUT(self):
from gcloud.datastore._implicit_environ import get_default_dataset_id
return get_default_dataset_id()

def test_default(self):
self.assertEqual(self._callFUT(), None)

def test_preset(self):
from gcloud._testing import _Monkey
from gcloud.datastore import _implicit_environ

SENTINEL = object()
with _Monkey(_implicit_environ, DATASET_ID=SENTINEL):
self.assertEqual(self._callFUT(), SENTINEL)

0 comments on commit 72b6359

Please sign in to comment.