@@ -11,6 +11,18 @@ class Key(object):
11
11
"""
12
12
13
13
def __init__ (self , dataset = None , namespace = None , path = None ):
14
+ """Constructor / initializer for a key.
15
+
16
+ :type dataset: :class:`gcloud.datastore.dataset.Dataset`
17
+ :param dataset: A dataset instance for the key.
18
+
19
+ :type namespace: :class:`str`
20
+ :param namespace: A namespace identifier for the key.
21
+
22
+ :type path: sequence of dicts
23
+ :param path: Each dict must have keys 'kind' (a string) and optionally
24
+ 'name' (a string) or 'id' (an integer).
25
+ """
14
26
self ._dataset = dataset
15
27
self ._namespace = namespace
16
28
self ._path = path or [{'kind' : '' }]
@@ -28,6 +40,20 @@ def _clone(self):
28
40
29
41
@classmethod
30
42
def from_protobuf (cls , pb , dataset = None ):
43
+ """Factory method for creating a key based on a protobuf.
44
+
45
+ The protobuf should be one returned from the Cloud Datastore Protobuf API.
46
+
47
+ :type pb: :class:`gcloud.datastore.datastore_v1_pb2.Key`
48
+ :param pb: The Protobuf representing the key.
49
+
50
+ :type dataset: :class:`gcloud.datastore.dataset.Dataset`
51
+ :param dataset: A dataset instance. If not passed, defaults to an
52
+ instance whose ID is derived from pb.
53
+
54
+ :rtype: :class:`gcloud.datastore.key.Key`
55
+ :returns: a new `Key` instance
56
+ """
31
57
path = []
32
58
for element in pb .path_element :
33
59
element_dict = {'kind' : element .kind }
@@ -46,6 +72,11 @@ def from_protobuf(cls, pb, dataset=None):
46
72
return cls (path = path , dataset = dataset )
47
73
48
74
def to_protobuf (self ):
75
+ """Return a protobuf corresponding to the key.
76
+
77
+ :rtype: :class:`gcloud.datastore.datastore_v1_pb2.Key`
78
+ :returns: The Protobuf representing the key.
79
+ """
49
80
key = datastore_pb .Key ()
50
81
51
82
# Technically a dataset is required to do anything with the key,
@@ -77,6 +108,20 @@ def to_protobuf(self):
77
108
78
109
@classmethod
79
110
def from_path (cls , * args , ** kwargs ):
111
+ """Factory method for creating a key based on a path.
112
+
113
+ :type args: :class:`tuple
114
+ :param args: sequence of even length, where the first of each
115
+ pair is a string representing the 'kind' of the path element, and the
116
+ second of the pair is either a string (for the path element's name)
117
+ or an integer (for its id).
118
+
119
+ :type kwargs: :class:`dict`
120
+ :param kwargs: Other named parameters which can be passed to `__init__()`.
121
+
122
+ :rtype: :class:`gcloud.datastore.key.Key`
123
+ :returns: a new `Key` instance
124
+ """
80
125
path = []
81
126
items = iter (args )
82
127
@@ -92,9 +137,25 @@ def from_path(cls, *args, **kwargs):
92
137
return cls (** kwargs )
93
138
94
139
def is_partial (self ):
140
+ """Boolean test: is the key fully mapped onto a backend entity?
141
+
142
+ :rtype: :class:`bool`
143
+ :returns: True if the last element of the key's path does not have an 'id'
144
+ or a 'name'.
145
+ """
95
146
return (self .id_or_name () is None )
96
147
97
148
def dataset (self , dataset = None ):
149
+ """Setter / getter.
150
+
151
+ :type dataset: :class:`gcloud.datastore.dataset.Dataset`
152
+ :param dataset: A dataset instance for the key.
153
+
154
+ :rtype: :class:`Key` (for setter); or
155
+ :class:`gcloud.datastore.dataset.Dataset` (for getter)
156
+ :returns: a new key, cloned from self., with the given dataset (setter);
157
+ or self's dataset (getter).
158
+ """
98
159
if dataset :
99
160
clone = self ._clone ()
100
161
clone ._dataset = dataset
@@ -103,6 +164,15 @@ def dataset(self, dataset=None):
103
164
return self ._dataset
104
165
105
166
def namespace (self , namespace = None ):
167
+ """Setter / getter.
168
+
169
+ :type namespace: :class:`str`
170
+ :param namespace: A namespace identifier for the key.
171
+
172
+ :rtype: :class:`Key` (for setter); or :class:`str` (for getter)
173
+ :returns: a new key, cloned from self., with the given namespace (setter);
174
+ or self's namespace (getter).
175
+ """
106
176
if namespace :
107
177
clone = self ._clone ()
108
178
clone ._namespace = namespace
@@ -111,6 +181,16 @@ def namespace(self, namespace=None):
111
181
return self ._namespace
112
182
113
183
def path (self , path = None ):
184
+ """Setter / getter.
185
+
186
+ :type path: sequence of dicts
187
+ :param path: Each dict must have keys 'kind' (a string) and optionally
188
+ 'name' (a string) or 'id' (an integer).
189
+
190
+ :rtype: :class:`Key` (for setter); or :class:`str` (for getter)
191
+ :returns: a new key, cloned from self., with the given path (setter);
192
+ or self's path (getter).
193
+ """
114
194
if path :
115
195
clone = self ._clone ()
116
196
clone ._path = path
@@ -119,6 +199,15 @@ def path(self, path=None):
119
199
return self ._path
120
200
121
201
def kind (self , kind = None ):
202
+ """Setter / getter. Based on the last element of path.
203
+
204
+ :type kind: :class:`str`
205
+ :param kind: The new kind for the key.
206
+
207
+ :rtype: :class:`Key` (for setter); or :class:`str` (for getter)
208
+ :returns: a new key, cloned from self., with the given kind (setter);
209
+ or self's kind (getter).
210
+ """
122
211
if kind :
123
212
clone = self ._clone ()
124
213
clone ._path [- 1 ]['kind' ] = kind
@@ -127,6 +216,15 @@ def kind(self, kind=None):
127
216
return self ._path [- 1 ]['kind' ]
128
217
129
218
def id (self , id = None ):
219
+ """Setter / getter. Based on the last element of path.
220
+
221
+ :type kind: :class:`str`
222
+ :param kind: The new kind for the key.
223
+
224
+ :rtype: :class:`Key` (for setter); or :class:`int` (for getter)
225
+ :returns: a new key, cloned from self., with the given id (setter);
226
+ or self's id (getter).
227
+ """
130
228
if id :
131
229
clone = self ._clone ()
132
230
clone ._path [- 1 ]['id' ] = id
@@ -135,6 +233,15 @@ def id(self, id=None):
135
233
return self ._path [- 1 ].get ('id' )
136
234
137
235
def name (self , name = None ):
236
+ """Setter / getter. Based on the last element of path.
237
+
238
+ :type kind: :class:`str`
239
+ :param kind: The new name for the key.
240
+
241
+ :rtype: :class:`Key` (for setter); or :class:`str` (for getter)
242
+ :returns: a new key, cloned from self., with the given name (setter);
243
+ or self's name (getter).
244
+ """
138
245
if name :
139
246
clone = self ._clone ()
140
247
clone ._path [- 1 ]['name' ] = name
@@ -143,6 +250,12 @@ def name(self, name=None):
143
250
return self ._path [- 1 ].get ('name' )
144
251
145
252
def id_or_name (self ):
253
+ """Getter. Based on the last element of path.
254
+
255
+ :rtype: :class:`int` (if 'id' is set); or :class:`str` (the 'name')
256
+ :returns: True if the last element of the key's path has either an 'id'
257
+ or a 'name'.
258
+ """
146
259
return self .id () or self .name ()
147
260
148
261
def parent (self ):#pragma NO COVER
0 commit comments