Mercurial > hg
annotate mercurial/thirdparty/attr/validators.py @ 50424:ed052780ad5e
peer: rename makepeer() → _make_peer()
In httppeer and sshpeer, there previously were makepeer() and make_peer(),
which was confusing. Therefore, this changeset renames one of the functions.
makepeer() was the internal function called by make_peer() and some debug
command. This function is renamed to _make_peer().
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Fri, 07 Apr 2023 15:42:49 +0200 |
parents | e1c586b9a43c |
children |
rev | line source |
---|---|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
1 # SPDX-License-Identifier: MIT |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
2 |
34397 | 3 """ |
4 Commonly useful validators. | |
5 """ | |
6 | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
7 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
8 import operator |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
9 import re |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
10 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
11 from contextlib import contextmanager |
34397 | 12 |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
13 from ._config import get_run_validators, set_run_validators |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
14 from ._make import _AndValidator, and_, attrib, attrs |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
15 from .exceptions import NotCallableError |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
16 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
17 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
18 try: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
19 Pattern = re.Pattern |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
20 except AttributeError: # Python <3.7 lacks a Pattern type. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
21 Pattern = type(re.compile("")) |
34397 | 22 |
23 | |
24 __all__ = [ | |
25 "and_", | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
26 "deep_iterable", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
27 "deep_mapping", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
28 "disabled", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
29 "ge", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
30 "get_disabled", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
31 "gt", |
34397 | 32 "in_", |
33 "instance_of", | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
34 "is_callable", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
35 "le", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
36 "lt", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
37 "matches_re", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
38 "max_len", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
39 "min_len", |
34397 | 40 "optional", |
41 "provides", | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
42 "set_disabled", |
34397 | 43 ] |
44 | |
45 | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
46 def set_disabled(disabled): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
47 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
48 Globally disable or enable running validators. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
49 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
50 By default, they are run. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
51 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
52 :param disabled: If ``True``, disable running all validators. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
53 :type disabled: bool |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
54 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
55 .. warning:: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
56 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
57 This function is not thread-safe! |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
58 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
59 .. versionadded:: 21.3.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
60 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
61 set_run_validators(not disabled) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
62 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
63 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
64 def get_disabled(): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
65 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
66 Return a bool indicating whether validators are currently disabled or not. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
67 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
68 :return: ``True`` if validators are currently disabled. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
69 :rtype: bool |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
70 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
71 .. versionadded:: 21.3.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
72 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
73 return not get_run_validators() |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
74 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
75 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
76 @contextmanager |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
77 def disabled(): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
78 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
79 Context manager that disables running validators within its context. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
80 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
81 .. warning:: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
82 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
83 This context manager is not thread-safe! |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
84 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
85 .. versionadded:: 21.3.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
86 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
87 set_run_validators(False) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
88 try: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
89 yield |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
90 finally: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
91 set_run_validators(True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
92 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
93 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
94 @attrs(repr=False, slots=True, hash=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
95 class _InstanceOfValidator: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
96 type = attrib() |
34397 | 97 |
98 def __call__(self, inst, attr, value): | |
99 """ | |
100 We use a callable class to be able to change the ``__repr__``. | |
101 """ | |
102 if not isinstance(value, self.type): | |
103 raise TypeError( | |
104 "'{name}' must be {type!r} (got {value!r} that is a " | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
105 "{actual!r}).".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
106 name=attr.name, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
107 type=self.type, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
108 actual=value.__class__, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
109 value=value, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
110 ), |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
111 attr, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
112 self.type, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
113 value, |
34397 | 114 ) |
115 | |
116 def __repr__(self): | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
117 return "<instance_of validator for type {type!r}>".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
118 type=self.type |
34397 | 119 ) |
120 | |
121 | |
122 def instance_of(type): | |
123 """ | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
124 A validator that raises a `TypeError` if the initializer is called |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
125 with a wrong type for this particular attribute (checks are performed using |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
126 `isinstance` therefore it's also valid to pass a tuple of types). |
34397 | 127 |
128 :param type: The type to check for. | |
129 :type type: type or tuple of types | |
130 | |
131 :raises TypeError: With a human readable error message, the attribute | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
132 (of type `attrs.Attribute`), the expected type, and the value it |
34397 | 133 got. |
134 """ | |
135 return _InstanceOfValidator(type) | |
136 | |
137 | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
138 @attrs(repr=False, frozen=True, slots=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
139 class _MatchesReValidator: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
140 pattern = attrib() |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
141 match_func = attrib() |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
142 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
143 def __call__(self, inst, attr, value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
144 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
145 We use a callable class to be able to change the ``__repr__``. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
146 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
147 if not self.match_func(value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
148 raise ValueError( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
149 "'{name}' must match regex {pattern!r}" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
150 " ({value!r} doesn't)".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
151 name=attr.name, pattern=self.pattern.pattern, value=value |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
152 ), |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
153 attr, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
154 self.pattern, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
155 value, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
156 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
157 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
158 def __repr__(self): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
159 return "<matches_re validator for pattern {pattern!r}>".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
160 pattern=self.pattern |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
161 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
162 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
163 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
164 def matches_re(regex, flags=0, func=None): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
165 r""" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
166 A validator that raises `ValueError` if the initializer is called |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
167 with a string that doesn't match *regex*. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
168 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
169 :param regex: a regex string or precompiled pattern to match against |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
170 :param int flags: flags that will be passed to the underlying re function |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
171 (default 0) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
172 :param callable func: which underlying `re` function to call. Valid options |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
173 are `re.fullmatch`, `re.search`, and `re.match`; the default ``None`` |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
174 means `re.fullmatch`. For performance reasons, the pattern is always |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
175 precompiled using `re.compile`. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
176 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
177 .. versionadded:: 19.2.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
178 .. versionchanged:: 21.3.0 *regex* can be a pre-compiled pattern. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
179 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
180 valid_funcs = (re.fullmatch, None, re.search, re.match) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
181 if func not in valid_funcs: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
182 raise ValueError( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
183 "'func' must be one of {}.".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
184 ", ".join( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
185 sorted( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
186 e and e.__name__ or "None" for e in set(valid_funcs) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
187 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
188 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
189 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
190 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
191 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
192 if isinstance(regex, Pattern): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
193 if flags: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
194 raise TypeError( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
195 "'flags' can only be used with a string pattern; " |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
196 "pass flags to re.compile() instead" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
197 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
198 pattern = regex |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
199 else: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
200 pattern = re.compile(regex, flags) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
201 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
202 if func is re.match: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
203 match_func = pattern.match |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
204 elif func is re.search: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
205 match_func = pattern.search |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
206 else: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
207 match_func = pattern.fullmatch |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
208 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
209 return _MatchesReValidator(pattern, match_func) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
210 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
211 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
212 @attrs(repr=False, slots=True, hash=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
213 class _ProvidesValidator: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
214 interface = attrib() |
34397 | 215 |
216 def __call__(self, inst, attr, value): | |
217 """ | |
218 We use a callable class to be able to change the ``__repr__``. | |
219 """ | |
220 if not self.interface.providedBy(value): | |
221 raise TypeError( | |
222 "'{name}' must provide {interface!r} which {value!r} " | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
223 "doesn't.".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
224 name=attr.name, interface=self.interface, value=value |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
225 ), |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
226 attr, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
227 self.interface, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
228 value, |
34397 | 229 ) |
230 | |
231 def __repr__(self): | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
232 return "<provides validator for interface {interface!r}>".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
233 interface=self.interface |
34397 | 234 ) |
235 | |
236 | |
237 def provides(interface): | |
238 """ | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
239 A validator that raises a `TypeError` if the initializer is called |
34397 | 240 with an object that does not provide the requested *interface* (checks are |
241 performed using ``interface.providedBy(value)`` (see `zope.interface | |
242 <https://zopeinterface.readthedocs.io/en/latest/>`_). | |
243 | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
244 :param interface: The interface to check for. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
245 :type interface: ``zope.interface.Interface`` |
34397 | 246 |
247 :raises TypeError: With a human readable error message, the attribute | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
248 (of type `attrs.Attribute`), the expected interface, and the |
34397 | 249 value it got. |
250 """ | |
251 return _ProvidesValidator(interface) | |
252 | |
253 | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
254 @attrs(repr=False, slots=True, hash=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
255 class _OptionalValidator: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
256 validator = attrib() |
34397 | 257 |
258 def __call__(self, inst, attr, value): | |
259 if value is None: | |
260 return | |
261 | |
262 self.validator(inst, attr, value) | |
263 | |
264 def __repr__(self): | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
265 return "<optional validator for {what} or None>".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
266 what=repr(self.validator) |
34397 | 267 ) |
268 | |
269 | |
270 def optional(validator): | |
271 """ | |
272 A validator that makes an attribute optional. An optional attribute is one | |
273 which can be set to ``None`` in addition to satisfying the requirements of | |
274 the sub-validator. | |
275 | |
276 :param validator: A validator (or a list of validators) that is used for | |
277 non-``None`` values. | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
278 :type validator: callable or `list` of callables. |
34397 | 279 |
280 .. versionadded:: 15.1.0 | |
281 .. versionchanged:: 17.1.0 *validator* can be a list of validators. | |
282 """ | |
283 if isinstance(validator, list): | |
284 return _OptionalValidator(_AndValidator(validator)) | |
285 return _OptionalValidator(validator) | |
286 | |
287 | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
288 @attrs(repr=False, slots=True, hash=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
289 class _InValidator: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
290 options = attrib() |
34397 | 291 |
292 def __call__(self, inst, attr, value): | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
293 try: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
294 in_options = value in self.options |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
295 except TypeError: # e.g. `1 in "abc"` |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
296 in_options = False |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
297 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
298 if not in_options: |
34397 | 299 raise ValueError( |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
300 "'{name}' must be in {options!r} (got {value!r})".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
301 name=attr.name, options=self.options, value=value |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
302 ), |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
303 attr, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
304 self.options, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
305 value, |
34397 | 306 ) |
307 | |
308 def __repr__(self): | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
309 return "<in_ validator with options {options!r}>".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
310 options=self.options |
34397 | 311 ) |
312 | |
313 | |
314 def in_(options): | |
315 """ | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
316 A validator that raises a `ValueError` if the initializer is called |
34397 | 317 with a value that does not belong in the options provided. The check is |
318 performed using ``value in options``. | |
319 | |
320 :param options: Allowed options. | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
321 :type options: list, tuple, `enum.Enum`, ... |
34397 | 322 |
323 :raises ValueError: With a human readable error message, the attribute (of | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
324 type `attrs.Attribute`), the expected options, and the value it |
34397 | 325 got. |
326 | |
327 .. versionadded:: 17.1.0 | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
328 .. versionchanged:: 22.1.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
329 The ValueError was incomplete until now and only contained the human |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
330 readable error message. Now it contains all the information that has |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
331 been promised since 17.1.0. |
34397 | 332 """ |
333 return _InValidator(options) | |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
334 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
335 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
336 @attrs(repr=False, slots=False, hash=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
337 class _IsCallableValidator: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
338 def __call__(self, inst, attr, value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
339 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
340 We use a callable class to be able to change the ``__repr__``. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
341 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
342 if not callable(value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
343 message = ( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
344 "'{name}' must be callable " |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
345 "(got {value!r} that is a {actual!r})." |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
346 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
347 raise NotCallableError( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
348 msg=message.format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
349 name=attr.name, value=value, actual=value.__class__ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
350 ), |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
351 value=value, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
352 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
353 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
354 def __repr__(self): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
355 return "<is_callable validator>" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
356 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
357 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
358 def is_callable(): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
359 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
360 A validator that raises a `attr.exceptions.NotCallableError` if the |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
361 initializer is called with a value for this particular attribute |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
362 that is not callable. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
363 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
364 .. versionadded:: 19.1.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
365 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
366 :raises `attr.exceptions.NotCallableError`: With a human readable error |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
367 message containing the attribute (`attrs.Attribute`) name, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
368 and the value it got. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
369 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
370 return _IsCallableValidator() |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
371 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
372 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
373 @attrs(repr=False, slots=True, hash=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
374 class _DeepIterable: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
375 member_validator = attrib(validator=is_callable()) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
376 iterable_validator = attrib( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
377 default=None, validator=optional(is_callable()) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
378 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
379 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
380 def __call__(self, inst, attr, value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
381 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
382 We use a callable class to be able to change the ``__repr__``. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
383 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
384 if self.iterable_validator is not None: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
385 self.iterable_validator(inst, attr, value) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
386 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
387 for member in value: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
388 self.member_validator(inst, attr, member) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
389 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
390 def __repr__(self): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
391 iterable_identifier = ( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
392 "" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
393 if self.iterable_validator is None |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
394 else " {iterable!r}".format(iterable=self.iterable_validator) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
395 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
396 return ( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
397 "<deep_iterable validator for{iterable_identifier}" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
398 " iterables of {member!r}>" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
399 ).format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
400 iterable_identifier=iterable_identifier, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
401 member=self.member_validator, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
402 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
403 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
404 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
405 def deep_iterable(member_validator, iterable_validator=None): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
406 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
407 A validator that performs deep validation of an iterable. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
408 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
409 :param member_validator: Validator(s) to apply to iterable members |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
410 :param iterable_validator: Validator to apply to iterable itself |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
411 (optional) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
412 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
413 .. versionadded:: 19.1.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
414 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
415 :raises TypeError: if any sub-validators fail |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
416 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
417 if isinstance(member_validator, (list, tuple)): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
418 member_validator = and_(*member_validator) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
419 return _DeepIterable(member_validator, iterable_validator) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
420 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
421 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
422 @attrs(repr=False, slots=True, hash=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
423 class _DeepMapping: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
424 key_validator = attrib(validator=is_callable()) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
425 value_validator = attrib(validator=is_callable()) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
426 mapping_validator = attrib(default=None, validator=optional(is_callable())) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
427 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
428 def __call__(self, inst, attr, value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
429 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
430 We use a callable class to be able to change the ``__repr__``. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
431 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
432 if self.mapping_validator is not None: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
433 self.mapping_validator(inst, attr, value) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
434 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
435 for key in value: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
436 self.key_validator(inst, attr, key) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
437 self.value_validator(inst, attr, value[key]) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
438 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
439 def __repr__(self): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
440 return ( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
441 "<deep_mapping validator for objects mapping {key!r} to {value!r}>" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
442 ).format(key=self.key_validator, value=self.value_validator) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
443 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
444 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
445 def deep_mapping(key_validator, value_validator, mapping_validator=None): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
446 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
447 A validator that performs deep validation of a dictionary. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
448 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
449 :param key_validator: Validator to apply to dictionary keys |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
450 :param value_validator: Validator to apply to dictionary values |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
451 :param mapping_validator: Validator to apply to top-level mapping |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
452 attribute (optional) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
453 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
454 .. versionadded:: 19.1.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
455 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
456 :raises TypeError: if any sub-validators fail |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
457 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
458 return _DeepMapping(key_validator, value_validator, mapping_validator) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
459 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
460 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
461 @attrs(repr=False, frozen=True, slots=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
462 class _NumberValidator: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
463 bound = attrib() |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
464 compare_op = attrib() |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
465 compare_func = attrib() |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
466 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
467 def __call__(self, inst, attr, value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
468 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
469 We use a callable class to be able to change the ``__repr__``. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
470 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
471 if not self.compare_func(value, self.bound): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
472 raise ValueError( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
473 "'{name}' must be {op} {bound}: {value}".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
474 name=attr.name, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
475 op=self.compare_op, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
476 bound=self.bound, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
477 value=value, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
478 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
479 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
480 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
481 def __repr__(self): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
482 return "<Validator for x {op} {bound}>".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
483 op=self.compare_op, bound=self.bound |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
484 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
485 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
486 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
487 def lt(val): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
488 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
489 A validator that raises `ValueError` if the initializer is called |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
490 with a number larger or equal to *val*. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
491 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
492 :param val: Exclusive upper bound for values |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
493 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
494 .. versionadded:: 21.3.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
495 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
496 return _NumberValidator(val, "<", operator.lt) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
497 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
498 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
499 def le(val): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
500 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
501 A validator that raises `ValueError` if the initializer is called |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
502 with a number greater than *val*. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
503 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
504 :param val: Inclusive upper bound for values |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
505 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
506 .. versionadded:: 21.3.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
507 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
508 return _NumberValidator(val, "<=", operator.le) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
509 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
510 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
511 def ge(val): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
512 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
513 A validator that raises `ValueError` if the initializer is called |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
514 with a number smaller than *val*. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
515 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
516 :param val: Inclusive lower bound for values |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
517 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
518 .. versionadded:: 21.3.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
519 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
520 return _NumberValidator(val, ">=", operator.ge) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
521 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
522 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
523 def gt(val): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
524 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
525 A validator that raises `ValueError` if the initializer is called |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
526 with a number smaller or equal to *val*. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
527 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
528 :param val: Exclusive lower bound for values |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
529 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
530 .. versionadded:: 21.3.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
531 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
532 return _NumberValidator(val, ">", operator.gt) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
533 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
534 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
535 @attrs(repr=False, frozen=True, slots=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
536 class _MaxLengthValidator: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
537 max_length = attrib() |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
538 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
539 def __call__(self, inst, attr, value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
540 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
541 We use a callable class to be able to change the ``__repr__``. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
542 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
543 if len(value) > self.max_length: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
544 raise ValueError( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
545 "Length of '{name}' must be <= {max}: {len}".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
546 name=attr.name, max=self.max_length, len=len(value) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
547 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
548 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
549 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
550 def __repr__(self): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
551 return "<max_len validator for {max}>".format(max=self.max_length) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
552 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
553 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
554 def max_len(length): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
555 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
556 A validator that raises `ValueError` if the initializer is called |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
557 with a string or iterable that is longer than *length*. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
558 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
559 :param int length: Maximum length of the string or iterable |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
560 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
561 .. versionadded:: 21.3.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
562 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
563 return _MaxLengthValidator(length) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
564 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
565 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
566 @attrs(repr=False, frozen=True, slots=True) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
567 class _MinLengthValidator: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
568 min_length = attrib() |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
569 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
570 def __call__(self, inst, attr, value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
571 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
572 We use a callable class to be able to change the ``__repr__``. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
573 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
574 if len(value) < self.min_length: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
575 raise ValueError( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
576 "Length of '{name}' must be => {min}: {len}".format( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
577 name=attr.name, min=self.min_length, len=len(value) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
578 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
579 ) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
580 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
581 def __repr__(self): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
582 return "<min_len validator for {min}>".format(min=self.min_length) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
583 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
584 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
585 def min_len(length): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
586 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
587 A validator that raises `ValueError` if the initializer is called |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
588 with a string or iterable that is shorter than *length*. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
589 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
590 :param int length: Minimum length of the string or iterable |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
591 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
592 .. versionadded:: 22.1.0 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
593 """ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
594 return _MinLengthValidator(length) |