-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathkey.py
More file actions
230 lines (181 loc) · 7.49 KB
/
key.py
File metadata and controls
230 lines (181 loc) · 7.49 KB
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
"""Create / interact with gcloud datastore keys."""
import copy
from itertools import izip
import six
from gcloud.datastore import datastore_v1_pb2 as datastore_pb
class Key(object):
"""An immutable representation of a datastore Key.
.. automethod:: __init__
"""
def __init__(self, path=None, namespace=None, dataset_id=None):
"""Constructor / initializer for a 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).
:type dataset_id: string
:param dataset: The dataset ID assigned by back-end for the key.
.. note::
The key's ``_dataset_id`` field must be None for keys created
by application code. The
:func:`gcloud.datastore.helpers.key_from_protobuf` factory
will be set the field to an appropriate value for keys
returned from the datastore backend. The application
**must** treat any value set by the back-end as opaque.
"""
self._path = path or [{'kind': ''}]
self._namespace = namespace
self._dataset_id = dataset_id
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
"""
return copy.deepcopy(self)
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()
if self._dataset_id is not None:
key.partition_id.dataset_id = self._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, six.string_types):
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 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()