This repository was archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmetadata.py
More file actions
350 lines (263 loc) · 9.74 KB
/
metadata.py
File metadata and controls
350 lines (263 loc) · 9.74 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#
# Copyright 2008 The ndb Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Models and helper functions for access to app's datastore metadata.
These entities cannot be created by users, but are created as results of
__namespace__, __kind__, __property__ and __entity_group__ metadata queries
or gets.
A simplified API is also offered:
ndb.metadata.get_namespaces(): A list of namespace names.
ndb.metadata.get_kinds(): A list of kind names.
ndb.metadata.get_properties_of_kind(kind):
A list of property names for the given kind name.
ndb.metadata.get_representations_of_kind(kind):
A dict mapping property names to lists of representation ids.
ndb.metadata.get_entity_group_version(key):
The version of the entity group containing key (HRD only).
get_kinds(), get_properties_of_kind(), get_representations_of_kind()
implicitly apply to the current namespace.
get_namespaces(), get_kinds(), get_properties_of_kind(),
get_representations_of_kind() have optional start and end arguments to limit the
query to a range of names, such that start <= name < end.
"""
from . import model
__all__ = ['Namespace', 'Kind', 'Property', 'EntityGroup',
'get_namespaces', 'get_kinds',
'get_properties_of_kind', 'get_representations_of_kind',
'get_entity_group_version',
]
class _BaseMetadata(model.Model):
"""Base class for all metadata models."""
_use_cache = False
_use_memcache = False
KIND_NAME = '' # Don't instantiate this class; always use a subclass.
@classmethod
def _get_kind(cls):
"""Kind name override."""
return cls.KIND_NAME
class Namespace(_BaseMetadata):
"""Model for __namespace__ metadata query results."""
KIND_NAME = '__namespace__'
EMPTY_NAMESPACE_ID = 1 # == datastore_types._EMPTY_NAMESPACE_ID
@property
def namespace_name(self):
"""Return the namespace name specified by this entity's key."""
return self.key_to_namespace(self.key)
@classmethod
def key_for_namespace(cls, namespace):
"""Return the Key for a namespace.
Args:
namespace: A string giving the namespace whose key is requested.
Returns:
The Key for the namespace.
"""
if namespace:
return model.Key(cls.KIND_NAME, namespace)
else:
return model.Key(cls.KIND_NAME, cls.EMPTY_NAMESPACE_ID)
@classmethod
def key_to_namespace(cls, key):
"""Return the namespace specified by a given __namespace__ key.
Args:
key: key whose name is requested.
Returns:
The namespace specified by key.
"""
return key.string_id() or ''
class Kind(_BaseMetadata):
"""Model for __kind__ metadata query results."""
KIND_NAME = '__kind__'
@property
def kind_name(self):
"""Return the kind name specified by this entity's key."""
return self.key_to_kind(self.key)
@classmethod
def key_for_kind(cls, kind):
"""Return the __kind__ key for kind.
Args:
kind: kind whose key is requested.
Returns:
The key for kind.
"""
return model.Key(cls.KIND_NAME, kind)
@classmethod
def key_to_kind(cls, key):
"""Return the kind specified by a given __kind__ key.
Args:
key: key whose name is requested.
Returns:
The kind specified by key.
"""
return key.id()
class Property(_BaseMetadata):
"""Model for __property__ metadata query results."""
KIND_NAME = '__property__'
@property
def property_name(self):
"""Return the property name specified by this entity's key."""
return self.key_to_property(self.key)
@property
def kind_name(self):
"""Return the kind name specified by this entity's key."""
return self.key_to_kind(self.key)
property_representation = model.StringProperty(repeated=True)
@classmethod
def key_for_kind(cls, kind):
"""Return the __property__ key for kind.
Args:
kind: kind whose key is requested.
Returns:
The parent key for __property__ keys of kind.
"""
return model.Key(Kind.KIND_NAME, kind)
@classmethod
def key_for_property(cls, kind, property):
"""Return the __property__ key for property of kind.
Args:
kind: kind whose key is requested.
property: property whose key is requested.
Returns:
The key for property of kind.
"""
return model.Key(Kind.KIND_NAME, kind, Property.KIND_NAME, property)
@classmethod
def key_to_kind(cls, key):
"""Return the kind specified by a given __property__ key.
Args:
key: key whose kind name is requested.
Returns:
The kind specified by key.
"""
if key.kind() == Kind.KIND_NAME:
return key.id()
else:
return key.parent().id()
@classmethod
def key_to_property(cls, key):
"""Return the property specified by a given __property__ key.
Args:
key: key whose property name is requested.
Returns:
property specified by key, or None if the key specified only a kind.
"""
if key.kind() == Kind.KIND_NAME:
return None
else:
return key.id()
class EntityGroup(_BaseMetadata):
"""Model for __entity_group__ metadata (available in HR datastore only).
This metadata contains a numeric __version__ property that is guaranteed
to increase on every change to the entity group. The version may increase
even in the absence of user-visible changes to the entity group. The
__entity_group__ entity may not exist if the entity group was never
written to.
"""
KIND_NAME = '__entity_group__'
ID = 1
version = model.IntegerProperty(name='__version__')
@classmethod
def key_for_entity_group(cls, key):
"""Return the key for the entity group containing key.
Args:
key: a key for an entity group whose __entity_group__ key you want.
Returns:
The __entity_group__ key for the entity group containing key.
"""
return model.Key(cls.KIND_NAME, cls.ID, parent=key.root())
def get_namespaces(start=None, end=None):
"""Return all namespaces in the specified range.
Args:
start: only return namespaces >= start if start is not None.
end: only return namespaces < end if end is not None.
Returns:
A list of namespace names between the (optional) start and end values.
"""
q = Namespace.query()
if start is not None:
q = q.filter(Namespace.key >= Namespace.key_for_namespace(start))
if end is not None:
q = q.filter(Namespace.key < Namespace.key_for_namespace(end))
return [x.namespace_name for x in q]
def get_kinds(start=None, end=None):
"""Return all kinds in the specified range, for the current namespace.
Args:
start: only return kinds >= start if start is not None.
end: only return kinds < end if end is not None.
Returns:
A list of kind names between the (optional) start and end values.
"""
q = Kind.query()
if start is not None and start != '':
q = q.filter(Kind.key >= Kind.key_for_kind(start))
if end is not None:
if end == '':
return []
q = q.filter(Kind.key < Kind.key_for_kind(end))
return [x.kind_name for x in q]
def get_properties_of_kind(kind, start=None, end=None):
"""Return all properties of kind in the specified range.
NOTE: This function does not return unindexed properties.
Args:
kind: name of kind whose properties you want.
start: only return properties >= start if start is not None.
end: only return properties < end if end is not None.
Returns:
A list of property names of kind between the (optional) start and end
values.
"""
q = Property.query(ancestor=Property.key_for_kind(kind))
if start is not None and start != '':
q = q.filter(Property.key >= Property.key_for_property(kind, start))
if end is not None:
if end == '':
return []
q = q.filter(Property.key < Property.key_for_property(kind, end))
return [Property.key_to_property(k) for k in q.iter(keys_only=True)]
def get_representations_of_kind(kind, start=None, end=None):
"""Return all representations of properties of kind in the specified range.
NOTE: This function does not return unindexed properties.
Args:
kind: name of kind whose properties you want.
start: only return properties >= start if start is not None.
end: only return properties < end if end is not None.
Returns:
A dictionary mapping property names to its list of representations.
"""
q = Property.query(ancestor=Property.key_for_kind(kind))
if start is not None and start != '':
q = q.filter(Property.key >= Property.key_for_property(kind, start))
if end is not None:
if end == '':
return {}
q = q.filter(Property.key < Property.key_for_property(kind, end))
result = {}
for property in q:
result[property.property_name] = property.property_representation
return result
def get_entity_group_version(key):
"""Return the version of the entity group containing key.
Args:
key: a key for an entity group whose __entity_group__ key you want.
Returns:
The version of the entity group containing key. This version is
guaranteed to increase on every change to the entity group. The version
may increase even in the absence of user-visible changes to the entity
group. May return None if the entity group was never written to.
On non-HR datatores, this function returns None.
"""
eg = EntityGroup.key_for_entity_group(key).get()
if eg:
return eg.version
else:
return None