[Zope-Checkins] CVS: Zope3/lib/python/Schema - Converter.py:1.1 IConverter.py:1.1 Exceptions.py:1.5
Martijn Faassen
m.faassen@vet.uu.nl
Sun, 14 Jul 2002 18:45:17 -0400
Update of /cvs-repository/Zope3/lib/python/Schema
In directory cvs.zope.org:/tmp/cvs-serv32286
Modified Files:
Exceptions.py
Added Files:
Converter.py IConverter.py
Log Message:
Added a start of a converter framework, loosely based on Stephan's
initial implementation, but inside the schema package. Note that
converters *are* view specific in that specific field views (widgets)
will use specific converters -- the framework itself and quite a few
basic converters however are general enough to fit them in Schema.
=== Added File Zope3/lib/python/Schema/Converter.py ===
from IConverter import IConverter
from Exceptions import ConversionError
# XXX is it worth is doing isImplementedBy checks in converters (input values)?
# if evil code could corrupt matters yes, but can it? (after all
# it'll also have to pass validation, unless the converter framework
# is used independently..something to ponder)
class NullConverter(object):
"""A converter that really doesn't do a thing.
"""
__implements__ = IConverter
def convert(self, value):
return value
class CombinedConverter(object):
"""A converter that chains several converters to each other.
"""
__implements__ = IConverter
def __init__(self, converters):
self._converters = converters
def convert(self, value):
for converter in self._converters:
value = converter.convert(value)
return value
class FunctionConverter(object):
"""Use a Python function to convert.
Turns *any* Python exception into a conversion error.
XXX Is this good/useful?
"""
__implements__ = IConverter
def convert(self, value):
try:
return self._function(value)
except Exception, e:
raise ConversionError('exception', e)
def _functionConverterFactory(klass_name, function):
"""Create a derived class of FunctionConvert which uses function.
"""
klass = type(klass_name, (FunctionConverter,), {})
klass._function = function
return klass
StrToIntConverter= _functionConverterFactory('StrToIntConverter', int)
IntToStrConverter = _functionConverterFactory('IntToStrConverter', str)
StrToFloatConverter = _functionConverterFactory('StrToFloatConverter', float)
FloatToStrConverter = _functionConverterFactory('FloatToStrConverter', str)
=== Added File Zope3/lib/python/Schema/IConverter.py ===
from Interface import Interface
class IConverter(Interface):
def convert(self, value):
"""Call an IConverter with a value, and it will try to convert to
another value and return the result. If conversion cannot take
place, the convertor will raise a ConversionError. (or a
ValidationError in case of Converters using Schemas inside?)
"""
pass
=== Zope3/lib/python/Schema/Exceptions.py 1.4 => 1.5 ===
way for the validator to save time."""
pass
-
class ValidationError(Exception):
"""If some check during the Validation process fails, this exception is
raised."""
@@ -34,7 +33,6 @@
def __cmp__(self, other):
return cmp(self.error_name, other.error_name)
-
class ValidationErrorsAll(Exception):
"""This is a collection error that contains all exceptions that occured
during the validation process."""
@@ -42,3 +40,11 @@
def __init__(self, list):
Exception.__init__(self)
self.errors = list
+
+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