Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tutorius
diff options
context:
space:
mode:
authormike <michael.jmontcalm@gmail.com>2009-12-05 19:22:28 (GMT)
committer mike <michael.jmontcalm@gmail.com>2009-12-05 19:22:28 (GMT)
commitf00c6abdf5804a82eb6d94f427de5d2609a2314c (patch)
tree8ec6697c413fd5af59665bdeabefc1f561609525 /tutorius
parent9500e288397de5710151f32ced2e9a2903766a2e (diff)
BoyScout2.0 : Adding type objects support inside the TypeConstraint
Diffstat (limited to 'tutorius')
-rw-r--r--tutorius/constraints.py29
-rw-r--r--tutorius/properties.py35
2 files changed, 43 insertions, 21 deletions
diff --git a/tutorius/constraints.py b/tutorius/constraints.py
index 9ac1ca4..ffb646d 100644
--- a/tutorius/constraints.py
+++ b/tutorius/constraints.py
@@ -57,13 +57,20 @@ class TypeConstraint(Constraint):
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
+ @param expected_type A string describing the name of the type to
+ enforce, or a full-blown type object. If a type
+ object is used, all objects that inherit from this
+ type will pass.
"""
self._expected_type = expected_type
def validate(self, value):
- if self._expected_type == type(value).__name__:
- return value
+ if type(self._expected_type) == str:
+ if self._expected_type == type(value).__name__:
+ return value
+ else:
+ if isinstance(value, self._expected_type):
+ return value
raise TypeConstraintError("Wrong type for value %s, got type %s; expected %s" % \
(str(value),
type(value).__name__,
@@ -116,13 +123,21 @@ class MultiTypeConstraint(Constraint):
"""
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.
+ of the types to enforce, or a list of type
+ objects. If there are type objects, then
+ value inheriting from this type will be
+ validated.
"""
self._expected_types_list = expected_types_list
-
+
def validate(self, value):
- if type(value).__name__ in self._expected_types_list:
- return value
+ for expected_type in self._expected_types_list:
+ if type(expected_type).__name__ == 'str':
+ if expected_type == type(value).__name__:
+ return value
+ else:
+ if isinstance(value, expected_type):
+ return value
raise TypeConstraintError("Wrong type for value %s, got type %s; expected one in %s" % \
(str(value),
type(value).__name__,
diff --git a/tutorius/properties.py b/tutorius/properties.py
index 2258656..91bda77 100644
--- a/tutorius/properties.py
+++ b/tutorius/properties.py
@@ -28,7 +28,7 @@ from .constraints import Constraint, \
ColorConstraint, FileConstraint, BooleanConstraint, EnumConstraint, \
ResourceConstraint, IntTypeConstraint, FloatTypeConstraint, \
StringTypeConstraint, ArrayTypeConstraint, BoolTypeConstraint, \
- SequenceTypeConstraint
+ SequenceTypeConstraint, TypeConstraint, TypeConstraintError
from .propwidgets import PropWidget, \
StringPropWidget, \
@@ -402,6 +402,10 @@ class TUAMProperty(TutoriusProperty):
self.default = self.validate(value)
+class AddonTypeConstraint(TypeConstraint):
+ def __init__(self):
+ TypeConstraint.__init__(self, TPropContainer)
+
class TAddonProperty(TutoriusProperty):
"""
Reprensents an embedded tutorius Addon Component (action, trigger, etc.)
@@ -416,13 +420,9 @@ class TAddonProperty(TutoriusProperty):
def __init__(self):
super(TAddonProperty, self).__init__()
self.type = "addon"
+ self.addon_type = AddonTypeConstraint()
self.default = self.NullAction()
- def validate(self, value):
- if isinstance(value, TPropContainer):
- return super(TAddonProperty, self).validate(value)
- raise ValueError("Expected TPropContainer instance as TaddonProperty value")
-
class TEventType(TutoriusProperty):
"""
Represents an GUI signal for a widget.
@@ -434,6 +434,20 @@ class TEventType(TutoriusProperty):
self.default = self.validate(value)
+class ListOfAddonTypeConstraint(TypeConstraint):
+ """
+ Ensures that the value is a list of Addons.
+ """
+ def __init__(self):
+ TypeConstraint.__init__(self, 'list')
+
+ def validate(self, value):
+ value = TypeConstraint.validate(self, value)
+ for component in value:
+ if not (isinstance(component, TPropContainer)):
+ raise TypeConstraintError("Expected a list of TPropContainer instances inside ListOfAddonTypeConstraint, got a %s" % (str(type(component))))
+ return value
+
class TAddonListProperty(TutoriusProperty):
"""
Reprensents an embedded tutorius Addon List Component.
@@ -442,13 +456,6 @@ class TAddonListProperty(TutoriusProperty):
def __init__(self):
TutoriusProperty.__init__(self)
self.type = "addonlist"
+ self.list_of_addon_type = ListOfAddonTypeConstraint()
self.default = []
- def validate(self, value):
- if isinstance(value, list):
- for component in value:
- if not (isinstance(component, TPropContainer)):
- raise ValueError("Expected a list of TPropContainer instances inside TAddonListProperty value, got a %s" % (str(type(component))))
- return value
- raise ValueError("Value proposed to TAddonListProperty is not a list")
-