From 96e5675b0003d57cb87eb44f470aa2d309d8329b Mon Sep 17 00:00:00 2001 From: mike Date: Sat, 05 Dec 2009 19:22:28 +0000 Subject: BoyScout2.0 : Adding type objects support inside the TypeConstraint --- (limited to 'tutorius') 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 4a80e9c..bfdb32c 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, \ @@ -424,6 +424,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.) @@ -438,13 +442,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. @@ -456,6 +456,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. @@ -464,13 +478,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") - -- cgit v0.9.1