File: compound.py

package info (click to toggle)
python-formencode 2.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,844 kB
  • sloc: python: 7,032; makefile: 139; sh: 96; javascript: 61
file content (285 lines) | stat: -rw-r--r-- 8,993 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
Validators for applying validations in sequence.
"""
from __future__ import absolute_import

from .api import (FancyValidator, Identity, Invalid, NoDefault, Validator,
    is_validator)
import six

__all__ = ['CompoundValidator', 'Any', 'All', 'Pipe']

############################################################
## Compound Validators
############################################################


def to_python(validator, value, state):
    return validator.to_python(value, state)


def from_python(validator, value, state):
    return validator.from_python(value, state)


class CompoundValidator(FancyValidator):
    """Base class for all compound validators."""

    if_invalid = NoDefault
    accept_iterator = False

    validators = []

    __unpackargs__ = ('*', 'validatorArgs')

    __mutableattributes__ = ('validators',)

    _deprecated_methods = (
        ('attempt_convert', '_attempt_convert'),)

    @staticmethod
    def __classinit__(cls, new_attrs):
        FancyValidator.__classinit__(cls, new_attrs)
        toAdd = []
        for name, value in six.iteritems(new_attrs):
            if is_validator(value) and value is not Identity:
                toAdd.append((name, value))
                # @@: Should we really delete too?
                delattr(cls, name)
        toAdd.sort()
        cls.validators.extend([value for _name, value in toAdd])

    def __init__(self, *args, **kw):
        Validator.__init__(self, *args, **kw)
        self.validators = self.validators[:]
        self.validators.extend(self.validatorArgs)

    @staticmethod
    def _repr_vars(names):
        return [n for n in Validator._repr_vars(names)
                if n != 'validatorArgs']

    def _attempt_convert(self, value, state, convertFunc):
        raise NotImplementedError("Subclasses must implement _attempt_convert")

    def _convert_to_python(self, value, state=None):
        return self._attempt_convert(value, state,
                                    to_python)

    def _convert_from_python(self, value, state=None):
        return self._attempt_convert(value, state,
                                    from_python)

    def subvalidators(self):
        return self.validators


class Any(CompoundValidator):
    """Check if any of the specified validators is valid.

    This class is like an 'or' operator for validators.  The first
    validator/converter in the order of evaluation that validates the value
    will be used.

    The order of evaluation differs depending on if you are validating to
    Python or from Python as follows:

    The validators are evaluated right to left when validating to Python.

    The validators are evaluated left to right when validating from Python.

    Examples::

        >>> from formencode.validators import DictConverter
        >>> av = Any(validators=[DictConverter({2: 1}),
        ... DictConverter({3: 2}), DictConverter({4: 3})])
        >>> av.to_python(3)
        2
        >>> av.from_python(2)
        3

    """

    def _attempt_convert(self, value, state, validate):
        lastException = None
        validators = self.validators
        if validate is to_python:
            validators = reversed(validators)
        for validator in validators:
            try:
                return validate(validator, value, state)
            except Invalid as e:
                lastException = e
        if self.if_invalid is NoDefault:
            raise lastException
        return self.if_invalid

    @property
    def not_empty(self):
        not_empty = True
        for validator in self.validators:
            not_empty = not_empty and getattr(validator, 'not_empty', False)
        return not_empty

    def is_empty(self, value):
        # sub-validators should handle emptiness.
        return False

    @property
    def accept_iterator(self):
        accept_iterator = False
        for validator in self.validators:
            accept_iterator = accept_iterator or getattr(
                validator, 'accept_iterator', False)
        return accept_iterator


class All(CompoundValidator):
    """Check if all of the specified validators are valid.

    This class is like an 'and' operator for validators.  All
    validators must work, and the results are passed in turn through
    all validators for conversion in the order of evaluation. All
    is the same as `Pipe` but operates in the reverse order.

    The order of evaluation differs depending on if you are validating to
    Python or from Python as follows:

    The validators are evaluated right to left when validating to Python.

    The validators are evaluated left to right when validating from Python.

    `Pipe` is more intuitive when predominantly validating to Python.

    Examples::

        >>> from formencode.validators import DictConverter
        >>> av = All(validators=[DictConverter({2: 1}),
        ... DictConverter({3: 2}), DictConverter({4: 3})])
        >>> av.to_python(4)
        1
        >>> av.from_python(1)
        4

    """

    def __repr__(self):
        return '<All %s>' % self.validators

    def _attempt_convert(self, value, state, validate):
        # To preserve the order of the transformations, we do them
        # differently when we are converting to and from Python.
        validators = self.validators
        if validate is to_python:
            validators = reversed(validators)
        try:
            for validator in validators:
                value = validate(validator, value, state)
        except Invalid:
            if self.if_invalid is NoDefault:
                raise
            return self.if_invalid
        return value

    def with_validator(self, validator):
        """Add another validator.

        Adds the validator (or list of validators) to a copy of
        this validator.
        """
        new = self.validators[:]
        if isinstance(validator, (list, tuple)):
            new.extend(validator)
        else:
            new.append(validator)
        return self.__class__(*new, **dict(if_invalid=self.if_invalid))

    @classmethod
    def join(cls, *validators):
        """Join the specified validators.

        Joins several validators together as a single validator,
        filtering out None and trying to keep `All` validators from
        being nested (which isn't needed).
        """
        validators = [v for v in validators if v and v is not Identity]
        if not validators:
            return Identity
        if len(validators) == 1:
            return validators[0]
        if isinstance(validators[0], All):
            return validators[0].with_validator(validators[1:])
        return cls(*validators)

    @property
    def if_missing(self):
        for validator in self.validators:
            v = validator.if_missing
            if v is not NoDefault:
                return v
        return NoDefault

    @property
    def not_empty(self):
        not_empty = False
        for validator in self.validators:
            not_empty = not_empty or getattr(validator, 'not_empty', False)
        return not_empty

    def is_empty(self, value):
        # sub-validators should handle emptiness.
        return False

    @property
    def accept_iterator(self):
        accept_iterator = True
        for validator in self.validators:
            accept_iterator = accept_iterator and getattr(
                validator, 'accept_iterator', False)
        return accept_iterator


class Pipe(All):
    """Pipe value through all specified validators.

    This class works like `All` but the order of evaluation is opposite. All
    validators must work, and the results are passed in turn through
    each validator for conversion in the order of evaluation.  A behaviour
    known to Unix and GNU users as 'pipe'.

    The order of evaluation differs depending on if you are validating to
    Python or from Python as follows:

    The validators are evaluated left to right when validating to Python.

    The validators are evaluated right to left when validating from Python.

    Examples::

        >>> from formencode.validators import DictConverter
        >>> pv = Pipe(validators=[DictConverter({1: 2}),
        ... DictConverter({2: 3}), DictConverter({3: 4})])
        >>> pv.to_python(1)
        4
        >>> pv.from_python(4)
        1

    """

    def __repr__(self):
        return '<Pipe %s>' % self.validators

    def _attempt_convert(self, value, state, validate):
        # To preserve the order of the transformations, we do them
        # differently when we are converting to and from Python.
        validators = self.validators
        if validate is from_python:
            validators = reversed(self.validators)
        try:
            for validator in validators:
                value = validate(validator, value, state)
        except Invalid:
            if self.if_invalid is NoDefault:
                raise
            return self.if_invalid
        return value