[Zope3-checkins] CVS: Zope3/src/zope/schema - __init__.py:1.11 _field.py:1.12 interfaces.py:1.12

Gary Poster gary@zope.com
Thu, 24 Apr 2003 16:52:29 -0400


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

Modified Files:
	__init__.py _field.py interfaces.py 
Log Message:
adding InterfaceField.



=== Zope3/src/zope/schema/__init__.py 1.10 => 1.11 ===
--- Zope3/src/zope/schema/__init__.py:1.10	Mon Apr 14 14:21:36 2003
+++ Zope3/src/zope/schema/__init__.py	Thu Apr 24 16:51:58 2003
@@ -23,6 +23,7 @@
 from zope.schema._field import Password, Dict, Datetime, SourceText
 from zope.schema._field import EnumeratedTextLine, EnumeratedInt
 from zope.schema._field import EnumeratedDatetime, EnumeratedFloat
+from zope.schema._field import InterfaceField
 from zope.schema._schema import getFields, getFieldsInOrder
 from zope.schema._schema import getFieldNames, getFieldNamesInOrder
 from zope.schema.accessors import accessors


=== Zope3/src/zope/schema/_field.py 1.11 => 1.12 ===
--- Zope3/src/zope/schema/_field.py:1.11	Mon Apr 14 16:02:31 2003
+++ Zope3/src/zope/schema/_field.py	Thu Apr 24 16:51:58 2003
@@ -19,13 +19,15 @@
 import warnings
 
 from zope.interface.implements import implements
+from zope.interface.interfaces import IInterface
 
 from zope.schema.interfaces import ValidationError
-from zope.schema.errornames import WrongContainedType
+from zope.schema.errornames import WrongContainedType, WrongType
 
 from zope.schema.interfaces import IField, IContainer, IIterable, IOrderable
 from zope.schema.interfaces import IMinMaxLen, IEnumerated, IText, ITextLine
 from zope.schema.interfaces import ISourceText
+from zope.schema.interfaces import IInterfaceField
 from zope.schema.interfaces import IBool, IInt, IBytes, IBytesLine, IFloat
 from zope.schema.interfaces import IDatetime, ISequence, ITuple, IList, IDict
 from zope.schema.interfaces import IPassword
@@ -114,6 +116,15 @@
 class EnumeratedDatetime(Datetime):
     __doc__ = IEnumeratedDatetime.__doc__
     __implements__ = IEnumeratedDatetime
+
+class InterfaceField(Field):
+    __doc__ = IInterfaceField.__doc__
+    __implements__ = IInterfaceField
+
+    def _validate(self, value):
+        super(InterfaceField, self)._validate(value)
+        if not IInterface.isImplementedBy(value):
+            raise ValidationError(WrongType)
 
 def _validate_sequence(value_types, value, errors=None):
     if errors is None:


=== Zope3/src/zope/schema/interfaces.py 1.11 => 1.12 ===
--- Zope3/src/zope/schema/interfaces.py:1.11	Thu Apr 24 10:51:54 2003
+++ Zope3/src/zope/schema/interfaces.py	Thu Apr 24 16:51:58 2003
@@ -16,6 +16,7 @@
 $Id$
 """
 from zope.interface import Interface
+from zope.app.i18n import ZopeMessageIDFactory as _
 
 class StopValidation(Exception):
     """This exception is raised, if the validation is done for sure early.
@@ -103,35 +104,35 @@
         """
 
     title = TextLine(
-        title=u"Title",
-        description=u"A short summary or label",
+        title=_(u"Title"),
+        description=_(u"A short summary or label"),
         default=u"",
         required=False,
         )
 
     description = Text(
-        title=u"Description",
-        description=u"A description of the field",
+        title=_(u"Description"),
+        description=_(u"A description of the field"),
         default=u"",
         required=False,
         )
 
     required = Bool(
-        title=u"Required",
+        title=_(u"Required"),
         description=(
-        u"tells whether a field requires its value to exist."),
+        _(u"tells whether a field requires its value to exist.")),
         default=True)
 
     readonly = Bool(
-        title=u"Read Only",
-        description=u"Read-only.", # XXX what is this?
+        title=_(u"Read Only"),
+        description=_(u"Read-only."), # XXX what is this?
         required=False,
         default=False)
 
     default = Field(
-        title=u"default field value if no value is present",
-        description=u"""The field default value may be None or a legal
-                        field value"""
+        title=_(u"default field value if no value is present"),
+        description=_(u"""The field default value may be None or a legal
+                        field value""")
         )
 
     def constraint(value):
@@ -152,15 +153,15 @@
         """
 
     order = Int(
-        title=u"Field Order",
-        description=u"""\
+        title=_(u"Field Order"),
+        description=_(u"""\
         The order attribute can be used to determine the order in
         which fields in a schema were defined. If one field is created
         after another (in the same thread), its order will be
         greater.
 
         (Fields in separate threads could have the same order.)
-        """,
+        """),
         required=True,
         readonly=True,
         )
@@ -218,13 +219,13 @@
     """
 
     min = Field(
-        title=u"Start of the range",
+        title=_(u"Start of the range"),
         required=False,
         default=None
         )
 
     max = Field(
-        title=u"End of the range (excluding the value itself)",
+        title=_(u"End of the range (excluding the value itself)"),
         required=False,
         default=None
         )
@@ -234,22 +235,22 @@
     u"""a Field requiring the length of its value to be within a range"""
 
     min_length = Int(
-        title=u"Minimum length",
-        description=u"""\
+        title=_(u"Minimum length"),
+        description=_(u"""\
         Value after whitespace processing cannot have less than
         min_length characters. If min_length is None, there is
         no minimum.
-        """,
+        """),
         required=False,
         min=0, # needs to be a positive number
         default=0)
 
     max_length = Int(
-        title=u"Maximum length",
-        description=u"""\
+        title=_(u"Maximum length"),
+        description=_(u"""\
         Value after whitespace processing cannot have greater
         or equal than max_length characters. If max_length is
-        None, there is no maximum.""",
+        None, there is no maximum."""),
         required=False,
         min=0, # needs to be a positive number
         default=None)
@@ -258,15 +259,19 @@
     u"""Field whose value is contained in a predefined set"""
 
     allowed_values = Container(
-        title=u"Allowed Values",
-        description=u"""\
+        title=_(u"Allowed Values"),
+        description=_(u"""\
         Only values specified here can be values of this field.
         If the list is empty, then there are no further
-        restictions.""",
+        restictions."""),
         required=False)
 
 IValueSet = IEnumerated
 
+class IInterfaceField(IField):
+    u"""Fields with a value that is an interface (implementing
+    zope.interface.Interface)."""
+
 class IBool(IField):
     u"""a Boolean Field."""
 
@@ -346,12 +351,12 @@
     """
 
     value_types = Iterable(
-        title=u"Value Types",
+        title=_(u"Value Types"),
         description=(
-        u"""\
+        _(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,
         )
@@ -370,22 +375,23 @@
     """
 
     key_types = Iterable(
-        title=u"Value Types",
-        description=u"""\
+        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",
+        title=_(u"Value Types"),
         description=(
-        u"""\
+        _(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,
         )
+