1##############################################################################
2#
3# Copyright (c) 2002 Zope Foundation and Contributors.
4# All Rights Reserved.
5#
6# This software is subject to the provisions of the Zope Public License,
7# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11# FOR A PARTICULAR PURPOSE.
12#
13##############################################################################
14"""Bootstrap schema interfaces and exceptions
15"""
16import zope.interface
17
18from zope.schema._messageid import _
19
20class StopValidation(Exception):
21    """Raised if the validation is completed early.
22
23    Note that this exception should be always caught, since it is just
24    a way for the validator to save time.
25    """
26
27class ValidationError(zope.interface.Invalid):
28    """Raised if the Validation process fails."""
29
30    def doc(self):
31        return self.__class__.__doc__
32
33    def __cmp__(self, other):
34        if not hasattr(other, 'args'):
35            return -1
36        return cmp(self.args, other.args)
37
38    def __eq__(self, other):
39        if not hasattr(other, 'args'):
40            return False
41        return self.args == other.args
42
43    __hash__ = zope.interface.Invalid.__hash__ # python3
44
45    def __repr__(self): #pragma NO COVER
46        return '%s(%s)' % (self.__class__.__name__,
47            ', '.join(repr(arg) for arg in self.args))
48
49class RequiredMissing(ValidationError):
50    __doc__ = _("""Required input is missing.""")
51
52class WrongType(ValidationError):
53    __doc__ = _("""Object is of wrong type.""")
54
55class TooBig(ValidationError):
56    __doc__ = _("""Value is too big""")
57
58class TooSmall(ValidationError):
59    __doc__ = _("""Value is too small""")
60
61class TooLong(ValidationError):
62    __doc__ = _("""Value is too long""")
63
64class TooShort(ValidationError):
65    __doc__ = _("""Value is too short""")
66
67class InvalidValue(ValidationError):
68    __doc__ = _("""Invalid value""")
69
70class ConstraintNotSatisfied(ValidationError):
71    __doc__ = _("""Constraint not satisfied""")
72
73class NotAContainer(ValidationError):
74    __doc__ = _("""Not a container""")
75
76class NotAnIterator(ValidationError):
77    __doc__ = _("""Not an iterator""")
78
79
80class IFromUnicode(zope.interface.Interface):
81    """Parse a unicode string to a value
82
83    We will often adapt fields to this interface to support views and
84    other applications that need to conver raw data as unicode
85    values.
86
87    """
88
89    def fromUnicode(str):
90        """Convert a unicode string to a value.
91        """
92
93class IContextAwareDefaultFactory(zope.interface.Interface):
94    """A default factory that requires a context.
95
96    The context is the field context. If the field is not bound, context may
97    be ``None``.
98    """
99
100    def __call__(context):
101        """Returns a default value for the field."""
102