[Zope3-checkins] CVS: Zope3/src/zope/schema - _field.py:1.17 interfaces.py:1.25

Richard Jones richard@commonground.com.au
Fri, 11 Jul 2003 22:47:57 -0400


Update of /cvs-repository/Zope3/src/zope/schema
In directory cvs.zope.org:/tmp/cvs-serv26157/src/zope/schema

Modified Files:
	_field.py interfaces.py 
Log Message:
Sequence value_types argument is now value_type (ie. a single Field type)
Dict key_types and value_types are also changed to key_type and value_type.
[having multiple value/key types makes no sense and actually makes widgets
extraordinarily difficult to implement (read: approaching impossible :)]


=== Zope3/src/zope/schema/_field.py 1.16 => 1.17 ===
--- Zope3/src/zope/schema/_field.py:1.16	Fri Jul 11 21:29:04 2003
+++ Zope3/src/zope/schema/_field.py	Fri Jul 11 22:47:22 2003
@@ -126,25 +126,24 @@
         if not IInterface.isImplementedBy(value):
             raise ValidationError(WrongType)
 
-def _validate_sequence(value_types, value, errors=None):
+def _validate_sequence(value_type, value, errors=None):
     if errors is None:
         errors = []
 
-    if value_types is None:
+    if value_type is None:
         return errors
 
     for item in value:
         error = None
-        for t in value_types:
-            try:
-                t.validate(item)
-            except ValidationError, error:
-                pass
-            else:
-                # We validated, so clear error (if any) and done with
-                # this value
-                error = None
-                break
+        try:
+            value_type.validate(item)
+        except ValidationError, error:
+            pass
+        else:
+            # We validated, so clear error (if any) and done with
+            # this value
+            error = None
+            break
 
         if error is not None:
             errors.append(error)
@@ -155,15 +154,16 @@
 class Sequence(MinMaxLen, Iterable, Field):
     __doc__ = ISequence.__doc__
     implements(ISequence)
-    value_types = FieldProperty(ISequence['value_types'])
+    value_type = None
 
-    def __init__(self, value_types=None, **kw):
+    def __init__(self, value_type=None, **kw):
         super(Sequence, self).__init__(**kw)
-        self.value_types = value_types
+        # XXX reject value_type of None?
+        self.value_type = value_type
 
     def _validate(self, value):
         super(Sequence, self)._validate(value)
-        errors = _validate_sequence(self.value_types, value)
+        errors = _validate_sequence(self.value_type, value)
         if errors:
             raise ValidationError(WrongContainedType, errors)
 
@@ -183,22 +183,23 @@
     """A field representing a Dict."""
     implements(IDict)
     _type = dict
-    key_types   = FieldProperty(IDict['key_types'])
-    value_types = FieldProperty(IDict['value_types'])
+    key_type   = None
+    value_type = None
 
-    def __init__(self, key_types=None, value_types=None, **kw):
+    def __init__(self, key_type=None, value_type=None, **kw):
         super(Dict, self).__init__(**kw)
-        self.key_types = key_types
-        self.value_types = value_types
+        # XXX reject key_type, value_type of None?
+        self.key_type = key_type
+        self.value_type = value_type
 
     def _validate(self, value):
         super(Dict, self)._validate(value)
         errors = []
         try:
-            if self.value_types:
-                errors = _validate_sequence(self.value_types, value.values(),
+            if self.value_type:
+                errors = _validate_sequence(self.value_type, value.values(),
                                             errors)
-            errors = _validate_sequence(self.key_types, value, errors)
+            errors = _validate_sequence(self.key_type, value, errors)
 
             if errors:
                 raise ValidationError(WrongContainedType, errors)


=== Zope3/src/zope/schema/interfaces.py 1.24 => 1.25 ===
--- Zope3/src/zope/schema/interfaces.py:1.24	Wed Jun 18 15:02:46 2003
+++ Zope3/src/zope/schema/interfaces.py	Fri Jul 11 22:47:22 2003
@@ -315,9 +315,14 @@
     The value may be constrained to an element of a specified list.
     """
 
+def _is_field(value):
+    if not IField.isImplementedBy(value):
+        return False
+    return True
+
 def _fields(values):
     for value in values:
-        if not IField.isImplementedBy(value):
+        if not _is_field(value):
             return False
     return True
 
@@ -327,16 +332,10 @@
     The Value must be iterable and may have a min_length/max_length.
     """
 
-    value_types = Iterable(
-        title=_(u"Value Types"),
-        description=(
-        _(u"""\
-        If set to a non-empty value, field value items must conform to one
-        of the given types, which are expressed via fields.
-        """)),
-        required=False,
-        constraint=_fields,
-        )
+    value_type = Attribute("value_type",
+        _(u"""Field value items must conform to the given type, expressed
+           via a Field.
+        """))
 
 class ITuple(ISequence):
     u"""Field containing a conventional tuple."""
@@ -347,30 +346,19 @@
 class IDict(IMinMaxLen, IIterable, IContainer):
     u"""Field containing a conventional dict.
 
-    The key_types and value_types field allow specification
+    The key_type and value_type fields allow specification
     of restrictions for keys and values contained in the dict.
     """
 
-    key_types = Iterable(
-        title=_(u"Value Types"),
-        description=_(u"""\
-        If set to a non-empty value, field value keys must conform to one
-        of the given types, which are expressed via fields.
-        """),
-        constraint=_fields,
-        required=False,
-        )
-
-    value_types = Iterable(
-        title=_(u"Value Types"),
-        description=(
-        _(u"""\
-        If set to a non-empty value, field value values must conform to one
-        of the given types, which are expressed via fields.
-        """)),
-        constraint=_fields,
-        required=False,
-        )
+    key_type = Attribute("key_type",
+        _(u"""Field keys must conform to the given type, expressed
+           via a Field.
+        """))
+
+    value_type = Attribute("value_type",
+        _(u"""Field values must conform to the given type, expressed
+           via a Field.
+        """))
 
 
 class IVocabularyQuery(Interface):