Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tutorius
diff options
context:
space:
mode:
Diffstat (limited to 'tutorius')
-rw-r--r--tutorius/constraints.py90
-rw-r--r--tutorius/properties.py26
-rw-r--r--tutorius/vault.py1
3 files changed, 115 insertions, 2 deletions
diff --git a/tutorius/constraints.py b/tutorius/constraints.py
index cd71167..9ac1ca4 100644
--- a/tutorius/constraints.py
+++ b/tutorius/constraints.py
@@ -47,6 +47,96 @@ class Constraint():
"""
raise NotImplementedError("Unable to validate a base Constraint")
+class TypeConstraintError(ConstraintException):
+ pass
+
+class TypeConstraint(Constraint):
+ """
+ Base class for ensuring that all the values have a specific Python type.
+ """
+ def __init__(self, expected_type):
+ """
+ Creates a new type constraint to restrict accepted values to a certain type.
+ @param expected_type A string describing the name of the type to enforce
+ """
+ self._expected_type = expected_type
+
+ def validate(self, value):
+ if self._expected_type == type(value).__name__:
+ return value
+ raise TypeConstraintError("Wrong type for value %s, got type %s; expected %s" % \
+ (str(value),
+ type(value).__name__,
+ self._expected_type))
+
+class IntTypeConstraint(TypeConstraint):
+ """
+ Forces the value to be a Python int.
+ """
+ def __init__(self):
+ TypeConstraint.__init__(self, 'int')
+
+class FloatTypeConstraint(TypeConstraint):
+ """
+ Forces the value to be a Python float.
+ """
+ def __init__(self):
+ TypeConstraint.__init__(self, 'float')
+
+class StringTypeConstraint(TypeConstraint):
+ """
+ Forces the value to be a Python string.
+ """
+ def __init__(self):
+ TypeConstraint.__init__(self, 'str')
+
+class TupleTypeConstraint(TypeConstraint):
+ """
+ Forces the value to be a Python tuple.
+ """
+ def __init__(self):
+ TypeConstraint.__init__(self, 'tuple')
+
+class ListTypeConstraint(TypeConstraint):
+ """
+ Forces the value to be a Python list.
+ """
+ def __init__(self):
+ TypeConstraint.__init__(self, 'list')
+
+class BoolTypeConstraint(TypeConstraint):
+ def __init__(self):
+ TypeConstraint.__init__(self, 'bool')
+
+class MultiTypeConstraint(Constraint):
+ """
+ Base class for ensuring that all the values have a specific Python type.
+ """
+ def __init__(self, expected_types_list):
+ """
+ Creates a new type constraint to restrict accepted values to a certain type.
+ @param expected_types_list A list of strings describing the possible names
+ of the types to enforce.
+ """
+ self._expected_types_list = expected_types_list
+
+ def validate(self, value):
+ if type(value).__name__ in self._expected_types_list:
+ return value
+ raise TypeConstraintError("Wrong type for value %s, got type %s; expected one in %s" % \
+ (str(value),
+ type(value).__name__,
+ str(self._expected_types_list)))
+
+class ArrayTypeConstraint(MultiTypeConstraint):
+ """ Forces the value to either be a list or a tuple."""
+ def __init__(self):
+ MultiTypeConstraint.__init__(self, ['list', 'tuple'])
+
+class SequenceTypeConstraint(MultiTypeConstraint):
+ def __init__(self):
+ MultiTypeConstraint.__init__(self, ['list', 'tuple', 'str'])
+
class ValueConstraint(Constraint):
"""
A value constraint contains a _limit member that can be used in a children
diff --git a/tutorius/properties.py b/tutorius/properties.py
index 07b1645..4a80e9c 100644
--- a/tutorius/properties.py
+++ b/tutorius/properties.py
@@ -26,7 +26,9 @@ from .constraints import Constraint, \
UpperLimitConstraint, LowerLimitConstraint, \
MaxSizeConstraint, MinSizeConstraint, \
ColorConstraint, FileConstraint, BooleanConstraint, EnumConstraint, \
- ResourceConstraint
+ ResourceConstraint, IntTypeConstraint, FloatTypeConstraint, \
+ StringTypeConstraint, ArrayTypeConstraint, BoolTypeConstraint, \
+ SequenceTypeConstraint
from .propwidgets import PropWidget, \
StringPropWidget, \
@@ -227,6 +229,7 @@ class TIntProperty(TutoriusProperty):
self.type = "int"
self.upper_limit = UpperLimitConstraint(upper_limit)
self.lower_limit = LowerLimitConstraint(lower_limit)
+ self.int_type = IntTypeConstraint()
self.default = self.validate(value)
@@ -242,6 +245,7 @@ class TFloatProperty(TutoriusProperty):
self.upper_limit = UpperLimitConstraint(upper_limit)
self.lower_limit = LowerLimitConstraint(lower_limit)
+ self.float_type = FloatTypeConstraint()
self.default = self.validate(value)
@@ -254,6 +258,7 @@ class TStringProperty(TutoriusProperty):
TutoriusProperty.__init__(self)
self.type = "string"
self.size_limit = MaxSizeConstraint(size_limit)
+ self.string_type = StringTypeConstraint()
self.default = self.validate(value)
@@ -268,6 +273,7 @@ class TArrayProperty(TutoriusProperty):
self.type = "array"
self.max_size_limit = MaxSizeConstraint(max_size_limit)
self.min_size_limit = MinSizeConstraint(min_size_limit)
+ self.array_type = ArrayTypeConstraint()
self.default = tuple(self.validate(value))
#Make this thing hashable
@@ -283,6 +289,22 @@ class TArrayProperty(TutoriusProperty):
value=self.value,
)
+class TSequenceProperty(TutoriusProperty):
+ """
+ Represents a data type that can be accessed with indices (via []). Those
+ are mainly for structures that only expect a list of elements that may
+ also be specified using a string. E.g. the strokes in a text type filter.
+ Since it is more convenient to specify a list of keystrokes as a string
+ rather than a list of characters, a sequence is the best option.
+ """
+ def __init__(self, value, min_size_limit=None, max_size_limit=None):
+ TutoriusProperty.__init__(self)
+ self.type = "sequence"
+ self.max_size_limit = MaxSizeConstraint(max_size_limit)
+ self.min_size_limit = MinSizeConstraint(min_size_limit)
+ self.sequence_type = SequenceTypeConstraint()
+ self.default = tuple(self.validate(value))
+
class TColorProperty(TutoriusProperty):
"""
Represents a RGB color with 3 8-bit integer values.
@@ -293,6 +315,7 @@ class TColorProperty(TutoriusProperty):
TutoriusProperty.__init__(self)
self.type = "color"
+ self.array_type = ArrayTypeConstraint()
self.color_constraint = ColorConstraint()
self._red = red or 0
@@ -384,6 +407,7 @@ class TBooleanProperty(TutoriusProperty):
self.type = "boolean"
+ self.bool_type = BoolTypeConstraint()
self.boolean_constraint = BooleanConstraint()
self.default = self.validate(value)
diff --git a/tutorius/vault.py b/tutorius/vault.py
index 1c1e33c..2b9c5b9 100644
--- a/tutorius/vault.py
+++ b/tutorius/vault.py
@@ -663,7 +663,6 @@ class XMLSerializer(Serializer):
if getattr(type(comp), propname).type == "addonlist":
compNode.appendChild(cls._create_addonlist_component_node(propname, propval, doc))
elif getattr(type(comp), propname).type == "addon":
- #import rpdb2; rpdb2.start_embedded_debugger('pass')
compNode.appendChild(cls._create_addon_component_node(propname, propval, doc))
else:
# repr instead of str, as we want to be able to eval() it into a