33
33
_GCD_DATASET_ENV_VAR_NAME = 'DATASTORE_DATASET'
34
34
35
35
36
- class _DefaultsContainer (object ):
37
- """Container for defaults.
38
-
39
- :type connection: :class:`gcloud.datastore.connection.Connection`
40
- :param connection: Persistent implied connection from environment.
41
-
42
- :type dataset_id: string
43
- :param dataset_id: Persistent implied dataset ID from environment.
44
- """
45
-
46
- def __init__ (self , connection = None , dataset_id = None , implicit = False ):
47
- self .implicit = implicit
48
- self .connection = connection
49
- self .dataset_id = dataset_id
50
-
51
-
52
36
def app_engine_id ():
53
37
"""Gets the App Engine application ID if it can be inferred.
54
38
@@ -150,6 +134,15 @@ def set_default_dataset_id(dataset_id=None):
150
134
raise EnvironmentError ('No dataset ID could be inferred.' )
151
135
152
136
137
+ def get_default_dataset_id ():
138
+ """Get default dataset ID.
139
+
140
+ :rtype: string or ``NoneType``
141
+ :returns: The default dataset ID if one has been set.
142
+ """
143
+ return _DEFAULTS .dataset_id
144
+
145
+
153
146
def get_default_connection ():
154
147
"""Get default connection.
155
148
@@ -159,13 +152,69 @@ def get_default_connection():
159
152
return _DEFAULTS .connection
160
153
161
154
162
- def get_default_dataset_id ( ):
163
- """Get default dataset ID .
155
+ class _LazyProperty ( object ):
156
+ """Descriptor for lazy loaded property .
164
157
165
- :rtype: string or ``NoneType``
166
- :returns: The default dataset ID if one has been set.
158
+ This follows the reify pattern: lazy evaluation and then replacement
159
+ after evaluation.
160
+
161
+ :type name: string
162
+ :param name: The name of the attribute / property being evaluated.
163
+
164
+ :type deferred_callable: callable that takes no arguments
165
+ :param deferred_callable: The function / method used to evaluate the
166
+ property.
167
167
"""
168
- return _DEFAULTS .dataset_id
168
+
169
+ def __init__ (self , name , deferred_callable ):
170
+ self ._name = name
171
+ self ._deferred_callable = deferred_callable
172
+
173
+ def __get__ (self , obj , objtype ):
174
+ if obj is None or objtype is not _DefaultsContainer :
175
+ return self
176
+
177
+ setattr (obj , self ._name , self ._deferred_callable ())
178
+ return getattr (obj , self ._name )
179
+
180
+
181
+ def _lazy_property_deco (deferred_callable ):
182
+ """Decorator a method to create a :class:`_LazyProperty`.
183
+
184
+ :type deferred_callable: callable that takes no arguments
185
+ :param deferred_callable: The function / method used to evaluate the
186
+ property.
187
+
188
+ :rtype: :class:`_LazyProperty`.
189
+ :returns: A lazy property which defers the deferred_callable.
190
+ """
191
+ if isinstance (deferred_callable , staticmethod ):
192
+ # H/T: http://stackoverflow.com/a/9527450/1068170
193
+ # For Python2.7+ deferred_callable.__func__ would suffice.
194
+ deferred_callable = deferred_callable .__get__ (True )
195
+ return _LazyProperty (deferred_callable .__name__ , deferred_callable )
196
+
197
+
198
+ class _DefaultsContainer (object ):
199
+ """Container for defaults.
200
+
201
+ :type connection: :class:`gcloud.datastore.connection.Connection`
202
+ :param connection: Persistent implied connection from environment.
203
+
204
+ :type dataset_id: string
205
+ :param dataset_id: Persistent implied dataset ID from environment.
206
+ """
207
+
208
+ @_lazy_property_deco
209
+ @staticmethod
210
+ def dataset_id ():
211
+ """Return the implicit default dataset ID."""
212
+ return _determine_default_dataset_id ()
213
+
214
+ def __init__ (self , connection = None , dataset_id = None , implicit = False ):
215
+ self .connection = connection
216
+ if dataset_id is not None or not implicit :
217
+ self .dataset_id = dataset_id
169
218
170
219
171
220
_DEFAULTS = _DefaultsContainer (implicit = True )
0 commit comments