Skip to content

Commit 0eca209

Browse files
committed
Merge pull request #501 from tseaver/cherrypick-499
Cherry pick cleanups from #499.
2 parents 7019708 + 90829f5 commit 0eca209

File tree

6 files changed

+269
-176
lines changed

6 files changed

+269
-176
lines changed

gcloud/datastore/__init__.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,40 @@ def get_connection():
104104
return Connection(credentials=scoped_credentials)
105105

106106

107-
def _require_dataset_id():
108-
"""Convenience method to ensure DATASET_ID is set.
107+
def _require_dataset_id(dataset_id=None):
108+
"""Infer a dataset ID from the environment, if not passed explicitly.
109109
110-
:rtype: :class:`str`
111-
:returns: A dataset ID based on the current environment.
112-
:raises: :class:`EnvironmentError` if DATASET_ID is not set.
110+
:type dataset_id: :class:`str`.
111+
:param dataset_id: Optional.
112+
113+
:rtype: :class:`gcloud.datastore.dataset.Dataset`
114+
:returns: A dataset based on the current environment.
115+
:raises: :class:`EnvironmentError` if ``dataset_id`` is None,
116+
and cannot be inferred from the environment.
113117
"""
114-
if _implicit_environ.DATASET_ID is None:
115-
raise EnvironmentError('Dataset ID could not be inferred.')
116-
return _implicit_environ.DATASET_ID
118+
if dataset_id is None:
119+
if _implicit_environ.DATASET_ID is None:
120+
raise EnvironmentError('Dataset ID could not be inferred.')
121+
dataset_id = _implicit_environ.DATASET_ID
122+
return dataset_id
117123

118124

119-
def _require_connection():
120-
"""Convenience method to ensure CONNECTION is set.
125+
def _require_connection(connection=None):
126+
"""Infer a connection from the environment, if not passed explicitly.
127+
128+
:type connection: :class:`gcloud.datastore.connection.Connection`
129+
:param connection: Optional.
121130
122131
:rtype: :class:`gcloud.datastore.connection.Connection`
123132
:returns: A connection based on the current environment.
124-
:raises: :class:`EnvironmentError` if CONNECTION is not set.
133+
:raises: :class:`EnvironmentError` if ``connection`` is None, and
134+
cannot be inferred from the environment.
125135
"""
126-
if _implicit_environ.CONNECTION is None:
127-
raise EnvironmentError('Connection could not be inferred.')
128-
return _implicit_environ.CONNECTION
136+
if connection is None:
137+
if _implicit_environ.CONNECTION is None:
138+
raise EnvironmentError('Connection could not be inferred.')
139+
connection = _implicit_environ.CONNECTION
140+
return connection
129141

130142

