[Zope3-checkins] CVS: Zope3/src/zope/app/component - interfacefield.py:1.5

Steve Alexander steve@cat-box.net
Mon, 6 Jan 2003 13:40:05 -0500


Update of /cvs-repository/Zope3/src/zope/app/component
In directory cvs.zope.org:/tmp/cvs-serv30732/src/zope/app/component

Modified Files:
	interfacefield.py 
Log Message:
Gave the interface and interfaces fields a basetype that can be None.
Made widgets display the interface name "Anything" for the catch-all
interface None.
Updated adapter and view configuration schemas to use None as the 
basetype for for_interface


=== Zope3/src/zope/app/component/interfacefield.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/component/interfacefield.py:1.4	Sun Jan  5 13:56:47 2003
+++ Zope3/src/zope/app/component/interfacefield.py	Mon Jan  6 13:39:32 2003
@@ -26,39 +26,68 @@
     __doc__ = IInterfaceField.__doc__
     __implements__ = IInterfaceField
 
-    type = Interface
-
-    def __init__(self, type=Interface, *args, **kw):
+    # This is the most base basetype.
+    # This isn't the default value. See the 'basetype' arg of __init__ for
+    # that.
+    basetype = None
+
+    def __init__(self, basetype=Interface, *args, **kw):
+        # XXX Workaround for None indicating a missing value
+        if basetype is None:
+            kw['required'] = False
         super(InterfaceField, self).__init__(*args, **kw)
-        self.validate(type)
-        self.type = type
+        self.validate(basetype)
+        self.basetype = basetype
 
     def _validate(self, value):
         super(InterfaceField, self)._validate(value)
-
+        basetype = self.basetype
+        
+        if value is None and basetype is None:
+            return
+
+        if basetype is None:
+            basetype = Interface
+            
         if not IInterface.isImplementedBy(value):
             raise ValidationError("Not an interface", value)
 
-        if not value.extends(self.type, 0):
-            raise ValidationError("Does not extend", value, self.type)
+        if not value.extends(basetype, 0):
+            raise ValidationError("Does not extend", value, basetype)
 
 class InterfacesField(Tuple):
     __doc__ = IInterfacesField.__doc__
     __implements__ = IInterfacesField
 
-    value_type = Interface
-
-    def __init__(self, value_type=Interface, default=(), *args, **kw):
+    # This is the most base basetype.
+    # This isn't the default value. See the 'basetype' arg of __init__ for
+    # that.
+    basetype = None
+
+    def __init__(self, basetype=Interface, default=(), *args, **kw):
+        # XXX Workaround for None indicating a missing value
+        if basetype is None:
+            kw['required'] = False
         super(InterfacesField, self).__init__(default=default, *args, **kw)
-        self.validate((value_type,))
-        self.value_type = value_type
+        self.validate((basetype,))
+        self.basetype = basetype
         # Not using schema.Sequence.value_types
 
     def _validate(self, value):
         super(InterfacesField, self)._validate(value)
+        basetype = self.basetype
+        if basetype is None:
+            none_ok = True
+            basetype = Interface
+        else:
+            none_ok = False
         for item in value:
+            if item is None and none_ok:
+                continue
+                
             if not IInterface.isImplementedBy(item):
                 raise ValidationError("Not an interface", item)
 
-            if not item.extends(self.value_type, 0):
-                raise ValidationError("Does not extend", item, self.value_type)
+            if not item.extends(basetype, 0):
+                raise ValidationError("Does not extend", item, basetype)
+