-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
key.py
285 lines (225 loc) · 9.35 KB
/
key.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""Create / interact with gcloud datastore keys."""
import copy
from itertools import izip
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
from gcloud.datastore.dataset import Dataset
class Key(object):
"""An immutable representation of a datastore Key.
.. automethod:: __init__
"""
def __init__(self, dataset=None, namespace=None, path=None):
"""Constructor / initializer for a key.
:type dataset: :class:`gcloud.datastore.dataset.Dataset`
:param dataset: A dataset instance for the key.
:type namespace: :class:`str`
:param namespace: A namespace identifier for the key.
:type path: sequence of dicts
:param path: Each dict must have keys 'kind' (a string) and optionally
'name' (a string) or 'id' (an integer).
"""
self._dataset = dataset
self._namespace = namespace
self._path = path or [{'kind': ''}]
def _clone(self):
"""Duplicates the Key.
We make a shallow copy of the :class:`gcloud.datastore.dataset.Dataset`
because it holds a reference an authenticated connection,
which we don't want to lose.
:rtype: :class:`gcloud.datastore.key.Key`
:returns: a new `Key` instance
"""
clone = copy.deepcopy(self)
clone._dataset = self._dataset # Make a shallow copy of the Dataset.
return clone
@classmethod
def from_protobuf(cls, pb, dataset=None):
"""Factory method for creating a key based on a protobuf.
The protobuf should be one returned from the Cloud Datastore
Protobuf API.
:type pb: :class:`gcloud.datastore.datastore_v1_pb2.Key`
:param pb: The Protobuf representing the key.
:type dataset: :class:`gcloud.datastore.dataset.Dataset`
:param dataset: A dataset instance. If not passed, defaults to an
instance whose ID is derived from pb.
:rtype: :class:`gcloud.datastore.key.Key`
:returns: a new `Key` instance
"""
path = []
for element in pb.path_element:
element_dict = {'kind': element.kind}
if element.HasField('id'):
element_dict['id'] = element.id
# This is safe: we expect proto objects returned will only have
# one of `name` or `id` set.
if element.HasField('name'):
element_dict['name'] = element.name
path.append(element_dict)
if not dataset:
dataset = Dataset(id=pb.partition_id.dataset_id)
namespace = pb.partition_id.namespace
else:
namespace = None
return cls(dataset, namespace, path)
def to_protobuf(self):
"""Return a protobuf corresponding to the key.
:rtype: :class:`gcloud.datastore.datastore_v1_pb2.Key`
:returns: The Protobuf representing the key.
"""
key = datastore_pb.Key()
# Technically a dataset is required to do anything with the key,
# but we shouldn't throw a cryptic error if one isn't provided
# in the initializer.
if self.dataset():
dataset_id = self.dataset().id()
if dataset_id:
key.partition_id.dataset_id = dataset_id
if self._namespace:
key.partition_id.namespace = self._namespace
for item in self.path():
element = key.path_element.add()
if 'kind' in item:
element.kind = item['kind']
if 'id' in item:
element.id = item['id']
if 'name' in item:
element.name = item['name']
return key
@classmethod
def from_path(cls, *args, **kwargs):
"""Factory method for creating a key based on a path.
:type args: :class:`tuple`
:param args: sequence of even length, where the first of each pair is a
string representing the 'kind' of the path element, and
the second of the pair is either a string (for the path
element's name) or an integer (for its id).
:type kwargs: :class:`dict`
:param kwargs: Other named parameters which can be passed to
:func:`Key.__init__`.
:rtype: :class:`gcloud.datastore.key.Key`
:returns: a new :class:`Key` instance
"""
if len(args) % 2:
raise ValueError('Must pass an even number of args.')
path = []
items = iter(args)
for kind, id_or_name in izip(items, items):
entry = {'kind': kind}
if isinstance(id_or_name, basestring):
entry['name'] = id_or_name
else:
entry['id'] = id_or_name
path.append(entry)
kwargs['path'] = path
return cls(**kwargs)
def is_partial(self):
"""Boolean test: is the key fully mapped onto a backend entity?
:rtype: :class:`bool`
:returns: True if the last element of the key's path does not have
an 'id' or a 'name'.
"""
return self.id_or_name() is None
def dataset(self, dataset=None):
"""Dataset setter / getter.
:type dataset: :class:`gcloud.datastore.dataset.Dataset`
:param dataset: A dataset instance for the key.
:rtype: :class:`Key` (for setter); or
:class:`gcloud.datastore.dataset.Dataset` (for getter)
:returns: a new key, cloned from self., with the given dataset
(setter); or self's dataset (getter).
"""
if dataset:
clone = self._clone()
clone._dataset = dataset
return clone
else:
return self._dataset
def namespace(self, namespace=None):
"""Namespace setter / getter.
:type namespace: :class:`str`
:param namespace: A namespace identifier for the key.
:rtype: :class:`Key` (for setter); or :class:`str` (for getter)
:returns: a new key, cloned from self., with the given namespace
(setter); or self's namespace (getter).
"""
if namespace:
clone = self._clone()
clone._namespace = namespace
return clone
else:
return self._namespace
def path(self, path=None):
"""Path setter / getter.
:type path: sequence of dicts
:param path: Each dict must have keys 'kind' (a string) and optionally
'name' (a string) or 'id' (an integer).
:rtype: :class:`Key` (for setter); or :class:`str` (for getter)
:returns: a new key, cloned from self., with the given path (setter);
or self's path (getter).
"""
if path:
clone = self._clone()
clone._path = path
return clone
else:
return self._path
def kind(self, kind=None):
"""Kind setter / getter. Based on the last element of path.
:type kind: :class:`str`
:param kind: The new kind for the key.
:rtype: :class:`Key` (for setter); or :class:`str` (for getter)
:returns: a new key, cloned from self., with the given kind (setter);
or self's kind (getter).
"""
if kind:
clone = self._clone()
clone._path[-1]['kind'] = kind
return clone
elif self.path():
return self._path[-1]['kind']
def id(self, id_to_set=None):
"""ID setter / getter. Based on the last element of path.
:type id_to_set: :class:`int`
:param id_to_set: The new ID for the key.
:rtype: :class:`Key` (for setter); or :class:`int` (for getter)
:returns: a new key, cloned from self., with the given id (setter);
or self's id (getter).
"""
if id_to_set:
clone = self._clone()
clone._path[-1]['id'] = id_to_set
return clone
elif self.path():
return self._path[-1].get('id')
def name(self, name=None):
"""Name setter / getter. Based on the last element of path.
:type kind: :class:`str`
:param kind: The new name for the key.
:rtype: :class:`Key` (for setter); or :class:`str` (for getter)
:returns: a new key, cloned from self., with the given name (setter);
or self's name (getter).
"""
if name:
clone = self._clone()
clone._path[-1]['name'] = name
return clone
elif self.path():
return self._path[-1].get('name')
def id_or_name(self):
"""Getter. Based on the last element of path.
:rtype: :class:`int` (if 'id' is set); or :class:`str` (the 'name')
:returns: True if the last element of the key's path has either an 'id'
or a 'name'.
"""
return self.id() or self.name()
def parent(self):
"""Getter: return a new key for the next highest element in path.
:rtype: :class:`gcloud.datastore.key.Key`
:returns: a new `Key` instance, whose path consists of all but the last
element of self's path. If self has only one path element,
return None.
"""
if len(self._path) <= 1:
return None
return self.path(self.path()[:-1])
def __repr__(self):
return '<Key%s>' % self.path()