131143
def get_entities(keys, missing=None, deferred=None,
@@ -154,8 +166,8 @@ def get_entities(keys, missing=None, deferred=None,
154166
:rtype: list of :class:`gcloud.datastore.entity.Entity`
155167
:returns: The requested entities.
156168
"""
157-
connection = connection or _require_connection()
158-
dataset_id = dataset_id or _require_dataset_id()
169+
connection = _require_connection(connection)
170+
dataset_id = _require_dataset_id(dataset_id)
159171

160172
entity_pbs = connection.lookup(
161173
dataset_id=dataset_id,
@@ -199,8 +211,8 @@ def allocate_ids(incomplete_key, num_ids, connection=None, dataset_id=None):
199211
:returns: The (complete) keys allocated with `incomplete_key` as root.
200212
:raises: `ValueError` if `incomplete_key` is not a partial key.
201213
"""
202-
connection = connection or _require_connection()
203-
dataset_id = dataset_id or _require_dataset_id()
214+
connection = _require_connection(connection)
215+
dataset_id = _require_dataset_id(dataset_id)
204216

205217
if not incomplete_key.is_partial:
206218
raise ValueError(('Key is not partial.', incomplete_key))

gcloud/datastore/connection.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,13 @@ def lookup(self, dataset_id, key_pbs,
163163
(:class:`gcloud.datastore.datastore_v1_pb2.Key` and
164164
:class:`gcloud.datastore.datastore_v1_pb2.Entity`) and is used
165165
under the hood for methods like
166-
:func:`gcloud.datastore.dataset.Dataset.get_entity`:
166+
:func:`gcloud.datastore.key.Key.get`:
167167
168168
>>> from gcloud import datastore
169169
>>> from gcloud.datastore.key import Key
170170
>>> connection = datastore.get_connection()
171-
>>> dataset = connection.dataset('dataset-id')
172-
>>> key = Key(dataset=dataset).kind('MyKind').id(1234)
173-
174-
Using the :class:`gcloud.datastore.dataset.Dataset` helper:
175-
176-
>>> dataset.get_entity(key)
171+
>>> key = Key('MyKind', 1234, dataset_id='dataset-id')
172+
>>> key.get()
177173
<Entity object>
178174
179175
Using the ``connection`` class directly:
@@ -182,7 +178,7 @@ def lookup(self, dataset_id, key_pbs,
182178
<Entity protobuf>
183179
184180
:type dataset_id: string
185-
:param dataset_id: The dataset to look up the keys.
181+
:param dataset_id: The ID of the dataset to look up the keys.
186182
187183
:type key_pbs: list of :class:`gcloud.datastore.datastore_v1_pb2.Key`
188184
(or a single Key)
@@ -262,12 +258,12 @@ def run_query(self, dataset_id, query_pb, namespace=None, eventual=False):
262258
uses this method to fetch data:
263259
264260
>>> from gcloud import datastore
261+
>>> from gcloud.datastore.query import Query
265262
>>> connection = datastore.get_connection()
266-
>>> dataset = connection.dataset('dataset-id')
267-
>>> query = dataset.query().kind('MyKind').filter(
268-
... 'property', '=', 'val')
263+
>>> query = Query(dataset_id='dataset-id', 'MyKind')
264+
>>> query.add_filter('property', '=', 'val')
269265
270-
Using the `fetch`` method...
266+
Using the query's ``fetch_page`` method...
271267
272268
>>> entities, cursor, more_results = query.fetch_page()
273269
>>> entities
@@ -319,7 +315,7 @@ def begin_transaction(self, dataset_id, serializable=False):
319315
Maps the ``DatastoreService.BeginTransaction`` protobuf RPC.
320316
321317
:type dataset_id: string
322-
:param dataset_id: The dataset over which to execute the transaction.
318+
:param dataset_id: The ID dataset to which the transaction applies.
323319
"""
324320

325321
if self.transaction():
@@ -346,7 +342,7 @@ def commit(self, dataset_id, mutation_pb):
346342
Maps the ``DatastoreService.Commit`` protobuf RPC.
347343
348344
:type dataset_id: string
349-
:param dataset_id: The dataset in which to perform the changes.
345+
:param dataset_id: The ID dataset to which the transaction applies.
350346
351347
:type mutation_pb: :class:`gcloud.datastore.datastore_v1_pb2.Mutation`.
352348
:param mutation_pb: The protobuf for the mutations being saved.
@@ -376,7 +372,8 @@ def rollback(self, dataset_id):
376372
if the connection isn't currently in a transaction.
377373
378374
:type dataset_id: string
379-
:param dataset_id: The dataset to which the transaction belongs.
375+
:param dataset_id: The ID of the dataset to which the transaction
376+
belongs.
380377
"""
381378
if not self.transaction() or not self.transaction().id:
382379
raise ValueError('No transaction to rollback.')
@@ -393,7 +390,8 @@ def allocate_ids(self, dataset_id, key_pbs):
393390
Maps the ``DatastoreService.AllocateIds`` protobuf RPC.
394391
395392
:type dataset_id: string
396-
:param dataset_id: The dataset to which the transaction belongs.
393+
:param dataset_id: The ID of the dataset to which the transaction
394+
belongs.
397395
398396
:type key_pbs: list of :class:`gcloud.datastore.datastore_v1_pb2.Key`
399397
:param key_pbs: The keys for which the backend should allocate IDs.
@@ -418,7 +416,7 @@ def save_entity(self, dataset_id, key_pb, properties,
418416
not passed in 'properties' no longer be set for the entity.
419417
420418
:type dataset_id: string
421-
:param dataset_id: The dataset in which to save the entity.
419+
:param dataset_id: The ID of the dataset in which to save the entity.
422420
423421
:type key_pb: :class:`gcloud.datastore.datastore_v1_pb2.Key`
424422
:param key_pb: The complete or partial key for the entity.
@@ -490,7 +488,7 @@ def delete_entities(self, dataset_id, key_pbs):
490488
:func:`gcloud.datastore.entity.Entity.delete` method.
491489
492490
:type dataset_id: string
493-
:param dataset_id: The dataset from which to delete the keys.
491+
:param dataset_id: The ID of the dataset from which to delete the keys.
494492
495493
:type key_pbs: list of :class:`gcloud.datastore.datastore_v1_pb2.Key`
496494
:param key_pbs: The keys to delete from the datastore.

gcloud/datastore/entity.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ class Entity(dict):
4040
This means you could take an existing entity and change the key
4141
to duplicate the object.
4242
43-
Use :func:`gcloud.datastore.dataset.Dataset.get_entity`
44-
to retrieve an existing entity.
43+
Use :metho:`gcloud.datastore.key.Key.get` to retrieve an existing entity.
4544
46-
>>> dataset.get_entity(key)
45+
>>> key.get()
4746
<Entity[{'kind': 'EntityKind', id: 1234}] {'property': 'value'}>
4847
4948
You can the set values on the entity just like you would on any

gcloud/datastore/key.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,39 +66,25 @@ def __init__(self, *path_args, **kwargs):
6666
keyword argument.
6767
"""
6868
self._flat_path = path_args
69-
self._parent = kwargs.get('parent')
69+
parent = self._parent = kwargs.get('parent')
7070
self._namespace = kwargs.get('namespace')
71-
self._dataset_id = kwargs.get('dataset_id')
71+
dataset_id = kwargs.get('dataset_id')
72+
self._dataset_id = _validate_dataset_id(dataset_id, parent)
7273
# _flat_path, _parent, _namespace and _dataset_id must be set before
7374
# _combine_args() is called.
7475
self._path = self._combine_args()
75-
self._validate_dataset_id()
76-
77-
def _validate_dataset_id(self):
78-
"""Ensures the dataset ID is set.
79-
80-
If unset, attempts to imply the ID from the environment.
81-
82-
:raises: `ValueError` if there is no `dataset_id` and none
83-
can be implied.
84-
"""
85-
if self._dataset_id is None:
86-
if _implicit_environ.DATASET_ID is not None:
87-
self._dataset_id = _implicit_environ.DATASET_ID
88-
else:
89-
raise ValueError('A Key must have a dataset ID set.')
9076

9177
@staticmethod
9278
def _parse_path(path_args):
9379
"""Parses positional arguments into key path with kinds and IDs.
9480
9581
:type path_args: :class:`tuple`
9682
:param path_args: A tuple from positional arguments. Should be
97-
alternating list of kinds (string) and id/name
83+
alternating list of kinds (string) and ID/name
9884
parts (int or string).
9985
10086
:rtype: list of dict
101-
:returns: A list of key parts with kind and id or name set.
87+
:returns: A list of key parts with kind and ID or name set.
10288
:raises: `ValueError` if there are no `path_args`, if one of the
10389
kinds is not a string or if one of the IDs/names is not
10490
a string or an integer.
@@ -140,7 +126,7 @@ def _combine_args(self):
140126
_namespace and _dataset_id if not already set.
141127
142128
:rtype: list of dict
143-
:returns: A list of key parts with kind and id or name set.
129+
:returns: A list of key parts with kind and ID or name set.
144130
:raises: `ValueError` if the parent key is not complete.
145131
"""
146132
child_path = self._parse_path(self._flat_path)
@@ -344,7 +330,7 @@ def dataset_id(self):
344330
"""Dataset ID getter.
345331
346332
:rtype: :class:`str`
347-
:returns: The key's dataset.
333+
:returns: The key's dataset ID.
348334
"""
349335
return self._dataset_id
350336

@@ -383,3 +369,25 @@ def parent(self):
383369

384370
def __repr__(self):
385371
return '<Key%s, dataset=%s>' % (self.path, self.dataset_id)
372+
373+
374+
def _validate_dataset_id(dataset_id, parent):
375+
"""Ensure the dataset ID is set appropriately.
376+
377+
If ``parent`` is passed, skip the test (it will be checked / fixed up
378+
later).
379+
380+
If ``dataset_id`` is unset, attempt to infer the ID from the environment.
381+
382+
:raises: `ValueError` if ``dataset_id`` is None and none can be inferred.
383+
"""
384+
if parent is None:
385+
386+
if dataset_id is None:
387+
388+
if _implicit_environ.DATASET_ID is None:
389+
raise ValueError("A Key must have a dataset ID set.")
390+
391+
dataset_id = _implicit_environ.DATASET_ID
392+
393+
return dataset_id

0 commit comments

Comments
 (0)