[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Forms - Converter.py:1.1 IConverter.py:1.1 IWidget.py:1.1 Widget.py:1.1

Stephan Richter srichter@cbu.edu
Sun, 14 Jul 2002 09:32:54 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Forms
In directory cvs.zope.org:/tmp/cvs-serv5853/Zope/App/Forms

Added Files:
	Converter.py IConverter.py IWidget.py Widget.py 
Log Message:
- I finished the Schema code. We now have the following Fields defined
  by default: String, Boolean, Integer, Float, Tuple, List, Dictionary

- I also reworked the Validator in a major way. A Validator now does only 
  one thing, such as check that a value has the right type. The Validators
  are then put into a ContainerValidator which runs through all the 
  specified Validators. BTW, I hope you like the code... I do. :)

- Rewrote and added new tests. Note that there are no explicit tests for 
  the Validators, since they are tested already through the Field validate
  method.

- Started working on Forms. There is a whole lot of work to be done there.
  More checkins on that later...


=== Added File Zope3/lib/python/Zope/App/Forms/Converter.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: Converter.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""
from Zope.App.Forms.IConverter import IConverter
from Schema.IField import *

class Converter:
    """ """
    __implements__ =  IConverter

    def __init__(self, context):
        self.context = context

    def convert(self, value):
        'See Zope.App.Forms.IConverter.IConverter'
        return value


class FieldToFieldConverter(Converter):
    """ """
    __convert_from__ = IField
    __convert_to__ = IField

    def convert(self, value):
        'See Zope.App.Forms.IConverter.IConverter'
        assert isinstance(value, self.__convert_from__.type), 'Wrong Type'
        value = self.__convert_to__.type(value)
        assert isinstance(value, self.__convert_to__.type), 'Wrong Type'
        return value


class StringToIntegerConverter(FieldConverter):
    """ """
    __convert_from__ = IString
    __convert_to__ = IInteger


class StringToFloatConverter(FieldConverter):
    """ """
    __convert_from__ = IString
    __convert_to__ = IFloat


class StringToBooleanConverter(FieldConverter):
    """ """
    __convert_from__ = IString
    __convert_to__ = IBoolean



class RequestConverter(Converter):
    """ """
    __convert_from__ = IRequest
    __convert_to__ = IString
    
    field_prefix = 'field_'        

    def convert(self, value):
        'See Zope.App.Forms.IConverter.IConverter'
        request = self.context
        raw_value = request.form.get(self.field_prefix+value)
        return raw_value


class ContainerConverter(Converter):
    """ """
    converters = []

    def convert(self, value):
        'See Zope.App.Forms.IConverter.IConverter'
        for converter in converters:
            value = converter(self.context).convert(value)
        return value

    


=== Added File Zope3/lib/python/Zope/App/Forms/IConverter.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: IConverter.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""

from Interface import Interface
from Interface.Attribute import Attribute


class IConverter(Interface):
    """Converts from one type of Field type to another one. Most common will
    be to convert from String to another type, such as Integer."""

    __convert_to__ = Attribute('The field type this converter converts to.')
    __convert_from__ = Attribute('The field type this converter accepts '
                                 'for conversion.')

    def convert(value):
        """This method converts from __convert_from__ to __convert_to__."""


=== Added File Zope3/lib/python/Zope/App/Forms/IWidget.py ===
##############################################################################
#
# Copyright (c) 2001, 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: IWidget.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""
from Zope.ComponentArchitecture.IContextDependent import IContextDependent
from Interface.Attribute import Attribute

class IWidget(IContextDependent):
    """Generically describes the behavior of a widget.

    The widget defines a list of propertyNames, which describes
    what properties of the widget are available to use for
    constructing the widget render output.

    Note that this level must be still presentation independent.
    """

    propertyNames = Attribute("""This is a list of attributes that are
                                 defined for the widget.""")

    def getValue(name):
        """Look up a Widget setting (value) by name."""


=== Added File Zope3/lib/python/Zope/App/Forms/Widget.py ===
##############################################################################
#
# Copyright (c) 2001, 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: Widget.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""
from IWidget import IWidget

class Widget(object):
    """I do not know what will be in this class, but it provides an extra
    layer."""
    __implements__ = IWidget

    # See Zope.App.Forms.IWidget.IWidget
    propertyNames = []

    def getValue(self, name):
        'See Zope.App.Forms.IWidget.IWidget'
        if name in self.propertyNames:
            return getattr(self, name, None)