From 4a4a0a76d75bd7d424561c22cdcd92c14a0c2efe Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 18 Feb 2015 10:41:03 -0800 Subject: [PATCH 1/2] Using getters in _implicit_environ instead of direct access. This is in preparation to add lazy loading. --- gcloud/datastore/_implicit_environ.py | 18 ++++++++ gcloud/datastore/api.py | 10 +++-- gcloud/datastore/batch.py | 6 ++- gcloud/datastore/key.py | 5 +-- gcloud/datastore/query.py | 4 +- gcloud/datastore/test__implicit_environ.py | 51 ++++++++++++++++++++++ 6 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 gcloud/datastore/test__implicit_environ.py diff --git a/gcloud/datastore/_implicit_environ.py b/gcloud/datastore/_implicit_environ.py index 504cd4c8942d..2af5b6490295 100644 --- a/gcloud/datastore/_implicit_environ.py +++ b/gcloud/datastore/_implicit_environ.py @@ -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 diff --git a/gcloud/datastore/api.py b/gcloud/datastore/api.py index 7e0cb3a402e0..d063e01a470c 100644 --- a/gcloud/datastore/api.py +++ b/gcloud/datastore/api.py @@ -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): @@ -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 diff --git a/gcloud/datastore/batch.py b/gcloud/datastore/batch.py index 2b61a96ab820..bf2f04f63e24 100644 --- a/gcloud/datastore/batch.py +++ b/gcloud/datastore/batch.py @@ -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 ' diff --git a/gcloud/datastore/key.py b/gcloud/datastore/key.py index 907602a8034d..f075ce0187f7 100644 --- a/gcloud/datastore/key.py +++ b/gcloud/datastore/key.py @@ -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 diff --git a/gcloud/datastore/query.py b/gcloud/datastore/query.py index 97f86efb1f23..6e330e642dee 100644 --- a/gcloud/datastore/query.py +++ b/gcloud/datastore/query.py @@ -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.") @@ -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") diff --git a/gcloud/datastore/test__implicit_environ.py b/gcloud/datastore/test__implicit_environ.py new file mode 100644 index 000000000000..7352cb13b2a5 --- /dev/null +++ b/gcloud/datastore/test__implicit_environ.py @@ -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) From 80fa002e99c9dac57256c87686080ce79bb35dad Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 18 Feb 2015 10:43:01 -0800 Subject: [PATCH 2/2] Putting get_default_(connection|dataset_id) in datastore pkg namespace. --- gcloud/datastore/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gcloud/datastore/__init__.py b/gcloud/datastore/__init__.py index eb03a318cd82..b0fc7e854b66 100644 --- a/gcloud/datastore/__init__.py +++ b/gcloud/datastore/__init__.py @@ -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