Skip to content

Commit

Permalink
Adding lazy loading support for dataset ID.
Browse files Browse the repository at this point in the history
Still need to support connection (and eventually do the same
in storage).

Again verified only the lazy loading tests used implicit
behavior via

```diff
diff --git a/gcloud/datastore/_implicit_environ.py b/gcloud/datastore/_implicit_environ.py
index 6e61636..6d67be6 100644
--- a/gcloud/datastore/_implicit_environ.py
+++ b/gcloud/datastore/_implicit_environ.py
@@ -177,6 +177,10 @@ class _LazyProperty(object):
         self._method = method

     def __get__(self, obj, objtype):
+        class FooError(Exception):
+            pass
+        raise FooError
+
         if obj is None or objtype is not _DefaultsContainer:
             return self
```
  • Loading branch information
dhermes committed Feb 19, 2015
1 parent 15db084 commit 4ce0753
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 23 deletions.
78 changes: 57 additions & 21 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'


class _DefaultsContainer(object):
"""Container for defaults.
:type connection: :class:`gcloud.datastore.connection.Connection`
:param connection: Persistent implied connection from environment.
:type dataset_id: string
:param dataset_id: Persistent implied dataset ID from environment.
"""

def __init__(self, connection=None, dataset_id=None, implicit=False):
self.implicit = implicit
self.connection = connection
self.dataset_id = dataset_id


def app_engine_id():
"""Gets the App Engine application ID if it can be inferred.
Expand Down Expand Up @@ -127,6 +111,15 @@ def _determine_default_dataset_id(dataset_id=None):
return dataset_id


def _lazy_dataset_id():
"""Alias wrapper for _determine_default_dataset_id.
Unit test need to be able to replace _determine_default_dataset_id()
so we can't wrap the actual ``function`` object in a ``_LazyProperty``.
"""
return _determine_default_dataset_id()


def set_default_dataset_id(dataset_id=None):
"""Set default dataset ID either explicitly or implicitly as fall-back.
Expand All @@ -150,6 +143,15 @@ def set_default_dataset_id(dataset_id=None):
raise EnvironmentError('No dataset ID could be inferred.')


def get_default_dataset_id():
"""Get default dataset ID.
:rtype: string or ``NoneType``
:returns: The default dataset ID if one has been set.
"""
return _DEFAULTS.dataset_id


def get_default_connection():
"""Get default connection.
Expand All @@ -159,13 +161,47 @@ def get_default_connection():
return _DEFAULTS.connection


def get_default_dataset_id():
"""Get default dataset ID.
class _LazyProperty(object):
"""Descriptor for lazy loaded property.
:rtype: string or ``NoneType``
:returns: The default dataset ID if one has been set.
This follows the reify pattern: lazy evaluation and then replacement
after evaluation.
:type name: string
:param name: The name of the attribute / property being evaluated.
:type method: callable that takes no arguments
:param method: The method used to evaluate the property.
"""
return _DEFAULTS.dataset_id

def __init__(self, name, method):
self._name = name
self._method = method

def __get__(self, obj, objtype):
if obj is None or objtype is not _DefaultsContainer:
return self

setattr(obj, self._name, self._method())
return getattr(obj, self._name)


class _DefaultsContainer(object):
"""Container for defaults.
:type connection: :class:`gcloud.datastore.connection.Connection`
:param connection: Persistent implied connection from environment.
:type dataset_id: string
:param dataset_id: Persistent implied dataset ID from environment.
"""

dataset_id = _LazyProperty('dataset_id', _lazy_dataset_id)

def __init__(self, connection=None, dataset_id=None, implicit=False):
self.connection = connection
if dataset_id is not None or not implicit:
self.dataset_id = dataset_id


_DEFAULTS = _DefaultsContainer(implicit=True)
4 changes: 2 additions & 2 deletions gcloud/datastore/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def _monkey_defaults(*args, **kwargs):
return _Monkey(_implicit_environ, _DEFAULTS=mock_defaults)


def _setup_defaults(test_case):
def _setup_defaults(test_case, *args, **kwargs):
test_case._replaced_defaults = _implicit_environ._DEFAULTS
_implicit_environ._DEFAULTS = _DefaultsContainer()
_implicit_environ._DEFAULTS = _DefaultsContainer(*args, **kwargs)


def _tear_down_defaults(test_case):
Expand Down
53 changes: 53 additions & 0 deletions gcloud/datastore/test__implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,59 @@ def test_set_implicit_three_env_appengine_and_compute(self):
self.assertEqual(connection.timeout, None)


class Test_lazy_loaded_dataset_id(unittest2.TestCase):

def setUp(self):
from gcloud.datastore._testing import _setup_defaults
_setup_defaults(self, implicit=True)

def tearDown(self):
from gcloud.datastore._testing import _tear_down_defaults
_tear_down_defaults(self)

def test_prop_default(self):
from gcloud.datastore import _implicit_environ
from gcloud.datastore._implicit_environ import _DefaultsContainer
from gcloud.datastore._implicit_environ import _LazyProperty

self.assertTrue(isinstance(_DefaultsContainer.dataset_id,
_LazyProperty))
self.assertEqual(_implicit_environ._DEFAULTS.dataset_id, None)

def test_prop_on_wrong_class(self):
from gcloud.datastore._implicit_environ import _LazyProperty

# Don't actually need a callable for ``method`` since
# __get__ will just return ``self`` in this test.
data_prop = _LazyProperty('dataset_id', None)

class FakeEnv(object):
dataset_id = data_prop

self.assertTrue(FakeEnv.dataset_id is data_prop)
self.assertTrue(FakeEnv().dataset_id is data_prop)

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

self.assertFalse(
'dataset_id' in _implicit_environ._DEFAULTS.__dict__)

DEFAULT = object()

def mock_default():
return DEFAULT

with _Monkey(_implicit_environ,
_determine_default_dataset_id=mock_default):
lazy_loaded = _implicit_environ._DEFAULTS.dataset_id

self.assertEqual(lazy_loaded, DEFAULT)
self.assertTrue(
'dataset_id' in _implicit_environ._DEFAULTS.__dict__)


class _AppIdentity(object):

def __init__(self, app_id):
Expand Down

0 comments on commit 4ce0753

Please sign in to comment.