summaryrefslogtreecommitdiff
path: root/src/zope/schema/interfaces.py
blob: 36275d71dd55bf7e9474986e4748f1dec10e1c71 (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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
##############################################################################
#
# Copyright (c) 2002 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.
#
##############################################################################
"""Schema interfaces and exceptions
"""
__docformat__ = "reStructuredText"

from zope.interface import Attribute
from zope.interface import Interface
from zope.interface.common.mapping import IEnumerableMapping
from zope.interface.interfaces import IInterface

from zope.schema._bootstrapfields import Bool
from zope.schema._bootstrapfields import Complex
from zope.schema._bootstrapfields import Decimal
from zope.schema._bootstrapfields import Field
from zope.schema._bootstrapfields import Int
from zope.schema._bootstrapfields import Integral
from zope.schema._bootstrapfields import Number
from zope.schema._bootstrapfields import Object
from zope.schema._bootstrapfields import Rational
from zope.schema._bootstrapfields import Real
from zope.schema._bootstrapfields import Text
from zope.schema._bootstrapfields import TextLine
# Import from _bootstrapinterfaces only because other packages will expect
# to find these interfaces here.
from zope.schema._bootstrapinterfaces import ConstraintNotSatisfied
from zope.schema._bootstrapinterfaces import IBeforeObjectAssignedEvent
from zope.schema._bootstrapinterfaces import IContextAwareDefaultFactory
from zope.schema._bootstrapinterfaces import IFromBytes
from zope.schema._bootstrapinterfaces import IFromUnicode
from zope.schema._bootstrapinterfaces import InvalidValue
from zope.schema._bootstrapinterfaces import IValidatable
from zope.schema._bootstrapinterfaces import LenOutOfBounds
from zope.schema._bootstrapinterfaces import NotAContainer
from zope.schema._bootstrapinterfaces import NotAnInterface
from zope.schema._bootstrapinterfaces import NotAnIterator
from zope.schema._bootstrapinterfaces import OrderableOutOfBounds
from zope.schema._bootstrapinterfaces import OutOfBounds
from zope.schema._bootstrapinterfaces import RequiredMissing
from zope.schema._bootstrapinterfaces import SchemaNotCorrectlyImplemented
from zope.schema._bootstrapinterfaces import SchemaNotFullyImplemented
from zope.schema._bootstrapinterfaces import SchemaNotProvided
from zope.schema._bootstrapinterfaces import StopValidation
from zope.schema._bootstrapinterfaces import TooBig
from zope.schema._bootstrapinterfaces import TooLong
from zope.schema._bootstrapinterfaces import TooShort
from zope.schema._bootstrapinterfaces import TooSmall
from zope.schema._bootstrapinterfaces import ValidationError
from zope.schema._bootstrapinterfaces import WrongContainedType
from zope.schema._bootstrapinterfaces import WrongType
from zope.schema._messageid import _


__all__ = [
    # Exceptions
    'ConstraintNotSatisfied',
    'InvalidDottedName',
    'InvalidId',
    'InvalidURI',
    'InvalidValue',
    'LenOutOfBounds',
    'NotAContainer',
    'NotAnInterface',
    'NotAnIterator',
    'NotUnique',
    'OrderableOutOfBounds',
    'OutOfBounds',
    'RequiredMissing',
    'SchemaNotCorrectlyImplemented',
    'SchemaNotFullyImplemented',
    'SchemaNotProvided',
    'StopValidation',
    'TooBig',
    'TooLong',
    'TooShort',
    'TooSmall',
    'Unbound',
    'ValidationError',
    'WrongContainedType',
    'WrongType',


    # Interfaces
    'IASCII',
    'IASCIILine',
    'IAbstractBag',
    'IAbstractSet',
    'IBaseVocabulary',
    'IBeforeObjectAssignedEvent',
    'IBool',
    'IBytes',
    'IBytesLine',
    'IChoice',
    'ICollection',
    'IComplex',
    'IContainer',
    'IContextAwareDefaultFactory',
    'IContextSourceBinder',
    'IDate',
    'IDatetime',
    'IDecimal',
    'IDict',
    'IDottedName',
    'IField',
    'IFieldEvent',
    'IFieldUpdatedEvent',
    'IFloat',
    'IFromBytes',
    'IFromUnicode',
    'IFrozenSet',
    'IId',
    'IInt',
    'IIntegral',
    'IInterfaceField',
    'IIterable',
    'IIterableSource',
    'IIterableVocabulary',
    'ILen',
    'IList',
    'IMapping',
    'IMinMax',
    'IMinMaxLen',
    'IMutableMapping',
    'IMutableSequence',
    'INativeString',
    'INativeStringLine',
    'INumber',
    'IObject',
    'IOrderable',
    'IPassword',
    'IPythonIdentifier',
    'IRational',
    'IReal',
    'ISequence',
    'ISet',
    'ISource',
    'ISourceQueriables',
    'ISourceText',
    'ITerm',
    'IText',
    'ITextLine',
    'ITime',
    'ITimedelta',
    'ITitledTokenizedTerm',
    'ITokenizedTerm',
    'ITreeVocabulary',
    'ITuple',
    'IURI',
    'IUnorderedCollection',
    'IVocabulary',
    'IVocabularyFactory',
    'IVocabularyRegistry',
    'IVocabularyTokenized',
]


class NotUnique(ValidationError):
    __doc__ = _("""One or more entries of sequence are not unique.""")


class InvalidURI(ValidationError):
    __doc__ = _("""The specified URI is not valid.""")


class InvalidId(ValidationError):
    __doc__ = _("""The specified id is not valid.""")


class InvalidDottedName(ValidationError):
    __doc__ = _("""The specified dotted name is not valid.""")


class Unbound(Exception):
    __doc__ = _("""The field is not bound.""")


class IField(IValidatable):
    """Basic Schema Field Interface.

    Fields are used for Interface specifications.  They at least provide
    a title, description and a default value.  You can also
    specify if they are required and/or readonly.

    The Field Interface is also used for validation and specifying
    constraints.

    We want to make it possible for a IField to not only work
    on its value but also on the object this value is bound to.
    This enables a Field implementation to perform validation
    against an object which also marks a certain place.

    Note that many fields need information about the object
    containing a field. For example, when validating a value to be
    set as an object attribute, it may be necessary for the field to
    introspect the object's state. This means that the field needs to
    have access to the object when performing validation::

         bound = field.bind(object)
         bound.validate(value)

    """

    def bind(object):
        """Return a copy of this field which is bound to context.

        The copy of the Field will have the 'context' attribute set
        to 'object'.  This way a Field can implement more complex
        checks involving the object's location/environment.

        Many fields don't need to be bound. Only fields that condition
        validation or properties on an object containing the field
        need to be bound.
        """

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

    description = Text(
        title=_("Description"),
        description=_("A description of the field"),
        default=u"",
        required=False,
        )

    required = Bool(
        title=_("Required"),
        description=(_("Tells whether a field requires its value to exist.")),
        default=True)

    readonly = Bool(
        title=_("Read Only"),
        description=_("If true, the field's value cannot be changed."),
        required=False,
        default=False)

    default = Field(
        title=_("Default Value"),
        description=_("""The field default value may be None or a legal
                        field value""")
        )

    missing_value = Field(
        title=_("Missing Value"),
        description=_("""If input for this Field is missing, and that's ok,
                          then this is the value to use""")
        )

    order = Int(
        title=_("Field Order"),
        description=_("""
        The order attribute can be used to determine the order in
        which fields in a schema were defined. If one field is created
        after another (in the same thread), its order will be
        greater.

        (Fields in separate threads could have the same order.)
        """),
        required=True,
        readonly=True,
        )

    def constraint(value):
        """Check a customized constraint on the value.

        You can implement this method with your Field to
        require a certain constraint.  This relaxes the need
        to inherit/subclass a Field you to add a simple constraint.
        Returns true if the given value is within the Field's constraint.
        """

    def validate(value):
        """Validate that the given value is a valid field value.

        Returns nothing but raises an error if the value is invalid.
        It checks everything specific to a Field and also checks
        with the additional constraint.
        """

    def get(object):
        """Get the value of the field for the given object."""

    def query(object, default=None):
        """Query the value of the field for the given object.

        Return the default if the value hasn't been set.
        """

    def set(object, value):
        """Set the value of the field for the object

        Raises a type error if the field is a read-only field.
        """


class IIterable(IField):
    """Fields with a value that can be iterated over.

    The value needs to support iteration; the implementation mechanism
    is not constrained.  (Either `__iter__()` or `__getitem__()` may be
    used.)
    """


class IContainer(IField):
    """Fields whose value allows an ``x in value`` check.

    The value needs to support the `in` operator, but is not
    constrained in how it does so (whether it defines `__contains__()`
    or `__getitem__()` is immaterial).
    """


class IOrderable(IField):
    """Field requiring its value to be orderable.

    The set of value needs support a complete ordering; the
    implementation mechanism is not constrained.  Either `__cmp__()` or
    'rich comparison' methods may be used.
    """


class ILen(IField):
    """A Field requiring its value to have a length.

    The value needs to have a conventional __len__ method.
    """


class IMinMax(IOrderable):
    """Field requiring its value to be between min and max.

    This implies that the value needs to support the IOrderable interface.
    """

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

    max = Field(
        title=_("End of the range (including the value itself)"),
        required=False,
        default=None
        )


class IMinMaxLen(ILen):
    """Field requiring the length of its value to be within a range"""

    min_length = Int(
        title=_("Minimum length"),
        description=_("""
        Value after whitespace processing cannot have less than
        `min_length` characters (if a string type) or elements (if
        another sequence type). 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 a string type) or
        elements (if another sequence type). If `max_length` is
        ``None``, there is no maximum."""),
        required=False,
        min=0,  # needs to be a positive number
        default=None)


class IInterfaceField(IField):
    """Fields with a value that is an interface (implementing
    zope.interface.Interface)."""


class IBool(IField):
    """Boolean Field."""

    default = Bool(
        title=_("Default Value"),
        description=_("""The field default value may be None or a legal
                        field value""")
        )
    required = Bool(
        title=_("Required"),
        description=(_("Tells whether a field requires its value to exist.")),
        required=False,
        default=False)


class IBytes(IMinMaxLen, IIterable, IField):
    """Field containing a byte string (like the python str).

    The value might be constrained to be with length limits.
    """


class IText(IMinMaxLen, IIterable, IField):
    """Field containing a unicode string."""


# for things which are of the str type on both Python 2 and 3
class INativeString(IText):
    """
    A field that always contains the native `str` type.

    .. versionchanged:: 4.9.0
       This is now a distinct type instead of an alias for either `IText`
       or `IBytes`, depending on the platform.
    """


class IASCII(INativeString):
    """Field containing a 7-bit ASCII string. No characters > DEL
    (chr(127)) are allowed

    The value might be constrained to be with length limits.
    """


class IBytesLine(IBytes):
    """Field containing a byte string without newlines."""


class IASCIILine(IASCII):
    """Field containing a 7-bit ASCII string without newlines."""


class ISourceText(IText):
    """Field for source text of object."""


class ITextLine(IText):
    """Field containing a unicode string without newlines."""


class INativeStringLine(ITextLine):
    """
    A field that always contains the native `str` type, without any newlines.

    .. versionchanged:: 4.9.0
       This is now a distinct type instead of an alias for either `ITextLine`
       or `IBytesLine`, depending on the platform.
    """


class IPassword(ITextLine):
    """Field containing a unicode password string without newlines."""


###
# Numbers
###

##
# Abstract numbers
##

class INumber(IMinMax, IField):
    """
    Field containing a generic number: :class:`numbers.Number`.

    .. seealso:: :class:`zope.schema.Number`
    .. versionadded:: 4.6.0
    """
    min = Number(
        title=_("Start of the range"),
        required=False,
        default=None
        )

    max = Number(
        title=_("End of the range (including the value itself)"),
        required=False,
        default=None
        )

    default = Number(
        title=_("Default Value"),
        description=_("""The field default value may be None or a legal
                        field value""")
        )


class IComplex(INumber):
    """
    Field containing a complex number: :class:`numbers.Complex`.

    .. seealso:: :class:`zope.schema.Real`
    .. versionadded:: 4.6.0
    """
    min = Complex(
        title=_("Start of the range"),
        required=False,
        default=None
        )

    max = Complex(
        title=_("End of the range (including the value itself)"),
        required=False,
        default=None
        )

    default = Complex(
        title=_("Default Value"),
        description=_("""The field default value may be None or a legal
                        field value""")
        )


class IReal(IComplex):
    """
    Field containing a real number: :class:`numbers.IReal`.

    .. seealso:: :class:`zope.schema.Real`
    .. versionadded:: 4.6.0
    """
    min = Real(
        title=_("Start of the range"),
        required=False,
        default=None
        )

    max = Real(
        title=_("End of the range (including the value itself)"),
        required=False,
        default=None
        )

    default = Real(
        title=_("Default Value"),
        description=_("""The field default value may be None or a legal
                        field value""")
        )


class IRational(IReal):
    """
    Field containing a rational number: :class:`numbers.IRational`.

    .. seealso:: :class:`zope.schema.Rational`
    .. versionadded:: 4.6.0
    """

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

    max = Rational(
        title=_("End of the range (including the value itself)"),
        required=False,
        default=None
        )

    default = Rational(
        title=_("Default Value"),
        description=_("""The field default value may be None or a legal
                        field value""")
        )


class IIntegral(IRational):
    """
    Field containing an integral number: class:`numbers.Integral`.

    .. seealso:: :class:`zope.schema.Integral`
    .. versionadded:: 4.6.0
    """
    min = Integral(
        title=_("Start of the range"),
        required=False,
        default=None
        )

    max = Integral(
        title=_("End of the range (including the value itself)"),
        required=False,
        default=None
        )

    default = Integral(
        title=_("Default Value"),
        description=_("""The field default value may be None or a legal
                        field value""")
        )


##
# Concrete numbers
##

class IInt(IIntegral):
    """
    Field containing exactly the native class :class:`int`.

    .. seealso:: :class:`zope.schema.Int`
    """

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

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

    default = Int(
        title=_("Default Value"),
        description=_("""The field default value may be None or a legal
                        field value""")
        )


class IFloat(IReal):
    """
    Field containing exactly the native class :class:`float`.

    :class:`IReal` is a more general interface, allowing all of
    floats, ints, and fractions.

    .. seealso:: :class:`zope.schema.Float`
    """


class IDecimal(INumber):
    """Field containing a :class:`decimal.Decimal`"""

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

    max = Decimal(
        title=_("End of the range (including the value itself)"),
        required=False,
        default=None
        )

    default = Decimal(
        title=_("Default Value"),
        description=_("""The field default value may be None or a legal
                        field value""")
        )

###
# End numbers
###


class IDatetime(IMinMax, IField):
    """Field containing a datetime."""


class IDate(IMinMax, IField):
    """Field containing a date."""


class ITimedelta(IMinMax, IField):
    """Field containing a timedelta."""


class ITime(IMinMax, IField):
    """Field containing a time."""


def _is_field(value):
    if not IField.providedBy(value):
        return False
    return True


def _fields(values):
    for value in values:
        if not _is_field(value):
            return False
    return True


class IURI(INativeStringLine):
    """A field containing an absolute URI
    """


class IId(INativeStringLine):
    """A field containing a unique identifier

    A unique identifier is either an absolute URI or a dotted name.
    If it's a dotted name, it should have a module/package name as a prefix.
    """


class IDottedName(INativeStringLine):
    """Dotted name field.

    Values of DottedName fields must be Python-style dotted names.
    """

    min_dots = Int(
        title=_("Minimum number of dots"),
        required=True,
        min=0,
        default=0
        )

    max_dots = Int(
        title=_("Maximum number of dots (should not be less than min_dots)"),
        required=False,
        default=None
        )


class IPythonIdentifier(INativeStringLine):
    """
    A single Python identifier, such as a  variable name.

    .. versionadded:: 4.9.0
    """


class IChoice(IField):
    """Field whose value is contained in a predefined set

    Only one, values or vocabulary, may be specified for a given choice.
    """
    vocabulary = Field(
        title=_("Vocabulary or source providing values"),
        description=_("The ISource, IContextSourceBinder or IBaseVocabulary "
                      "object that provides values for this field."),
        required=False,
        default=None
        )

    vocabularyName = TextLine(
        title=_("Vocabulary name"),
        description=_("Vocabulary name to lookup in the vocabulary registry"),
        required=False,
        default=None
        )


# Collections:

# Abstract

class ICollection(IMinMaxLen, IIterable, IContainer):
    """Abstract interface containing a collection value.

    The Value must be iterable and may have a min_length/max_length.
    """

    value_type = Object(
        IField,
        title=_("Value Type"),
        description=_("Field value items must conform to the given type, "
                      "expressed via a Field."))

    unique = Bool(
        title=_('Unique Members'),
        description=_('Specifies whether the members of the collection '
                      'must be unique.'),
        default=False)


class ISequence(ICollection):
    """Abstract interface specifying that the value is ordered"""


class IMutableSequence(ISequence):
    """
    Abstract interface specifying that the value is ordered and
    mutable.

    .. versionadded:: 4.6.0
    """


class IUnorderedCollection(ICollection):
    """Abstract interface specifying that the value cannot be ordered"""


class IAbstractSet(IUnorderedCollection):
    """An unordered collection of unique values."""

    unique = Bool(
        description="This ICollection interface attribute must be True")


class IAbstractBag(IUnorderedCollection):
    """An unordered collection of values, with no limitations on whether
    members are unique"""

    unique = Bool(
        description="This ICollection interface attribute must be False")


# Concrete


class ITuple(ISequence):
    """Field containing a value that implements the API of a conventional
    Python tuple."""


class IList(IMutableSequence):
    """Field containing a value that implements the API of a conventional
    Python list."""


class ISet(IAbstractSet):
    """Field containing a value that implements the API of a Python2.4+ set.
    """


class IFrozenSet(IAbstractSet):
    """Field containing a value that implements the API of a conventional
    Python 2.4+ frozenset."""

# (end Collections)


class IObject(IField):
    """
    Field containing an Object value.

    .. versionchanged:: 4.6.0
       Add the *validate_invariants* attribute.
    """

    schema = Object(
        IInterface,
        description=_("The Interface that defines the Fields comprising the "
                      "Object.")
    )

    validate_invariants = Bool(
        title=_("Validate Invariants"),
        description=_("A boolean that says whether "
                      "``schema.validateInvariants`` is called from "
                      "``self.validate()``. The default is true."),
        default=True,
    )


class IMapping(IMinMaxLen, IIterable, IContainer):
    """
    Field containing an instance of :class:`collections.Mapping`.

    The *key_type* and *value_type* fields allow specification
    of restrictions for keys and values contained in the dict.

    """
    key_type = Object(
        IField,
        description=_("Field keys must conform to the given type, expressed "
                      "via a Field.")
    )

    value_type = Object(
        IField,
        description=_("Field values must conform to the given type, expressed "
                      "via a Field.")
    )


class IMutableMapping(IMapping):
    """
    Field containing an instance of :class:`collections.MutableMapping`.
    """


class IDict(IMutableMapping):
    """Field containing a conventional dict.
    """


class ITerm(Interface):
    """Object representing a single value in a vocabulary."""

    value = Attribute(
        "value", "The value used to represent vocabulary term in a field.")


class ITokenizedTerm(ITerm):
    """Object representing a single value in a tokenized vocabulary.
    """

    # Should be a ``zope.schema.ASCIILine``, but `ASCIILine` is not a bootstrap
    # field. `ASCIILine` is a type of NativeString.
    token = Attribute(
        "token",
        """Token which can be used to represent the value on a stream.

        The value of this attribute must be a non-empty 7-bit ``str``.
        Control characters, including newline, are not allowed.
        """)


class ITitledTokenizedTerm(ITokenizedTerm):
    """A tokenized term that includes a title."""

    title = TextLine(title=_("Title"))


class ISource(Interface):
    """A set of values from which to choose

    Sources represent sets of values. They are used to specify the
    source for choice fields.

    Sources can be large (even infinite), in which case, they need to
    be queried to find out what their values are.
    """

    def __contains__(value):
        """Return whether the value is available in this source
        """


class ISourceQueriables(Interface):
    """A collection of objects for querying sources
    """

    def getQueriables():
        """Return an iterable of objects that can be queried

        The returned obects should be two-tuples with:

        - A unicode id

          The id must uniquely identify the queriable object within
          the set of queriable objects. Furthermore, in subsequent
          calls, the same id should be used for a given queriable
          object.

        - A queriable object

          This is an object for which there is a view provided for
          searching for items.

        """


class IContextSourceBinder(Interface):
    def __call__(context):
        """Return a context-bound instance that implements ISource.
        """


class IBaseVocabulary(ISource):
    """Representation of a vocabulary.

    At this most basic level, a vocabulary only need to support a test
    for containment.  This can be implemented either by __contains__()
    or by sequence __getitem__() (the later only being useful for
    vocabularies which are intrinsically ordered).
    """

    def getTerm(value):
        """Return the ITerm object for the term 'value'.

        If 'value' is not a valid term, this method raises LookupError.
        """


class IIterableSource(ISource):
    """Source which supports iteration over allowed values.

    The objects iteration provides must be values from the source.
    """

    def __iter__():
        """Return an iterator which provides the values from the source."""

    def __len__():
        """Return the number of valid values, or sys.maxint."""


# BBB vocabularies are pending deprecation, hopefully in 3.3
class IIterableVocabulary(Interface):
    """Vocabulary which supports iteration over allowed values.

    The objects iteration provides must conform to the ITerm
    interface.
    """

    def __iter__():
        """Return an iterator which provides the terms from the vocabulary."""

    def __len__():
        """Return the number of valid terms, or sys.maxint."""


class IVocabulary(IIterableVocabulary, IBaseVocabulary):
    """Vocabulary which is iterable."""


class IVocabularyTokenized(IVocabulary):
    """Vocabulary that provides support for tokenized representation.

    Terms returned from getTerm() and provided by iteration must
    conform to ITokenizedTerm.
    """

    def getTermByToken(token):
        """Return an ITokenizedTerm for the passed-in token.

        If `token` is not represented in the vocabulary, `LookupError`
        is raised.
        """


class ITreeVocabulary(IVocabularyTokenized, IEnumerableMapping):
    """A tokenized vocabulary with a tree-like structure.

       The tree is implemented as dictionary, with keys being ITokenizedTerm
       terms and the values being similar dictionaries. Leaf values are empty
       dictionaries.
    """


class IVocabularyRegistry(Interface):
    """
    Registry that provides `IBaseVocabulary` objects for specific
    fields.

    The fields of this package use the vocabulary registry that is
    returned from :func:`~.getVocabularyRegistry`. This is a hook
    function; by default it returns an instance of
    :class:`~.VocabularyRegistry`, but the function
    :func:`~.setVocabularyRegistry` can be used to change this.

    In particular, the package `zope.vocabularyregistry
    <https://pypi.org/project/zope.vocabularyregistry/>`_ can be used
    to install a vocabulary registry that uses the :mod:`zope.component`
    architecture.
    """

    def get(context, name):
        """
        Return the vocabulary named *name* for the content object
        *context*.

        When the vocabulary cannot be found, `LookupError` is raised.
        """


class IVocabularyFactory(Interface):
    """
    An object that can create `IBaseVocabulary`.

    Objects that implement this interface can be registered with the
    default :class:`~.VocabularyRegistry` provided by this package.

    Alternatively, `zope.vocabularyregistry
    <https://pypi.org/project/zope.vocabularyregistry/>`_ can be used
    to install a `IVocabularyRegistry` that looks for named utilities
    using :func:`zope.component.getUtility` which provide this
    interface.
    """

    def __call__(context):
        """The *context* provides a location that vocabulary can make use of.
        """


class IFieldEvent(Interface):

    field = Object(
        IField,
        description="The field that has been changed")

    object = Attribute("The object containing the field")


class IFieldUpdatedEvent(IFieldEvent):
    """
    A field has been modified

    Subscribers will get the old and the new value together with the field
    """

    old_value = Attribute("The value of the field before modification")

    new_value = Attribute("The value of the field after modification")