RacksDB 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- RacksDB-0.2.0.dist-info/LICENSE +674 -0
- RacksDB-0.2.0.dist-info/METADATA +126 -0
- RacksDB-0.2.0.dist-info/RECORD +42 -0
- RacksDB-0.2.0.dist-info/WHEEL +5 -0
- RacksDB-0.2.0.dist-info/entry_points.txt +3 -0
- RacksDB-0.2.0.dist-info/top_level.txt +1 -0
- racksdb/__init__.py +74 -0
- racksdb/bases.py +117 -0
- racksdb/drawers/__init__.py +10 -0
- racksdb/drawers/base.py +42 -0
- racksdb/drawers/infrastructure.py +253 -0
- racksdb/drawers/room.py +160 -0
- racksdb/dtypes/__init__.py +0 -0
- racksdb/dtypes/angle.py +21 -0
- racksdb/dtypes/bytes.py +31 -0
- racksdb/dtypes/dimension.py +23 -0
- racksdb/dtypes/netif_type.py +17 -0
- racksdb/dtypes/rack_height.py +17 -0
- racksdb/dtypes/rack_width.py +26 -0
- racksdb/dtypes/storage_type.py +17 -0
- racksdb/errors.py +9 -0
- racksdb/exec.py +218 -0
- racksdb/generic/__init__.py +0 -0
- racksdb/generic/db.py +609 -0
- racksdb/generic/definedtype.py +24 -0
- racksdb/generic/dumpers/__init__.py +30 -0
- racksdb/generic/dumpers/_common.py +30 -0
- racksdb/generic/dumpers/console.py +31 -0
- racksdb/generic/dumpers/json.py +108 -0
- racksdb/generic/dumpers/yaml.py +138 -0
- racksdb/generic/errors.py +21 -0
- racksdb/generic/openapi.py +199 -0
- racksdb/generic/schema.py +367 -0
- racksdb/generic/views.py +139 -0
- racksdb/tests/__init__.py +0 -0
- racksdb/tests/test_schema.py +211 -0
- racksdb/tests/test_schema_defined_type_loader.py +23 -0
- racksdb/tests/tests_db_load.py +83 -0
- racksdb/version.py +11 -0
- racksdb/views.py +136 -0
- racksdb/web/__init__.py +0 -0
- racksdb/web/app.py +195 -0
racksdb/generic/db.py
ADDED
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
# Copyright (c) 2022-2023 Rackslab
|
|
2
|
+
#
|
|
3
|
+
# This file is part of RacksDB.
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
|
|
9
|
+
import yaml
|
|
10
|
+
from ClusterShell.NodeSet import NodeSet
|
|
11
|
+
|
|
12
|
+
from .errors import DBFormatError
|
|
13
|
+
from .definedtype import SchemaDefinedType
|
|
14
|
+
from .schema import (
|
|
15
|
+
SchemaGenericValueType,
|
|
16
|
+
SchemaNativeType,
|
|
17
|
+
SchemaContainerList,
|
|
18
|
+
SchemaExpandable,
|
|
19
|
+
SchemaRangeId,
|
|
20
|
+
SchemaObject,
|
|
21
|
+
SchemaReference,
|
|
22
|
+
SchemaBackReference,
|
|
23
|
+
SchemaProperty,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
logger = logging.getLogger(__name__)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class DBObject:
|
|
30
|
+
LOADED_PREFIX = "__loaded_"
|
|
31
|
+
|
|
32
|
+
def __init__(self, db, schema):
|
|
33
|
+
self._db = db
|
|
34
|
+
self._schema = schema
|
|
35
|
+
|
|
36
|
+
def _filter(self, **kwargs):
|
|
37
|
+
"""Abstract filter method, must be overriden in specialized bases module
|
|
38
|
+
classes when filtering is needed."""
|
|
39
|
+
return True
|
|
40
|
+
|
|
41
|
+
def _computed_props(self):
|
|
42
|
+
"""Generator to iterate over the list of DBObject computed properties."""
|
|
43
|
+
if hasattr(self, "COMPUTED_PROPERTIES"):
|
|
44
|
+
for prop in self.COMPUTED_PROPERTIES:
|
|
45
|
+
yield prop
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class DBExpandableObject(DBObject):
|
|
49
|
+
def _attributes(self):
|
|
50
|
+
"""Return the dict of stable attributes, range attribues and the pair of range
|
|
51
|
+
attribute and value."""
|
|
52
|
+
stable_attributes = {}
|
|
53
|
+
range_attribute = None
|
|
54
|
+
rangeid_attributes = {}
|
|
55
|
+
for attribute, value in vars(self).items():
|
|
56
|
+
# Skip selection of special _key attribute at this stage, it is
|
|
57
|
+
# handled later when attributes are set on freshly instanciated
|
|
58
|
+
# object.
|
|
59
|
+
if attribute == "_key":
|
|
60
|
+
continue
|
|
61
|
+
if isinstance(value, DBObjectRange):
|
|
62
|
+
range_attribute = (attribute, value)
|
|
63
|
+
elif isinstance(value, DBObjectRangeId):
|
|
64
|
+
rangeid_attributes[attribute] = value
|
|
65
|
+
else:
|
|
66
|
+
stable_attributes[attribute] = value
|
|
67
|
+
return stable_attributes, range_attribute, rangeid_attributes
|
|
68
|
+
|
|
69
|
+
def _instanciate_obj(
|
|
70
|
+
self, index, value, range_attribute, rangeid_attributes, stable_attributes
|
|
71
|
+
):
|
|
72
|
+
"""Instanciate the object at the given index."""
|
|
73
|
+
|
|
74
|
+
_attributes = stable_attributes.copy()
|
|
75
|
+
_attributes[range_attribute[0]] = value
|
|
76
|
+
for rangeid_name, rangeid_value in rangeid_attributes.items():
|
|
77
|
+
_attributes[rangeid_name] = rangeid_value.index(index)
|
|
78
|
+
|
|
79
|
+
bases = [DBObject]
|
|
80
|
+
# Add provided module base if defined
|
|
81
|
+
try:
|
|
82
|
+
# Insert bases module class in the beginning of the list to make
|
|
83
|
+
# sure methods from this classes are called over the methods from
|
|
84
|
+
# DBObject.
|
|
85
|
+
bases.insert(
|
|
86
|
+
0,
|
|
87
|
+
getattr(
|
|
88
|
+
self._db._bases,
|
|
89
|
+
f"{self._db._prefix}{self._schema.name}Base",
|
|
90
|
+
),
|
|
91
|
+
)
|
|
92
|
+
except AttributeError:
|
|
93
|
+
pass
|
|
94
|
+
obj = type(f"{self._db._prefix}{self._schema.name}", tuple(bases), dict())(
|
|
95
|
+
self._db, self._schema
|
|
96
|
+
)
|
|
97
|
+
for attr_name, attr_value in _attributes.items():
|
|
98
|
+
setattr(obj, attr_name, attr_value)
|
|
99
|
+
# Set object _key attribute if property is a key
|
|
100
|
+
prop = self._schema.prop(attr_name)
|
|
101
|
+
if prop is not None and prop.key:
|
|
102
|
+
setattr(obj, "_key", attr_value)
|
|
103
|
+
return obj
|
|
104
|
+
|
|
105
|
+
def objects(self):
|
|
106
|
+
"""Return the list of all expanded objects."""
|
|
107
|
+
result = []
|
|
108
|
+
stable_attributes, range_attribute, rangeid_attributes = self._attributes()
|
|
109
|
+
first = None
|
|
110
|
+
for index, value in enumerate(range_attribute[1].expanded()):
|
|
111
|
+
obj = self._instanciate_obj(
|
|
112
|
+
index, value, range_attribute, rangeid_attributes, stable_attributes
|
|
113
|
+
)
|
|
114
|
+
if first is None:
|
|
115
|
+
first = obj
|
|
116
|
+
setattr(obj, "_first", first)
|
|
117
|
+
result.append(obj)
|
|
118
|
+
return result
|
|
119
|
+
|
|
120
|
+
def getobject(self, key):
|
|
121
|
+
"""Return an instance of the object with provided key. The first object is also
|
|
122
|
+
instanciated and linked in _first attribute."""
|
|
123
|
+
stable_attributes, range_attribute, rangeid_attributes = self._attributes()
|
|
124
|
+
first = None
|
|
125
|
+
for index, value in enumerate(range_attribute[1].expanded()):
|
|
126
|
+
if first and value != key:
|
|
127
|
+
continue
|
|
128
|
+
obj = self._instanciate_obj(
|
|
129
|
+
index, value, range_attribute, rangeid_attributes, stable_attributes
|
|
130
|
+
)
|
|
131
|
+
if first is None:
|
|
132
|
+
first = obj
|
|
133
|
+
setattr(obj, "_first", first)
|
|
134
|
+
if value == key:
|
|
135
|
+
return obj
|
|
136
|
+
|
|
137
|
+
raise KeyError(f"key '{key}' not found in {str(range_attribute[1])}")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class DBObjectRange:
|
|
141
|
+
def __init__(self, rangeset):
|
|
142
|
+
self.rangeset = NodeSet(rangeset)
|
|
143
|
+
|
|
144
|
+
def expanded(self):
|
|
145
|
+
return list(self.rangeset)
|
|
146
|
+
|
|
147
|
+
def __repr__(self):
|
|
148
|
+
return str(self.rangeset)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class DBObjectRangeId:
|
|
152
|
+
def __init__(self, start):
|
|
153
|
+
self.start = start
|
|
154
|
+
|
|
155
|
+
def index(self, value):
|
|
156
|
+
return self.start + value
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class DBList(list):
|
|
160
|
+
def __iter__(self):
|
|
161
|
+
for item in super().__iter__():
|
|
162
|
+
if isinstance(item, DBExpandableObject):
|
|
163
|
+
for expanded_item in item.objects():
|
|
164
|
+
yield expanded_item
|
|
165
|
+
else:
|
|
166
|
+
yield item
|
|
167
|
+
|
|
168
|
+
def __len__(self):
|
|
169
|
+
"""Return the number of values in the list."""
|
|
170
|
+
return len([value for value in self])
|
|
171
|
+
|
|
172
|
+
def itervalues(self):
|
|
173
|
+
"""Additional iterators over the list values that does not trigger expansion of
|
|
174
|
+
DBExpandableObjects."""
|
|
175
|
+
for item in super().__iter__():
|
|
176
|
+
yield item
|
|
177
|
+
|
|
178
|
+
def filter(self, **kwargs):
|
|
179
|
+
"""Return a copy of the current DBList without values that do not match provided
|
|
180
|
+
filter criteria."""
|
|
181
|
+
result = DBList()
|
|
182
|
+
for item in self:
|
|
183
|
+
if item._filter(**kwargs):
|
|
184
|
+
result.append(item)
|
|
185
|
+
return result
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class DBDict(dict):
|
|
189
|
+
def filter(self, **kwargs):
|
|
190
|
+
"""Return a copy of the current DBDict without key and values that do not match
|
|
191
|
+
provided filter criteria."""
|
|
192
|
+
result = DBDict()
|
|
193
|
+
for key, value in self.items():
|
|
194
|
+
if value._filter(**kwargs):
|
|
195
|
+
result[key] = value
|
|
196
|
+
return result
|
|
197
|
+
|
|
198
|
+
def __iter__(self):
|
|
199
|
+
for item in self.values():
|
|
200
|
+
if isinstance(item, DBExpandableObject):
|
|
201
|
+
for expanded_item in item.objects():
|
|
202
|
+
yield expanded_item
|
|
203
|
+
else:
|
|
204
|
+
yield item
|
|
205
|
+
|
|
206
|
+
def __getitem__(self, key):
|
|
207
|
+
# Try to get the item from parent dict. If the key cannot be found in dict,
|
|
208
|
+
# search for the key in all DBObjectRange keys. If found, return an instance
|
|
209
|
+
# of this particular expanded object.
|
|
210
|
+
try:
|
|
211
|
+
return super().__getitem__(key)
|
|
212
|
+
except KeyError:
|
|
213
|
+
for _key in self.keys():
|
|
214
|
+
if isinstance(_key, DBObjectRange) and key in _key.rangeset:
|
|
215
|
+
return super().__getitem__(_key).getobject(key)
|
|
216
|
+
raise KeyError(key)
|
|
217
|
+
|
|
218
|
+
def __len__(self):
|
|
219
|
+
"""Return the number of values in the dictionnary. It counts the number of
|
|
220
|
+
values in expandable objects by counting the number of values in the rangeset
|
|
221
|
+
without requiring instanciating of objects in memory."""
|
|
222
|
+
values = 0
|
|
223
|
+
for key in self.keys():
|
|
224
|
+
if isinstance(key, DBObjectRange):
|
|
225
|
+
values += len(key.rangeset)
|
|
226
|
+
else:
|
|
227
|
+
values += 1
|
|
228
|
+
return values
|
|
229
|
+
|
|
230
|
+
def first(self):
|
|
231
|
+
"""Return the first expanded object of the dictionnary."""
|
|
232
|
+
return list(self)[0] # list() calls __iter__()
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class DBFileLoader:
|
|
236
|
+
def __init__(self, path):
|
|
237
|
+
with open(path) as fh:
|
|
238
|
+
try:
|
|
239
|
+
self.content = yaml.safe_load(fh)
|
|
240
|
+
except yaml.composer.ComposerError as err:
|
|
241
|
+
raise DBFormatError(err)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
class DBSplittedFilesLoader:
|
|
245
|
+
def __init__(self, path):
|
|
246
|
+
# try the parent folder
|
|
247
|
+
if not path.exists():
|
|
248
|
+
raise DBFormatError(f"DB path {path} does not exist")
|
|
249
|
+
elif path.is_file():
|
|
250
|
+
if not path.name.endswith(".yml"):
|
|
251
|
+
raise DBFormatError(f"DB contains file {path} without .yml extension")
|
|
252
|
+
logger.debug("Loading DB file %s", path)
|
|
253
|
+
self.content = DBFileLoader(path).content
|
|
254
|
+
elif path.suffix == ".l":
|
|
255
|
+
self.content = []
|
|
256
|
+
for item in path.iterdir():
|
|
257
|
+
self.content.append(DBSplittedFilesLoader(item).content)
|
|
258
|
+
else:
|
|
259
|
+
# if directory, load recursively
|
|
260
|
+
self.content = {}
|
|
261
|
+
for item in path.iterdir():
|
|
262
|
+
logger.debug("Loading DB directory %s", path)
|
|
263
|
+
self.content[item.stem] = DBSplittedFilesLoader(item).content
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
class GenericDB(DBObject):
|
|
267
|
+
def __init__(self, prefix, schema, bases):
|
|
268
|
+
super().__init__(self, schema)
|
|
269
|
+
self._prefix = prefix
|
|
270
|
+
# Module of base classes for instanciated DB objects
|
|
271
|
+
self._bases = bases
|
|
272
|
+
self._indexes = {} # objects indexes
|
|
273
|
+
# Set of SchemaObjects for which objects have been already loaded,
|
|
274
|
+
# including SchemaObjects of optional objects not present in database.
|
|
275
|
+
self._loaded_classes = set()
|
|
276
|
+
|
|
277
|
+
def load(self, loader):
|
|
278
|
+
obj = self.load_object("_root", loader.content, self._schema.content, None)
|
|
279
|
+
for key, value in vars(obj).items():
|
|
280
|
+
# Copy loaded object attributes to self GenericDB object, except
|
|
281
|
+
# _schema where we want to keep Schema object instead of loaded
|
|
282
|
+
# SchemaObject.
|
|
283
|
+
if key != "_schema":
|
|
284
|
+
setattr(self, key, value)
|
|
285
|
+
|
|
286
|
+
def load_type(
|
|
287
|
+
self,
|
|
288
|
+
token,
|
|
289
|
+
literal,
|
|
290
|
+
schema_type: SchemaGenericValueType,
|
|
291
|
+
parent,
|
|
292
|
+
):
|
|
293
|
+
logger.debug("Loading type %s (%s)", token, schema_type)
|
|
294
|
+
if isinstance(schema_type, SchemaNativeType):
|
|
295
|
+
for native_type in [str, int, float, bool]:
|
|
296
|
+
if schema_type.native is native_type:
|
|
297
|
+
if type(literal) != native_type:
|
|
298
|
+
raise DBFormatError(
|
|
299
|
+
f"{token} {literal} is not a valid "
|
|
300
|
+
f"{native_type.__name__}"
|
|
301
|
+
)
|
|
302
|
+
return literal
|
|
303
|
+
elif isinstance(schema_type, SchemaDefinedType):
|
|
304
|
+
return self.load_defined_type(literal, schema_type)
|
|
305
|
+
elif isinstance(schema_type, SchemaExpandable):
|
|
306
|
+
if not isinstance(literal, str):
|
|
307
|
+
DBFormatError(
|
|
308
|
+
f"token {token} of {schema_type} is not a valid expandable str"
|
|
309
|
+
)
|
|
310
|
+
return self.load_expandable(literal)
|
|
311
|
+
elif isinstance(schema_type, SchemaRangeId):
|
|
312
|
+
if not isinstance(literal, int):
|
|
313
|
+
DBFormatError(
|
|
314
|
+
f"token {token} of {schema_type} is not a valid rangeid integer"
|
|
315
|
+
)
|
|
316
|
+
return self.load_rangeid(literal)
|
|
317
|
+
elif isinstance(schema_type, SchemaContainerList):
|
|
318
|
+
return self.load_list(token, literal, schema_type, parent)
|
|
319
|
+
elif isinstance(schema_type, SchemaObject):
|
|
320
|
+
return self.load_object(token, literal, schema_type, parent)
|
|
321
|
+
elif isinstance(schema_type, SchemaReference):
|
|
322
|
+
return self.load_reference(token, literal, schema_type)
|
|
323
|
+
elif isinstance(schema_type, SchemaBackReference):
|
|
324
|
+
raise DBFormatError(
|
|
325
|
+
f"Back reference {token} cannot be defined in database for "
|
|
326
|
+
f"object {schema_type}"
|
|
327
|
+
)
|
|
328
|
+
raise DBFormatError(
|
|
329
|
+
f"Unknow literal {literal} for token {token} for type {schema_type}"
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
def load_defined_type(self, literal, schema_type: SchemaDefinedType):
|
|
333
|
+
return schema_type.parse(literal)
|
|
334
|
+
|
|
335
|
+
def load_object(
|
|
336
|
+
self, token, literal, schema_object: SchemaObject, parent: SchemaObject
|
|
337
|
+
):
|
|
338
|
+
logger.debug("Loading object %s with %s (%s)", token, literal, schema_object)
|
|
339
|
+
# is it expandable?
|
|
340
|
+
if schema_object.expandable:
|
|
341
|
+
bases = [DBExpandableObject]
|
|
342
|
+
classname = f"{self._prefix}Expandable{schema_object.name}"
|
|
343
|
+
else:
|
|
344
|
+
bases = [DBObject]
|
|
345
|
+
classname = f"{self._prefix}{schema_object.name}"
|
|
346
|
+
# Add provided module base if defined
|
|
347
|
+
try:
|
|
348
|
+
# Insert bases module class in the beginning of the list to make
|
|
349
|
+
# sure methods from this classes are called over the methods from
|
|
350
|
+
# DBObject.
|
|
351
|
+
bases.insert(
|
|
352
|
+
0,
|
|
353
|
+
getattr(self._bases, f"{self._prefix}{schema_object.name}Base"),
|
|
354
|
+
)
|
|
355
|
+
except AttributeError:
|
|
356
|
+
pass
|
|
357
|
+
# instanciate the object with its dynamically defined class
|
|
358
|
+
obj = type(classname, tuple(bases), dict())(self, schema_object)
|
|
359
|
+
|
|
360
|
+
obj._parent = parent
|
|
361
|
+
|
|
362
|
+
# load object attributes
|
|
363
|
+
self.load_object_attributes(obj, literal, schema_object)
|
|
364
|
+
|
|
365
|
+
for prop in schema_object.properties:
|
|
366
|
+
# Check all required properties are properly defined in obj
|
|
367
|
+
# attributes.
|
|
368
|
+
if (
|
|
369
|
+
not isinstance(prop.type, SchemaBackReference)
|
|
370
|
+
and prop.required
|
|
371
|
+
and not hasattr(obj, prop.name)
|
|
372
|
+
):
|
|
373
|
+
raise DBFormatError(
|
|
374
|
+
f"Property {prop.name} is required in schema for object "
|
|
375
|
+
f"{schema_object}"
|
|
376
|
+
)
|
|
377
|
+
# Load back references
|
|
378
|
+
if isinstance(prop.type, SchemaBackReference):
|
|
379
|
+
setattr(obj, prop.name, self.load_back_reference(obj, prop.type))
|
|
380
|
+
# Assign default value to optional properties when provided in
|
|
381
|
+
# schema.
|
|
382
|
+
if not hasattr(obj, prop.name) and prop.default is not None:
|
|
383
|
+
value = self.load_type(prop.name, prop.default, prop.type, obj)
|
|
384
|
+
logger.debug(
|
|
385
|
+
"Assigning object %s property %s default value %s",
|
|
386
|
+
schema_object,
|
|
387
|
+
prop.name,
|
|
388
|
+
value,
|
|
389
|
+
)
|
|
390
|
+
setattr(obj, prop.name, value)
|
|
391
|
+
# If property is a key, check value uniqueness and set object _key
|
|
392
|
+
# attribute.
|
|
393
|
+
if prop.key:
|
|
394
|
+
value = getattr(obj, prop.name)
|
|
395
|
+
# If another object of the same type is already present in index
|
|
396
|
+
if schema_object.name in self._indexes:
|
|
397
|
+
# Check over all previously loaded objects of the same type
|
|
398
|
+
# if this key value has not been used yet.
|
|
399
|
+
for _obj in self._indexes[schema_object.name]:
|
|
400
|
+
if getattr(_obj, prop.name) == value:
|
|
401
|
+
raise DBFormatError(
|
|
402
|
+
f"Key value {value} of {schema_object} is not "
|
|
403
|
+
"unique."
|
|
404
|
+
)
|
|
405
|
+
setattr(obj, "_key", value)
|
|
406
|
+
|
|
407
|
+
# add object to db indexes
|
|
408
|
+
if schema_object.name not in self._indexes:
|
|
409
|
+
self._indexes[schema_object.name] = []
|
|
410
|
+
self._indexes[schema_object.name].append(obj)
|
|
411
|
+
self._loaded_classes |= schema_object.subobjs
|
|
412
|
+
logger.debug("Loaded classes: %s", [cls.name for cls in self._loaded_classes])
|
|
413
|
+
return obj
|
|
414
|
+
|
|
415
|
+
def load_object_attributes(self, obj, content, schema_object: SchemaObject):
|
|
416
|
+
|
|
417
|
+
_content = content.copy()
|
|
418
|
+
passes = 0
|
|
419
|
+
|
|
420
|
+
while len(_content):
|
|
421
|
+
# Pass number
|
|
422
|
+
passes += 1
|
|
423
|
+
# Flag to know if at least one attribute has been loaded in last
|
|
424
|
+
# pass.
|
|
425
|
+
processed = False
|
|
426
|
+
|
|
427
|
+
# Iterate over a copy of last pass remaining object attributes
|
|
428
|
+
for token, literal in _content.copy().items():
|
|
429
|
+
# Get the schema property corresponding to this token
|
|
430
|
+
token_property = self.token_object_property(token, schema_object)
|
|
431
|
+
# Check if this attribute can be loaded, considering its
|
|
432
|
+
# references and loaded objects.
|
|
433
|
+
if not self.loadable_attribute(token, token_property, passes, obj):
|
|
434
|
+
# The attribute cannot be loaded, jump to next attribute.
|
|
435
|
+
logger.debug(
|
|
436
|
+
"Skipping object %s property %s in pass %d",
|
|
437
|
+
schema_object.name,
|
|
438
|
+
token,
|
|
439
|
+
passes,
|
|
440
|
+
)
|
|
441
|
+
continue
|
|
442
|
+
else:
|
|
443
|
+
# The attribute can be loaded, set the flag and remove the
|
|
444
|
+
# attribute from the dict for the next pass.
|
|
445
|
+
processed = True
|
|
446
|
+
del _content[token]
|
|
447
|
+
|
|
448
|
+
attribute = self.load_type(token, literal, token_property.type, obj)
|
|
449
|
+
if token.endswith("[]"):
|
|
450
|
+
token = token[:-2]
|
|
451
|
+
# Check the bases module class does not already provide a
|
|
452
|
+
# conflicting attribute with the same name. In this case, it is
|
|
453
|
+
# renamed with LOADED_PREFIX.
|
|
454
|
+
if hasattr(obj, token):
|
|
455
|
+
logger.debug(
|
|
456
|
+
"Renaming object attribute %s.%s with prefix %s",
|
|
457
|
+
obj,
|
|
458
|
+
token,
|
|
459
|
+
obj.LOADED_PREFIX,
|
|
460
|
+
)
|
|
461
|
+
token = obj.LOADED_PREFIX + token
|
|
462
|
+
setattr(obj, token, attribute)
|
|
463
|
+
# Check if at least one attribute has been loaded during this pass,
|
|
464
|
+
# or raise DB format error exception.
|
|
465
|
+
if not processed:
|
|
466
|
+
raise DBFormatError(
|
|
467
|
+
f"Unable to load {token} {schema_object.name} after "
|
|
468
|
+
f"{passes} passes, probably because of circular references"
|
|
469
|
+
)
|
|
470
|
+
|
|
471
|
+
def token_object_property(
|
|
472
|
+
self, token, schema_object: SchemaObject
|
|
473
|
+
) -> SchemaProperty:
|
|
474
|
+
token_property = schema_object.prop(token)
|
|
475
|
+
if token_property is None:
|
|
476
|
+
raise DBFormatError(
|
|
477
|
+
f"Property {token} is not defined in schema for object "
|
|
478
|
+
f"{schema_object}"
|
|
479
|
+
)
|
|
480
|
+
return token_property
|
|
481
|
+
|
|
482
|
+
def loadable_attribute(self, token, token_property, passes, obj):
|
|
483
|
+
subtype = token_property.type
|
|
484
|
+
# If the property is a list, check the content of the list.
|
|
485
|
+
if isinstance(token_property.type, SchemaContainerList):
|
|
486
|
+
subtype = token_property.type.content
|
|
487
|
+
if isinstance(subtype, SchemaObject):
|
|
488
|
+
# If one object reference is not defined in inspected object
|
|
489
|
+
# sub-objects and not available in set of already loaded objects,
|
|
490
|
+
# the attribute cannot be loaded (yet).
|
|
491
|
+
for ref in subtype.refs - subtype.subobjs:
|
|
492
|
+
if ref not in self._loaded_classes:
|
|
493
|
+
logger.debug(
|
|
494
|
+
"Found undefined reference to %s in property %s in pass %d",
|
|
495
|
+
ref.name,
|
|
496
|
+
token,
|
|
497
|
+
passes,
|
|
498
|
+
)
|
|
499
|
+
return False
|
|
500
|
+
# Check back references with properties are all loaded. If one back
|
|
501
|
+
# reference with a property points to a property not already loaded
|
|
502
|
+
# on back referenced object, the attribute cannot be loaded (yet).
|
|
503
|
+
for prop in subtype.properties:
|
|
504
|
+
if (
|
|
505
|
+
isinstance(prop.type, SchemaBackReference)
|
|
506
|
+
and prop.type.prop is not None
|
|
507
|
+
):
|
|
508
|
+
# Loop to find the back referenced object
|
|
509
|
+
while obj._schema is not prop.type.obj and obj is not None:
|
|
510
|
+
obj = obj._parent
|
|
511
|
+
# Check attribute is already defined on referenced object
|
|
512
|
+
if not hasattr(obj, prop.type.prop):
|
|
513
|
+
logger.debug(
|
|
514
|
+
"Found undefined back reference to %s.%s in "
|
|
515
|
+
"property %s in pass %d",
|
|
516
|
+
ref.name,
|
|
517
|
+
prop.type.prop,
|
|
518
|
+
token,
|
|
519
|
+
passes,
|
|
520
|
+
)
|
|
521
|
+
return False
|
|
522
|
+
return True
|
|
523
|
+
|
|
524
|
+
def load_reference(self, token, literal, schema_type: SchemaReference):
|
|
525
|
+
all_objs = self.find_objects(schema_type.obj.name, expand=True)
|
|
526
|
+
if all_objs is None:
|
|
527
|
+
raise DBFormatError(
|
|
528
|
+
f"Unable to find {token} {literal} reference because objects "
|
|
529
|
+
f"{schema_type.obj.name} are missing in DB indexes"
|
|
530
|
+
)
|
|
531
|
+
|
|
532
|
+
logger.debug("Found objects for type %s: %s", schema_type.obj.name, all_objs)
|
|
533
|
+
for _obj in all_objs:
|
|
534
|
+
property_value = getattr(_obj, schema_type.prop)
|
|
535
|
+
if property_value == literal:
|
|
536
|
+
return _obj
|
|
537
|
+
raise DBFormatError(f"Unable to find {token} reference with value {literal}")
|
|
538
|
+
|
|
539
|
+
def load_back_reference(self, parent, schema_type: SchemaBackReference):
|
|
540
|
+
logger.debug("Loading back reference of %s/%s", parent, schema_type)
|
|
541
|
+
while parent._schema is not schema_type.obj and parent is not None:
|
|
542
|
+
logger.debug("Back reference %s != %s", parent._schema, schema_type.obj)
|
|
543
|
+
parent = parent._parent
|
|
544
|
+
|
|
545
|
+
# If the SchemaBackReference has a property, return the reference to
|
|
546
|
+
# this object property, or return the reference to the whole object.
|
|
547
|
+
if schema_type.prop is not None:
|
|
548
|
+
return getattr(parent, schema_type.prop)
|
|
549
|
+
else:
|
|
550
|
+
return parent
|
|
551
|
+
|
|
552
|
+
def load_list(self, token, literal, schema_object: SchemaContainerList, parent):
|
|
553
|
+
# Check the literal is a valid list
|
|
554
|
+
if not isinstance(literal, list):
|
|
555
|
+
# If it is not a list, it must be a dict and the contained object must have
|
|
556
|
+
# a key property. In this case, the literal is transformed into a list of
|
|
557
|
+
# dictionnaries augmented with object key property. Then it is loaded
|
|
558
|
+
# exactly the same way as if it was declared in database as a list with the
|
|
559
|
+
# key property.
|
|
560
|
+
if isinstance(literal, dict) and schema_object.content.has_key():
|
|
561
|
+
logger.debug(
|
|
562
|
+
"Transforming dictionnary into a list of %s with key property",
|
|
563
|
+
schema_object.content,
|
|
564
|
+
)
|
|
565
|
+
literal = [
|
|
566
|
+
{**{schema_object.content.key_property(): key}, **value}
|
|
567
|
+
for key, value in literal.items()
|
|
568
|
+
]
|
|
569
|
+
else:
|
|
570
|
+
raise DBFormatError(f"token {token} {schema_object} must be a list")
|
|
571
|
+
# Check if the contained object has a key property
|
|
572
|
+
has_key = False
|
|
573
|
+
if isinstance(schema_object.content, SchemaObject):
|
|
574
|
+
for prop in schema_object.content.properties:
|
|
575
|
+
if prop.key:
|
|
576
|
+
has_key = True
|
|
577
|
+
# If the contained object has a key, instanciate and fill a DBDict that
|
|
578
|
+
# can be subscripted with the key.
|
|
579
|
+
if has_key:
|
|
580
|
+
result = DBDict()
|
|
581
|
+
for item in literal:
|
|
582
|
+
content = self.load_type(token, item, schema_object.content, parent)
|
|
583
|
+
result[content._key] = content
|
|
584
|
+
return result
|
|
585
|
+
# Otherwise, instanciated a simple DBList.
|
|
586
|
+
else:
|
|
587
|
+
result = DBList()
|
|
588
|
+
for item in literal:
|
|
589
|
+
result.append(
|
|
590
|
+
self.load_type(token, item, schema_object.content, parent)
|
|
591
|
+
)
|
|
592
|
+
return result
|
|
593
|
+
|
|
594
|
+
def load_expandable(self, literal):
|
|
595
|
+
return type(f"{self._prefix}ExpandableRange", (DBObjectRange,), dict())(literal)
|
|
596
|
+
|
|
597
|
+
def load_rangeid(self, literal):
|
|
598
|
+
return type(f"{self._prefix}RangeId", (DBObjectRangeId,), dict())(literal)
|
|
599
|
+
|
|
600
|
+
def find_objects(self, object_type_name, expand=False):
|
|
601
|
+
if object_type_name not in self._indexes:
|
|
602
|
+
return None
|
|
603
|
+
result = []
|
|
604
|
+
for obj in self._indexes[object_type_name]:
|
|
605
|
+
if expand and isinstance(obj, DBExpandableObject):
|
|
606
|
+
result += obj.objects()
|
|
607
|
+
else:
|
|
608
|
+
result.append(obj)
|
|
609
|
+
return result
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Copyright (c) 2022-2023 Rackslab
|
|
2
|
+
#
|
|
3
|
+
# This file is part of RacksDB.
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
import re
|
|
8
|
+
|
|
9
|
+
from .errors import DBFormatError
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class SchemaDefinedType:
|
|
13
|
+
def __init__(self):
|
|
14
|
+
self.name = self.__class__.__module__
|
|
15
|
+
|
|
16
|
+
def __str__(self):
|
|
17
|
+
return f"~{self.name}"
|
|
18
|
+
|
|
19
|
+
def _match(self, value):
|
|
20
|
+
regex = re.compile(self.pattern)
|
|
21
|
+
match = regex.match(str(value))
|
|
22
|
+
if match is None:
|
|
23
|
+
raise DBFormatError(f"Unable to match {self} pattern with value {value}")
|
|
24
|
+
return match
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Copyright (c) 2023 Rackslab
|
|
2
|
+
#
|
|
3
|
+
# This file is part of RacksDB.
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
from .yaml import DBDumperYAML, SchemaDumperYAML
|
|
8
|
+
from .json import DBDumperJSON
|
|
9
|
+
from .console import DBDumperConsole
|
|
10
|
+
from ..errors import DBDumperError
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class DBDumperFactory:
|
|
14
|
+
FORMATS = {"yaml": DBDumperYAML, "json": DBDumperJSON, "console": DBDumperConsole}
|
|
15
|
+
|
|
16
|
+
@staticmethod
|
|
17
|
+
def get(_format):
|
|
18
|
+
if _format not in DBDumperFactory.FORMATS:
|
|
19
|
+
raise DBDumperError(f"Unsupported DB dump format {_format}")
|
|
20
|
+
return DBDumperFactory.FORMATS[_format]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class SchemaDumperFactory:
|
|
24
|
+
FORMATS = {"yaml": SchemaDumperYAML}
|
|
25
|
+
|
|
26
|
+
@staticmethod
|
|
27
|
+
def get(_format):
|
|
28
|
+
if _format not in SchemaDumperFactory.FORMATS:
|
|
29
|
+
raise DBDumperError(f"Unsupported schema dump format {_format}")
|
|
30
|
+
return SchemaDumperFactory.FORMATS[_format]
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Copyright (c) 2023 Rackslab
|
|
2
|
+
#
|
|
3
|
+
# This file is part of RacksDB.
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
from typing import Dict, Union
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MapperDumper:
|
|
11
|
+
def __init__(self, objects_map: Dict[str, Union[str, None]]):
|
|
12
|
+
self.objects_map = objects_map
|
|
13
|
+
|
|
14
|
+
def map(self, obj, prop, value):
|
|
15
|
+
found = False
|
|
16
|
+
if f"{obj.__class__.__name__}.{prop}" in self.objects_map.keys():
|
|
17
|
+
target = self.objects_map[f"{obj.__class__.__name__}.{prop}"]
|
|
18
|
+
found = True
|
|
19
|
+
if value.__class__.__name__ in self.objects_map.keys():
|
|
20
|
+
target = self.objects_map[value.__class__.__name__]
|
|
21
|
+
found = True
|
|
22
|
+
if found:
|
|
23
|
+
# If the object is mapped to None, discard the attribute and
|
|
24
|
+
# continue to the next one.
|
|
25
|
+
if target is None:
|
|
26
|
+
return None
|
|
27
|
+
# Else, map the object to one of its attribute.
|
|
28
|
+
return getattr(value, target)
|
|
29
|
+
# Mapping not found, return value unmodified.
|
|
30
|
+
return value
|