attribute definitions can be serialized and deserialized

This commit is contained in:
Jakub Trllo 2022-10-07 16:34:50 +02:00
parent 06e1cf0b0f
commit bc39b99270

View file

@ -90,6 +90,8 @@ class AbtractAttrDef:
next to value input or ahead.
"""
type_attributes = []
is_value_def = True
def __init__(
@ -135,6 +137,35 @@ class AbtractAttrDef:
pass
def serialize(self):
"""Serialize object to data so it's possible to recreate it.
Returns:
Dict[str, Any]: Serialized object that can be passed to
'deserialize' method.
"""
data = {
"type": self.type,
"key": self.key,
"label": self.label,
"tooltip": self.tooltip,
"default": self.default,
"is_label_horizontal": self.is_label_horizontal
}
for attr in self.type_attributes:
data[attr] = getattr(self, attr)
return data
@classmethod
def deserialize(cls, data):
"""Recreate object from data.
Data can be received using 'serialize' method.
"""
return cls(**data)
# -----------------------------------------
# UI attribute definitoins won't hold value
@ -196,6 +227,12 @@ class NumberDef(AbtractAttrDef):
"""
type = "number"
type_attributes = [
"minimum",
"maximum",
"decimals"
]
def __init__(
self, key, minimum=None, maximum=None, decimals=None, default=None,
**kwargs
@ -267,6 +304,12 @@ class TextDef(AbtractAttrDef):
default(str, None): Default value. Empty string used when not defined.
"""
type = "text"
type_attributes = [
"multiline",
"placeholder",
]
def __init__(
self, key, multiline=None, regex=None, placeholder=None, default=None,
**kwargs
@ -305,6 +348,11 @@ class TextDef(AbtractAttrDef):
return value
return self.default
def serialize(self):
data = super(TextDef, self).serialize()
data["regex"] = self.regex.pattern
return data
class EnumDef(AbtractAttrDef):
"""Enumeration of single item from items.
@ -352,6 +400,11 @@ class EnumDef(AbtractAttrDef):
return value
return self.default
def serialize(self):
data = super(TextDef, self).serialize()
data["items"] = list(self.items)
return data
class BoolDef(AbtractAttrDef):
"""Boolean representation.
@ -605,6 +658,14 @@ class FileDef(AbtractAttrDef):
"""
type = "path"
type_attributes = [
"single_item",
"folders",
"extensions",
"allow_sequences",
"extensions_label",
]
def __init__(
self, key, single_item=True, folders=None, extensions=None,
allow_sequences=True, extensions_label=None, default=None, **kwargs