[Zope3-checkins] CVS: Zope3/lib/python/Zope/Schema - FieldProperty.py:1.1 Exceptions.py:1.2 IField.py:1.2 IValidator.py:1.2 Validator.py:1.2 _Field.py:1.2 _Schema.py:1.2 __init__.py:1.2 Converter.py:NONE IConverter.py:NONE

Jim Fulton jim@zope.com
Sat, 7 Sep 2002 12:18:52 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/Schema
In directory cvs.zope.org:/tmp/cvs-serv19433/lib/python/Zope/Schema

Modified Files:
	Exceptions.py IField.py IValidator.py Validator.py _Field.py 
	_Schema.py __init__.py 
Added Files:
	FieldProperty.py 
Removed Files:
	Converter.py IConverter.py 
Log Message:
More cleanup/refactoring of Schemas and forms. There's more to come,
but I'm checkpointing here.

I:

- Added schema field properties. These are like standard Python
  properies except that they are derived from Schema fields.

- Decomposed Str fields into Bytes fields and Text fields.
  Bytes fields contain 8-bit data and are stored as python strings.
  Text fields contain written human discourse, and are stored as
  unicode. It is invalid to store Python strings in Text fields or
  unicode in Bytes fields.

- Moved converters from schemas to forms, where they are used.

- Widgets are now responsible for:

  - Getting raw data from the request

  - Converting raw data to application data

  - Validating converted data against schema fields

- Began defining an error framework for errors in forms.

- Simplified FormViews to reflect new widget responsibilities.

- Added Bytes, Int and Float widgets and changed some application and
  test code to use them.




=== Added File Zope3/lib/python/Zope/Schema/FieldProperty.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""
$Id: FieldProperty.py,v 1.1 2002/09/07 16:18:51 jim Exp $
"""

__metaclass__ = type

_marker = object()

class FieldProperty:
    """Computed attributes based on schema fields

    Field properties provide default values, data validation and error messages
    based on data found in field meta-data.
    """

    def __init__(self, field, name=None):
        if name is None:
            name = field.__name__

        self.__field = field
        self.__name = name
        self.__private_name = "_fp__" + name

    def __get__(self, inst, klass):
        if inst is None:
            return self

        value = getattr(inst, self.__private_name, _marker)
        if value is _marker:
            value = getattr(self.__field, 'default', _marker)
            if value is _marker:
                raise AttributeError, self.__name

        return value

    def __set__(self, inst, value):
        self.__field.validate(value)
        setattr(inst, self.__private_name, value)
            

__doc__ = FieldProperty.__doc__ + __doc__



=== Zope3/lib/python/Zope/Schema/Exceptions.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/Schema/Exceptions.py:1.1	Thu Sep  5 14:55:03 2002
+++ Zope3/lib/python/Zope/Schema/Exceptions.py	Sat Sep  7 12:18:51 2002
@@ -35,14 +35,7 @@
         return cmp(self.error_name, other.error_name)
 
 
-class ConversionError(Exception):
-    """If some conversion fails, this exception is raised."""
-
-    def __init__(self, error_name, original_exception=None):
-        Exception.__init__(self)
-        self.error_name = error_name
-        self.original_exception = original_exception
-
+# XXX YAGNI, this is doomed. ;)
 
 class ErrorContainer(Exception):
     """ """


=== Zope3/lib/python/Zope/Schema/IField.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/Schema/IField.py:1.1	Thu Sep  5 14:55:03 2002
+++ Zope3/lib/python/Zope/Schema/IField.py	Sat Sep  7 12:18:51 2002
@@ -24,13 +24,13 @@
     IBool, requiredness settings may make no difference.
     """
 
