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.
Files changed (42) hide show
  1. RacksDB-0.2.0.dist-info/LICENSE +674 -0
  2. RacksDB-0.2.0.dist-info/METADATA +126 -0
  3. RacksDB-0.2.0.dist-info/RECORD +42 -0
  4. RacksDB-0.2.0.dist-info/WHEEL +5 -0
  5. RacksDB-0.2.0.dist-info/entry_points.txt +3 -0
  6. RacksDB-0.2.0.dist-info/top_level.txt +1 -0
  7. racksdb/__init__.py +74 -0
  8. racksdb/bases.py +117 -0
  9. racksdb/drawers/__init__.py +10 -0
  10. racksdb/drawers/base.py +42 -0
  11. racksdb/drawers/infrastructure.py +253 -0
  12. racksdb/drawers/room.py +160 -0
  13. racksdb/dtypes/__init__.py +0 -0
  14. racksdb/dtypes/angle.py +21 -0
  15. racksdb/dtypes/bytes.py +31 -0
  16. racksdb/dtypes/dimension.py +23 -0
  17. racksdb/dtypes/netif_type.py +17 -0
  18. racksdb/dtypes/rack_height.py +17 -0
  19. racksdb/dtypes/rack_width.py +26 -0
  20. racksdb/dtypes/storage_type.py +17 -0
  21. racksdb/errors.py +9 -0
  22. racksdb/exec.py +218 -0
  23. racksdb/generic/__init__.py +0 -0
  24. racksdb/generic/db.py +609 -0
  25. racksdb/generic/definedtype.py +24 -0
  26. racksdb/generic/dumpers/__init__.py +30 -0
  27. racksdb/generic/dumpers/_common.py +30 -0
  28. racksdb/generic/dumpers/console.py +31 -0
  29. racksdb/generic/dumpers/json.py +108 -0
  30. racksdb/generic/dumpers/yaml.py +138 -0
  31. racksdb/generic/errors.py +21 -0
  32. racksdb/generic/openapi.py +199 -0
  33. racksdb/generic/schema.py +367 -0
  34. racksdb/generic/views.py +139 -0
  35. racksdb/tests/__init__.py +0 -0
  36. racksdb/tests/test_schema.py +211 -0
  37. racksdb/tests/test_schema_defined_type_loader.py +23 -0
  38. racksdb/tests/tests_db_load.py +83 -0
  39. racksdb/version.py +11 -0
  40. racksdb/views.py +136 -0
  41. racksdb/web/__init__.py +0 -0
  42. racksdb/web/app.py +195 -0
