File: interfaces.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 (72 lines) | stat: -rw-r--r-- 2,086 bytes parent folder | download | duplicates (4)
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
"""
Interfaces for FormEncode (for documentation purposes only)
"""


class Attribute(object):

    def __init__(self, description, name=None):
        self.description = description
        self.name = name


class Interface(object):
    pass


class IDeclarative(Interface):

    def __init__(**kw):
        """
        Instantiates this class with all the keywords being used to
        update the instance variables.
        """

    def __call__(**kw):
        """
        Returns a copy with all attributes using the given keywords,
        being updated.
        """


class IValidator(IDeclarative):

    messages = Attribute("""
    A dictionary of messages (with formatting strings) for error
    responses""", name='messages')
    if_missing = Attribute("""
    If the source that this validator would handle is missing (e.g.,
    a field that was not specified), use this value.  If
    Validator.NoDefault, then if the field is missing an exception
    should be raised.""", name='ifMissing')
    repeating = Attribute("""
    A boolean; this object accepts lists if true, subvalidators can be
    found in the validators attribute.""", name='repeating')
    compound = Attribute("""
    A boolean; this object has a dictionary of validators if this is
    true, subvalidators can be found in the field attribute (a
    dictionary).""", name='compound')

    def to_python(value, state=None):
        """
        Convert `value` from its foreign representation to its Python
        representation.  `state` is for application-specific hooks.
        """

    def from_python(value, state=None):
        """
        Convert `value` from its Python representation to the foreign
        representation.  `state` is for application-specific hooks.
        """

    def message(name, default):
        """
        Return the message (from the `messages` attribute) that goes
        with `name`, or return default if `name` not found `default`.
        """


class ISchema(IValidator):

    fields = Attribute('A dictionary of (field name: validator)',
        name='fields')