Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/core/hardware/rtschemamixin.py: 0%
34 statements
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3from marshmallow import fields, validate
5from daiquiri.core.hardware.abstract import HardwareProperty
6from daiquiri.core.hardware.abstract.generic import (
7 GenericPropertiesSchema,
8 GenericCallablesSchema,
9)
12class RTSchemaMixin:
13 """RunTime Schema Mixin
15 Allows the validation schema for the object to be generated at
16 run time from the hardware.yml config
17 """
19 def __init__(self, *args, **kwargs):
20 super().__init__(*args, **kwargs)
22 attrs, ui = self._map_to_schema(kwargs.get("attributes", {}))
24 class Meta:
25 uischema = ui
27 attrs["Meta"] = Meta
29 properties_schema = type(
30 self.id() + "PropertiesSchema", (GenericPropertiesSchema,), attrs
31 )
32 self._properties = properties_schema()
34 callables_schema = type(
35 self.id() + "CallablesSchema", (GenericCallablesSchema,), attrs
36 )
37 self._callables = callables_schema()
39 def schema_name(self):
40 return self.id()
42 def _map_to_schema(self, attributes):
43 attr_map = {
44 "int": fields.Int,
45 "float": fields.Float,
46 "bool": fields.Bool,
47 "str": fields.Str,
48 }
50 attrs = {}
51 ui = {}
52 for a in attributes:
53 if a.get("type") in attr_map:
54 field = attr_map[a["type"]]
55 val = None
56 if a.get("min") is not None and a.get("max") is not None:
57 val = (validate.Range(min=a["min"], max=a["max"]),)
59 extra_kws = {}
60 if a.get("step"):
61 extra_kws["multipleOf"] = a["step"]
63 attrs[a["id"]] = field(
64 validate=val, metadata={"title": a.get("name", None)}, **extra_kws
65 )
67 if a.get("ui_schema"):
68 ui[a.get("id")] = a.get("ui_schema")
70 self._property_map[a["id"]] = HardwareProperty(a["id"])
72 return attrs, ui