@@ -0,0 +1,367 @@
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
+ import importlib
9
+ import pkgutil
10
+ import yaml
11
+ import logging
12
+
13
+ from .errors import DBSchemaError
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+
18
+ class SchemaDefinedTypeLoader:
19
+ def __init__(self, path):
20
+ self.path = path
21
+
22
+ @property
23
+ def content(self):
24
+ results = {}
25
+ base_module = importlib.import_module(self.path)
26
+ logger.debug("Searching module in %s", base_module.__path__)
27
+ for importer, modname, _ in pkgutil.iter_modules(base_module.__path__):
28
+ logger.debug("Loading module %s/%s", modname, importer)
29
+ module = importlib.import_module(f"{self.path}.{modname}")
30
+ class_suffix = modname.replace("_", " ").title().replace(" ", "")
31
+ class_name = f"SchemaDefinedType{class_suffix}"
32
+ logger.debug("Loading class %s", class_name)
33
+ results[modname] = getattr(module, class_name)()
34
+ return results
35
+
36
+
37
+ class SchemaFileLoader:
38
+ def __init__(self, path, extensions):
39
+ self.path = path
40
+ self.extensions = extensions
41
+
42
+ @property
43
+ def content(self):
44
+ if not self.path.exists():
45
+ raise DBSchemaError(f"Schema path {self.path} does not exist")
46
+ with open(self.path) as fh:
47
+ try:
48
+ result = yaml.safe_load(fh)
49
+ except yaml.composer.ComposerError as err:
50
+ raise DBSchemaError(err)
51
+ # load schema extensions
52
+ result = self.load_extensions(result)
53
+ return result
54
+
55
+ def load_extensions(self, result):
56
+ if not self.extensions.exists():
57
+ logger.debug(
58
+ "Schema extensions file %s not found, skipping extensions",
59
+ self.extensions,
60
+ )
61
+ return result
62
+
63
+ logger.debug("Loading schema extensions file %s", self.extensions)
64
+ with open(self.extensions) as fh:
65
+ try:
66
+ extensions = yaml.safe_load(fh)
67
+ except yaml.composer.ComposerError as err:
68
+ raise DBSchemaError(err)
69
+ if "_content" in extensions:
70
+ logger.debug("Updating schema with additional content found in extension")
71
+ result["_content"]["properties"].update(extensions["_content"]["properties"])
72
+ if "_objects" in extensions:
73
+ for obj, definition in extensions["_objects"].items():
74
+ if obj in result["_objects"]:
75
+ logger.debug(
76
+ "Updating object class %s with properties found in extension",
77
+ obj,
78
+ )
79
+ result["_objects"][obj]["properties"].update(definition["properties"])
80
+ else:
81
+ logger.debug("Additional object class %s found in extension", obj)
82
+ result["_objects"][obj] = definition
83
+ return result
84
+
85
+
86
+ class SchemaGenericValueType:
87
+ pass
88
+
89
+
90
+ class SchemaNativeType(SchemaGenericValueType):
91
+ def __init__(self, native):
92
+ self.native = native
93
+
94
+ def __str__(self):
95
+ if self.native is str:
96
+ return "str"
97
+ elif self.native is int:
98
+ return "int"
99
+ elif self.native is float:
100
+ return "float"
101
+ elif self.native is bool:
102
+ return "bool"
103
+
104
+
105
+ class SchemaObject(SchemaGenericValueType):
106
+ def __init__(self, name, description):
107
+ self.name = name
108
+ self.description = description
109
+ self.properties = [] # list of SchemaProperty
110
+ self.expandable = False
111
+ # Set of SchemaObject attached to this object properties, recursively.
112
+ self.subobjs = set()
113
+ # Set of references in this object and sub-objects properties.
114
+ self.refs = set()
115
+
116
+ def __str__(self):
117
+ if self.expandable:
118
+ return f"Schema{self.name}+"
119
+ else:
120
+ return f"Schema{self.name}"
121
+
122
+ def prop(self, name):
123
+ for _prop in self.properties:
124
+ if _prop.name == name:
125
+ return _prop
126
+ return None
127
+
128
+ def has_key(self):
129
+ """Return True if the object has a key property or False otherwise."""
130
+ for prop in self.properties:
131
+ if prop.key:
132
+ return True
133
+ return False
134
+
135
+ def key_property(self):
136
+ """Return the name of object's key property. Raise DBSchemaError if key property
137
+ is not found."""
138
+ for prop in self.properties:
139
+ if prop.key:
140
+ return prop.name
141
+ raise DBSchemaError(f"Unable to find key property for object {self.name}")
142
+
143
+
144
+ class SchemaContainerList(SchemaGenericValueType):
145
+ def __init__(self, content):
146
+ self.content = content
147
+
148
+ def __str__(self):
149
+ return f"list[{self.content}]"
150
+
151
+
152
+ class SchemaExpandable(SchemaGenericValueType):
153
+ def __str__(self):
154
+ return "expandable"
155
+
156
+
157
+ class SchemaRangeId(SchemaGenericValueType):
158
+ def __str__(self):
159
+ return "rangeid"
160
+
161
+
162
+ class SchemaReference(SchemaGenericValueType):
163
+ def __init__(self, obj, prop):
164
+ self.obj = obj
165
+ self.prop = prop
166
+
167
+ def __str__(self):
168
+ return f"${self.obj}.{self.prop}"
169
+
170
+
171
+ class SchemaBackReference(SchemaGenericValueType):
172
+ def __init__(self, obj, prop):
173
+ self.obj = obj
174
+ self.prop = prop
175
+
176
+ def __str__(self):
177
+ result = f"^{self.obj}"
178
+ if self.prop is not None:
179
+ result += f".{self.prop}"
180
+ return result
181
+
182
+
183
+ class SchemaProperty:
184
+ def __init__(self, name, required, key, default, value_type, description, example):
185
+ self.name = name
186
+ self.required = required
187
+ self.key = key
188
+ self.default = default
189
+ self.type = value_type
190
+ self.description = description
191
+ self.example = example
192
+
193
+ def __str__(self):
194
+ if self.required:
195
+ result = "required "
196
+ else:
197
+ result = "optional "
198
+ if self.key:
199
+ result += "key "
200
+ result += str(self.type)
201
+ if self.default is not None:
202
+ result += f" ({self.default})"
203
+ return result
204
+
205
+
206
+ class Schema:
207
+
208
+ pattern_type_obj = re.compile(r":(\w+)")
209
+ pattern_type_defined = re.compile(r"~(\w+)")
210
+ pattern_type_ref = re.compile(r"\$(\w+)\.(\w+)")
211
+ pattern_type_backref = re.compile(r"\^(\w+)(\.(\w+))?")
212
+ pattern_type_list = re.compile(r"list\[(.+)\]")
213
+
214
+ def __init__(self, schema_loader, types_loader):
215
+ self._schema = schema_loader.content
216
+
217
+ try:
218
+ self.version = self._schema["_version"]
219
+ except KeyError:
220
+ raise DBSchemaError("Version must be defined in schema")
221
+ self.types = types_loader.content
222
+
223
+ self.objects = {}
224
+
225
+ try:
226
+ self.content = self.parse_obj("_content", self._schema["_content"])
227
+ except KeyError:
228
+ raise DBSchemaError("Content must be defined in schema")
229
+
230
+ def prop_spec(self, name, spec):
231
+ # check optional
232
+ required = True
233
+ if "optional" in spec and spec["optional"] is True:
234
+ required = False
235
+
236
+ # check key
237
+ key = False
238
+ if "key" in spec and spec["key"] is True:
239
+ key = True
240
+
241
+ # check default
242
+ default = None
243
+ if "default" in spec:
244
+ required = False
245
+ default = spec["default"]
246
+ return SchemaProperty(
247
+ name,
248
+ required,
249
+ key,
250
+ default,
251
+ self.value_type(spec["type"]),
252
+ spec.get("description"),
253
+ spec.get("example"),
254
+ )
255
+
256
+ def value_type(self, spec):
257
+ # parse native types
258
+ for native_type in (str, int, float, bool):
259
+ if spec == native_type.__name__:
260
+ return SchemaNativeType(native_type)
261
+ if spec == "expandable":
262
+ return SchemaExpandable()
263
+ if spec == "rangeid":
264
+ return SchemaRangeId()
265
+ # list
266
+ match = self.pattern_type_list.match(spec)
267
+ if match is not None:
268
+ content = self.value_type(match.group(1))
269
+ return SchemaContainerList(content)
270
+ # obj
271
+ match = self.pattern_type_obj.match(spec)
272
+ if match is not None:
273
+ return self.find_obj(match.group(1))
274
+ # defined
275
+ match = self.pattern_type_defined.match(spec)
276
+ if match is not None:
277
+ return self.find_defined_type(match.group(1))
278
+ # ref
279
+ match = self.pattern_type_ref.match(spec)
280
+ if match is not None:
281
+ return self.obj_reference(spec, match.group(1), match.group(2))
282
+ # backref
283
+ match = self.pattern_type_backref.match(spec)
284
+ if match is not None:
285
+ return self.obj_back_reference(match.group(1), match.group(3))
286
+ raise DBSchemaError(f"Unable to parse value type '{spec}'")
287
+
288
+ def find_obj(self, object_id):
289
+ if object_id in self.objects:
290
+ return self.objects[object_id]
291
+ if "_objects" not in self._schema or object_id not in self._schema["_objects"]:
292
+ raise DBSchemaError(f"Definition of object {object_id} not found in schema")
293
+ obj = self.parse_obj(object_id, self._schema["_objects"][object_id])
294
+ return obj
295
+
296
+ def parse_obj(self, object_id, objdef):
297
+ logger.debug("Loading class of %s", object_id)
298
+
299
+ obj = SchemaObject(object_id, objdef.get("description"))
300
+ # The new SchemaObject must be added soon in objects hash to avoid
301
+ # recursion loop in the following calls to prop_keys(), calling
302
+ # value_type() → find_obj() → parse_obj() with back references.
303
+ # There is an exception for _content fake root object as it is already
304
+ # hold by Schema.content attribute.
305
+ if object_id != "_content":
306
+ self.objects[object_id] = obj
307
+
308
+ has_key = False # flag to check key property uniquess
309
+
310
+ for key, spec in objdef["properties"].items():
311
+ prop = self.prop_spec(key, spec)
312
+ if isinstance(prop.type, SchemaObject) and prop.type.expandable:
313
+ raise DBSchemaError(
314
+ f"Expandable object {prop.type} must be in a list, it "
315
+ f"cannot be member of object such as {object_id}"
316
+ )
317
+ if isinstance(prop.type, SchemaExpandable):
318
+ # check expandable uniqueness
319
+ if obj.expandable:
320
+ raise DBSchemaError(
321
+ f"Expandable object {object_id} cannot contain more "
322
+ "than one expandable property"
323
+ )
324
+ obj.expandable = True
325
+ if prop.key:
326
+ # check key uniqueness among all properties
327
+ if has_key:
328
+ raise DBSchemaError(
329
+ f"Object {object_id} cannot contain more than one key"
330
+ )
331
+ has_key = True
332
+
333
+ # Define refs and subobjs recursively
334
+ if isinstance(prop.type, SchemaReference):
335
+ obj.refs |= {prop.type.obj}
336
+ subtype = prop.type
337
+ if isinstance(prop.type, SchemaContainerList):
338
+ subtype = prop.type.content
339
+ if isinstance(subtype, SchemaObject):
340
+ obj.refs |= subtype.refs
341
+ obj.subobjs |= {subtype} | subtype.subobjs
342
+ obj.properties.append(prop)
343
+ logger.debug(
344
+ "Class %s refs: %s subobjs: %s",
345
+ object_id,
346
+ [obj.name for obj in obj.refs],
347
+ [obj.name for obj in obj.subobjs],
348
+ )
349
+ return obj
350
+
351
+ def find_defined_type(self, defined):
352
+ try:
353
+ return self.types[defined]
354
+ except KeyError:
355
+ raise DBSchemaError(f"Definition of defined type {defined} not found")
356
+
357
+ def obj_reference(self, spec, object_id, object_prop):
358
+ obj = self.find_obj(object_id)
359
+ # verify property is defined for object
360
+ if obj.prop(object_prop) is None:
361
+ raise DBSchemaError(f"Reference {spec} to undefined {obj} object property")
362
+ return SchemaReference(obj, object_prop)
363
+
364
+ def obj_back_reference(self, object_id, object_prop):
365
+ logger.debug("Loading back reference to %s", object_id)
366
+ obj = self.find_obj(object_id)
367
+ return SchemaBackReference(obj, object_prop)
@@ -0,0 +1,139 @@
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 List, Dict, Union
8
+ from itertools import chain
9
+
10
+ from .errors import DBViewError
11
+
12
+
13
+ class DBActionParameter:
14
+ def __init__(
15
+ self,
16
+ name,
17
+ description,
18
+ short=None,
19
+ nargs=None,
20
+ positional=False,
21
+ required=False,
22
+ choices=None,
23
+ default=None,
24
+ ):
25
+ self.name = name
26
+ self.description = description
27
+ self.short = short
28
+ self.nargs = nargs
29
+ self.positional = positional
30
+ self.required = required
31
+ self.choices = choices
32
+ self.default = default
33
+
34
+
35
+ class DBActionResponse:
36
+ def __init__(self, mimetype, binary=False, object_name=None):
37
+ self.mimetype = mimetype
38
+ self.binary = binary
39
+ self.object = object_name
40
+
41
+
42
+ class DBAction:
43
+ def __init__(self, name, path, description, parameters=[], responses=[]):
44
+ self.name = name
45
+ self.path = path
46
+ self.description = description
47
+ self.parameters = parameters
48
+ self.responses = responses
49
+
50
+ def inpath(self, parameter: DBActionParameter) -> bool:
51
+ """Return True if a DBActionParameter is in DBAction path"""
52
+ return f"<{parameter.name}>" in self.path
53
+
54
+
55
+ class DBViewParameter(DBActionParameter):
56
+ def __init__(
57
+ self, name, description, short=None, nargs=None, choices=None, default=None
58
+ ):
59
+ super().__init__(
60
+ name,
61
+ description,
62
+ short=short,
63
+ nargs=nargs,
64
+ choices=choices,
65
+ default=default,
66
+ )
67
+
68
+
69
+ class DBViewFilter(DBActionParameter):
70
+ def __init__(
71
+ self, name: str, description: str, nargs: Union[str, int, None] = None
72
+ ):
73
+ super().__init__(name, description, nargs=nargs)
74
+
75
+
76
+ class DBView:
77
+ def __init__(
78
+ self,
79
+ content: str,
80
+ objects_name: str,
81
+ description: str,
82
+ filters: List[DBViewFilter],
83
+ objects_map: Dict[str, Union[None, str]],
84
+ ):
85
+ self.content = content
86
+ self.objects_name = objects_name
87
+ self.description = description
88
+ self.filters = filters
89
+ self.objects_map = objects_map
90
+
91
+
92
+ class DBViewSet:
93
+ def __iter__(self):
94
+ for view in self.VIEWS:
95
+ yield view
96
+
97
+ def views_actions(self):
98
+ """Generate the list of generic DBActions (with their DBActionParameters and
99
+ DBActionResponses) corresponding to the list of DBViews attached this
100
+ DBViewSet."""
101
+ actions = []
102
+ for view in self:
103
+ # Merge view filters and generic parameters to form the full list of
104
+ # parameters.
105
+ parameters = [
106
+ parameter for parameter in chain(view.filters, self.parameters())
107
+ ]
108
+ if not len(parameters):
109
+ parameters = None
110
+ # List of responses
111
+ responses = []
112
+ for mimetype in ["application/json", "application/x-yaml"]:
113
+ responses.append(
114
+ DBActionResponse(mimetype, object_name=view.objects_name)
115
+ )
116
+ actions.append(
117
+ DBAction(
118
+ name=view.content,
119
+ path=f"/{view.content}",
120
+ description=view.description,
121
+ parameters=parameters,
122
+ responses=responses,
123
+ )
124
+ )
125
+ return actions
126
+
127
+ def parameters(self):
128
+ for parameter in self.PARAMETERS:
129
+ yield parameter
130
+
131
+ def actions(self):
132
+ for action in self.ACTIONS:
133
+ yield action
134
+
135
+ def __getitem__(self, key):
136
+ for view in self.VIEWS:
137
+ if view.content == key:
138
+ return view
139
+ raise DBViewError(f"Unable to find view for '{key}' content")
File without changes