aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/_util.py
diff options
context:
space:
mode:
Diffstat (limited to 'sgdfi/_util.py')
-rwxr-xr-xsgdfi/_util.py50
1 files changed, 48 insertions, 2 deletions
diff --git a/sgdfi/_util.py b/sgdfi/_util.py
index a119c83..088b1da 100755
--- a/sgdfi/_util.py
+++ b/sgdfi/_util.py
@@ -4,7 +4,7 @@
# This file is part of the sgdfi project, which is MIT-licensed.
#******************************************************************************
""" Base objects for the intranet objects representations, never used
- directly. """
+ by the end user of the module. """
from inspect import getargspec as _getargspec
from copy import deepcopy as _deepcopy
@@ -18,7 +18,7 @@ from pytz import timezone as _timezone
__all__ = ["IID", "Enum",
"Property", "IIDProperty", "DateProperty", "BoolProperty", "EnumProperty",
- "ArrayProperty", "TextProperty", "Base"]
+ "ArrayProperty", "TextProperty", "ObjectProperty", "ValueProperty", "Base"]
# ---
# Attribute helpers.
@@ -319,6 +319,52 @@ class ArrayProperty(Property):
def delete(self):
self.__array.empty()
+class ObjectProperty(Property):
+ """ An object property among specified types. """
+
+ def init(self, types = (), default = None):
+ self.__default = default
+ self.__types = types
+ self.__value = self.__default
+
+ def get(self):
+ return self.__value
+
+ def set(self, value):
+ if value is None:
+ self.__value = self.__default
+ return
+
+ if not any(isinstance(value, cls) for cls in self.__types):
+ raise ValueError("Shall be an instance of one of: " \
+ f"{repr(self.__types)}")
+
+ self.__value = value
+
+ def delete(self):
+ self.__value = self.__default
+
+class ValueProperty(Property):
+ """ A value property.
+ Basically uses a class to store its value. """
+
+ def init(self, cls, default = None):
+ self.__cls = cls
+ self.__data = default
+
+ def get(self):
+ return self.__data
+
+ def set(self, value):
+ if value is None:
+ self.__data = _deepcopy(default)
+ return
+
+ self.__data = self.__cls(value)
+
+ def delete(self):
+ self.__data = _deepcopy(default)
+
class TextProperty(Property):
""" A text property.
`lines` represents the maximum number of allowed lines.