summaryrefslogtreecommitdiff
path: root/src/zope/configuration/tests/nested.py
blob: e9a4c25f0497ce1ac179e6206639d0a25869f37e (plain)
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
##############################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (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.
#
##############################################################################
""" Utilities for the 'nested directive' section in the narrative docs.
"""

from zope.interface import Attribute
from zope.interface import Interface
from zope.interface import implementer
from zope.schema import Id
from zope.schema import Int
from zope.schema import NativeStringLine
from zope.schema import Text
from zope.schema import TextLine

from zope.configuration.config import GroupingContextDecorator
from zope.configuration.fields import Bool


schema_registry = {}


class ISchemaInfo(Interface):
    """Parameter schema for the schema directive
    """

    name = TextLine(
        title="The schema name",
        description="This is a descriptive name for the schema.",
    )

    id = Id(title="The unique id for the schema")


class ISchema(Interface):
    """Interface that distinguishes the schema directive
    """

    fields = Attribute("Dictionary of field definitions")


@implementer(ISchema)
class Schema(GroupingContextDecorator):
    """Handle schema directives
    """

    def __init__(self, context, name, id):
        self.context, self.name, self.id = context, name, id
        self.fields = {}

    def after(self):
        schema = Interface.__class__(
            self.name,
            (Interface, ),
            self.fields
        )
        schema.__doc__ = self.info.text.strip()
        self.action(
            discriminator=('schema', self.id),
            callable=schema_registry.__setitem__,
            args=(self.id, schema),
        )


class IFieldInfo(Interface):

    name = NativeStringLine(
        title="The field name",
    )

    title = TextLine(
        title="Title",
        description="A short summary or label",
        default="",
        required=False,
    )

    required = Bool(
        title="Required",
        description="Determines whether a value is required.",
        default=True)

    readonly = Bool(
        title="Read Only",
        description="Can the value be modified?",
        required=False,
        default=False)


class ITextInfo(IFieldInfo):

    min_length = Int(
        title="Minimum length",
        description=(
            "Value after whitespace processing cannot have less than "
            "min_length characters. If min_length is None, there is "
            "no minimum."
        ),
        required=False,
        min=0,  # needs to be a positive number
        default=0)

    max_length = Int(
        title="Maximum length",
        description=(
            "Value after whitespace processing cannot have greater "
            "or equal than max_length characters. If max_length is "
            "None, there is no maximum."
        ),
        required=False,
        min=0,  # needs to be a positive number
        default=None)


def field(context, constructor, name, **kw):

    # Compute the field
    field = constructor(description=context.info.text.strip(), **kw)

    # Save it in the schema's field dictionary
    schema = context.context
    if name in schema.fields:
        raise ValueError("Duplicate field", name)
    schema.fields[name] = field


def textField(context, **kw):
    field(context, Text, **kw)


class IIntInfo(IFieldInfo):

    min = Int(
        title="Start of the range",
        required=False,
        default=None
    )

    max = Int(
        title="End of the range (excluding the value itself)",
        required=False,
        default=None
    )


def intField(context, **kw):
    field(context, Int, **kw)