-    title = Field.Str(
+    title = Field.Text(
         title="Title",
         description="Title.",
         default=""
         )
     
-    description = Field.Str(
+    description = Field.Text(
         title="Description",
         description="Description.",
         default="",
@@ -46,6 +46,11 @@
         description="Required.",
         default=1)
 
+    def validate(value):
+        """Validate that the given value is a valid field entry.
+
+        Returns nothing but raises an error if the value is invalid.
+        """
 
 class ISingleValueField(IField):
     """This field consists always only of one value and it not a homogeneous
@@ -71,13 +76,39 @@
         default=0)
 
 
-class IStr(ISingleValueField):
-    """Describes the footprint of a Str variable."""
+class IBytes(ISingleValueField):
+    """Describes the footprint of a Bytes variable."""
 
-    default = Field.Str(
+    default = Field.Bytes(
         title="Default",
         description="Default.",
         default="")
+    
+    min_length = Field.Int(
+        title="Minimum length",
+        description=("Value after whitespace processing cannot have less than "
+                     "min_length characters. If min_length is None, there is "
+                     "no minimum."),
+        required=0,
+        min=0, # needs to be a positive number
+        default=0)
+
+    max_length = Field.Int(
+        title="Maximum length",
+        description=("Value after whitespace processing cannot have greater "
+                     "or equal than max_length characters. If max_length is "
+                     "None, there is no maximum."),
+        required=0,
+        min=0, # needs to be a positive number
+        default=None)
+
+class IText(ISingleValueField):
+    """Describes the footprint of a Str variable."""
+
+    default = Field.Text(
+        title="Default",
+        description="Default.",
+        default=u"")
     
     min_length = Field.Int(
         title="Minimum length",


=== Zope3/lib/python/Zope/Schema/IValidator.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/Schema/IValidator.py:1.1	Thu Sep  5 14:55:03 2002
+++ Zope3/lib/python/Zope/Schema/IValidator.py	Sat Sep  7 12:18:51 2002
@@ -32,4 +32,7 @@
         """Validate the the value.
 
         This should not return anything, only raise an exception in case
-        of an invalid value.""" 
+        of an invalid value.
+
+        The special exception, Zope.Schema.StopValidation may be raised to
+        indicate that no more validation should be performed.""" 


=== Zope3/lib/python/Zope/Schema/Validator.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/Schema/Validator.py:1.1	Thu Sep  5 14:55:03 2002
+++ Zope3/lib/python/Zope/Schema/Validator.py	Sat Sep  7 12:18:51 2002
@@ -197,6 +197,7 @@
                                                MinimumLengthValidator,
                                                MaximumLengthValidator]
 
+
 class BoolValidator(SimpleValidator):
     """Completely validates a Bool Field."""
 


=== Zope3/lib/python/Zope/Schema/_Field.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/Schema/_Field.py:1.1	Thu Sep  5 14:55:03 2002
+++ Zope3/lib/python/Zope/Schema/_Field.py	Sat Sep  7 12:18:51 2002
@@ -41,13 +41,19 @@
 
     def validate(self, value):
         try:
-            return self.validator(self).validate(value)
+            self.validator(self).validate(value)
         except StopValidation:
-            return value
+            pass
 
-class Str(Field):
+class Bytes(Field):
     """A field representing a Str."""
-    type = str, unicode
+    type = str
+    min_length = None
+    max_length = None
+
+class Text(Field):
+    """A field representing a Str."""
+    type = unicode
     min_length = None
     max_length = None
 


=== Zope3/lib/python/Zope/Schema/_Schema.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/Schema/_Schema.py:1.1	Thu Sep  5 14:55:03 2002
+++ Zope3/lib/python/Zope/Schema/_Schema.py	Sat Sep  7 12:18:51 2002
@@ -75,10 +75,15 @@
     implements(Bool, IBool, 0)
     Bool.validator = Validator.BoolValidator
 
-    from IField import IStr
-    from _Field import Str
-    implements(Str, IStr, 0)
-    Str.validator = Validator.StrValidator
+    from IField import IBytes
+    from _Field import Bytes
+    implements(Bytes, IBytes, 0)
+    Bytes.validator = Validator.StrValidator
+
+    from IField import IText
+    from _Field import Text
+    implements(Text, IText, 0)
+    Text.validator = Validator.StrValidator
 
     from IField import IInt
     from _Field import Int


=== Zope3/lib/python/Zope/Schema/__init__.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/Schema/__init__.py:1.1	Thu Sep  5 14:55:03 2002
+++ Zope3/lib/python/Zope/Schema/__init__.py	Sat Sep  7 12:18:51 2002
@@ -15,5 +15,5 @@
 
 $Id$
 """
-from _Field import Field, Str, Bool, Int, Float, Tuple, List, Dict
+from _Field import Field, Bytes, Text, Bool, Int, Float, Tuple, List, Dict
 from _Schema import validateMapping, validateMappingAll, getFields

=== Removed File Zope3/lib/python/Zope/Schema/Converter.py ===

=== Removed File Zope3/lib/python/Zope/Schema/IConverter.py ===