author | robin <robin@reportlab.com> |
Tue, 07 Mar 2017 10:00:34 +0000 | |
changeset 4330 | 617ffa6bbdc8 |
parent 4252 | fe660f227cac |
child 4367 | 9960d82643bf |
permissions | -rw-r--r-- |
4330 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2017 |
1559 | 2 |
#see license.txt for license details |
2332 | 3 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/validators.py |
4252 | 4 |
__version__='3.3.0' |
3029 | 5 |
__doc__="""Standard verifying functions used by attrmap.""" |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
6 |
|
3824 | 7 |
import sys, codecs |
3835 | 8 |
from reportlab.lib.utils import isSeq, isBytes, isStr, isPy3 |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
9 |
from reportlab.lib import colors |
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
10 |
|
3625 | 11 |
class Percentage(float): |
12 |
pass |
|
13 |
||
945 | 14 |
class Validator: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
15 |
"base validator class" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
16 |
def __call__(self,x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
17 |
return self.test(x) |
945 | 18 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
19 |
def __str__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
20 |
return getattr(self,'_str',self.__class__.__name__) |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
21 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
22 |
def normalize(self,x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
23 |
return x |
931 | 24 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
25 |
def normalizeTest(self,x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
26 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
27 |
self.normalize(x) |
2120 | 28 |
return True |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
29 |
except: |
2120 | 30 |
return False |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
31 |
|
948 | 32 |
class _isAnything(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
33 |
def test(self,x): |
2120 | 34 |
return True |
948 | 35 |
|
36 |
class _isNothing(Validator): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
37 |
def test(self,x): |
2120 | 38 |
return False |
948 | 39 |
|
40 |
class _isBoolean(Validator): |
|
3762 | 41 |
def test(self,x): |
42 |
if isinstance(int,bool): return x in (0,1) |
|
43 |
return self.normalizeTest(x) |
|
931 | 44 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
45 |
def normalize(self,x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
46 |
if x in (0,1): return x |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
47 |
try: |
3824 | 48 |
S = x.upper() |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
49 |
except: |
3824 | 50 |
raise ValueError('Must be boolean not %s' % ascii(s)) |
2120 | 51 |
if S in ('YES','TRUE'): return True |
52 |
if S in ('NO','FALSE',None): return False |
|
3824 | 53 |
raise ValueError('Must be boolean not %s' % ascii(s)) |
909
a5ee7d2bdb17
Extracted validator functions into lib.validators.
dinu_gherman
parents:
908
diff
changeset
|
54 |
|
948 | 55 |
class _isString(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
56 |
def test(self,x): |
3908 | 57 |
return isStr(x) |
931 | 58 |
|
2616 | 59 |
class _isCodec(Validator): |
60 |
def test(self,x): |
|
3762 | 61 |
if not isStr(x): |
2616 | 62 |
return False |
63 |
try: |
|
64 |
a,b,c,d = codecs.lookup(x) |
|
65 |
return True |
|
66 |
except LookupError: |
|
67 |
return False |
|
68 |
||
948 | 69 |
class _isNumber(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
70 |
def test(self,x): |
3762 | 71 |
if isinstance(x,(float,int)): return True |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
72 |
return self.normalizeTest(x) |
931 | 73 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
74 |
def normalize(self,x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
75 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
76 |
return float(x) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
77 |
except: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
78 |
return int(x) |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
79 |
|
1055
30ca3865f6ed
Added isInt and changed SequenceOf and OneOf to be more friendly
rgbecker
parents:
1022
diff
changeset
|
80 |
class _isInt(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
81 |
def test(self,x): |
3824 | 82 |
if not isinstance(x,int) and not isStr(x): return False |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
83 |
return self.normalizeTest(x) |
1055
30ca3865f6ed
Added isInt and changed SequenceOf and OneOf to be more friendly
rgbecker
parents:
1022
diff
changeset
|
84 |
|
3835 | 85 |
if not isPy3: |
86 |
def normalize(self,x): |
|
87 |
return int(x) |
|
88 |
else: |
|
89 |
def normalize(self,x): |
|
90 |
return int(x.decode('utf8') if isBytes(x) else x) |
|
1683 | 91 |
|
948 | 92 |
class _isNumberOrNone(_isNumber): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
93 |
def test(self,x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
94 |
return x is None or isNumber(x) |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
95 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
96 |
def normalize(self,x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
97 |
if x is None: return x |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
98 |
return _isNumber.normalize(x) |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
99 |
|
948 | 100 |
class _isListOfNumbersOrNone(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
101 |
"ListOfNumbersOrNone validator class." |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
102 |
def test(self, x): |
2120 | 103 |
if x is None: return True |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
104 |
return isListOfNumbers(x) |
931 | 105 |
|
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
106 |
class isNumberInRange(_isNumber): |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
107 |
def __init__(self, min, max): |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
108 |
self.min = min |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
109 |
self.max = max |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
110 |
|
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
111 |
def test(self, x): |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
112 |
try: |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
113 |
n = self.normalize(x) |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
114 |
if self.min <= n <= self.max: |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
115 |
return True |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
116 |
except ValueError: |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
117 |
pass |
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
118 |
return False |
3219
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
119 |
|
3198
683ca9eb6b18
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3029
diff
changeset
|
120 |
|
948 | 121 |
class _isListOfShapes(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
122 |
"ListOfShapes validator class." |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
123 |
def test(self, x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
124 |
from reportlab.graphics.shapes import Shape |
3731 | 125 |
if isSeq(x): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
126 |
answer = 1 |
2775 | 127 |
for e in x: |
128 |
if not isinstance(e, Shape): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
129 |
answer = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
130 |
return answer |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
131 |
else: |
2120 | 132 |
return False |
931 | 133 |
|
948 | 134 |
class _isListOfStringsOrNone(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
135 |
"ListOfStringsOrNone validator class." |
931 | 136 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
137 |
def test(self, x): |
2120 | 138 |
if x is None: return True |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
139 |
return isListOfStrings(x) |
931 | 140 |
|
948 | 141 |
class _isTransform(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
142 |
"Transform validator class." |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
143 |
def test(self, x): |
3731 | 144 |
if isSeq(x): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
145 |
if len(x) == 6: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
146 |
for element in x: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
147 |
if not isNumber(element): |
2120 | 148 |
return False |
149 |
return True |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
150 |
else: |
2120 | 151 |
return False |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
152 |
else: |
2120 | 153 |
return False |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
154 |
|
948 | 155 |
class _isColor(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
156 |
"Color validator class." |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
157 |
def test(self, x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
158 |
return isinstance(x, colors.Color) |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
159 |
|
948 | 160 |
class _isColorOrNone(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
161 |
"ColorOrNone validator class." |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
162 |
def test(self, x): |
2120 | 163 |
if x is None: return True |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
164 |
return isColor(x) |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
165 |
|
3339 | 166 |
from reportlab.lib.normalDate import NormalDate |
167 |
class _isNormalDate(Validator): |
|
168 |
def test(self,x): |
|
169 |
if isinstance(x,NormalDate): |
|
170 |
return True |
|
3418 | 171 |
return x is not None and self.normalizeTest(x) |
3339 | 172 |
|
173 |
def normalize(self,x): |
|
174 |
return NormalDate(x) |
|
175 |
||
948 | 176 |
class _isValidChild(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
177 |
"ValidChild validator class." |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
178 |
def test(self, x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
179 |
"""Is this child allowed in a drawing or group? |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
180 |
I.e. does it descend from Shape or UserNode? |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
181 |
""" |
931 | 182 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
183 |
from reportlab.graphics.shapes import UserNode, Shape |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
184 |
return isinstance(x, UserNode) or isinstance(x, Shape) |
931 | 185 |
|
1083 | 186 |
class _isValidChildOrNone(_isValidChild): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
187 |
def test(self,x): |
2575 | 188 |
return _isValidChild.test(self,x) or x is None |
1083 | 189 |
|
1391 | 190 |
class _isCallable(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
191 |
def test(self, x): |
3326 | 192 |
return hasattr(x,'__call__') |
1391 | 193 |
|
931 | 194 |
class OneOf(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
195 |
"""Make validator functions for list of choices. |
931 | 196 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
197 |
Usage: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
198 |
f = reportlab.lib.validators.OneOf('happy','sad') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
199 |
or |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
200 |
f = reportlab.lib.validators.OneOf(('happy','sad')) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
201 |
f('sad'),f('happy'), f('grumpy') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
202 |
(1,1,0) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
203 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
204 |
def __init__(self, enum,*args): |
3762 | 205 |
if isSeq(enum): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
206 |
if args!=(): |
3721 | 207 |
raise ValueError("Either all singleton args or a single sequence argument") |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
208 |
self._enum = tuple(enum)+args |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
209 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
210 |
self._enum = (enum,)+args |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
211 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
212 |
def test(self, x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
213 |
return x in self._enum |
931 | 214 |
|
945 | 215 |
class SequenceOf(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
216 |
def __init__(self,elemTest,name=None,emptyOK=1, NoneOK=0, lo=0,hi=0x7fffffff): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
217 |
self._elemTest = elemTest |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
218 |
self._emptyOK = emptyOK |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
219 |
self._NoneOK = NoneOK |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
220 |
self._lo, self._hi = lo, hi |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
221 |
if name: self._str = name |
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
222 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
223 |
def test(self, x): |
3731 | 224 |
if not isSeq(x): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
225 |
if x is None: return self._NoneOK |
2120 | 226 |
return False |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
227 |
if x==[] or x==(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
228 |
return self._emptyOK |
2120 | 229 |
elif not self._lo<=len(x)<=self._hi: return False |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
230 |
for e in x: |
2120 | 231 |
if not self._elemTest(e): return False |
232 |
return True |
|
908
3564ccbf97e6
Added future validators module (stripped off from shapes.py).
dinu_gherman
parents:
diff
changeset
|
233 |
|
1385 | 234 |
class EitherOr(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
235 |
def __init__(self,tests,name=None): |
3731 | 236 |
if not isSeq(tests): tests = (tests,) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
237 |
self._tests = tests |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
238 |
if name: self._str = name |
1385 | 239 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
240 |
def test(self, x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
241 |
for t in self._tests: |
2120 | 242 |
if t(x): return True |
243 |
return False |
|
1385 | 244 |
|
1197 | 245 |
class NoneOr(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
246 |
def __init__(self,elemTest,name=None): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
247 |
self._elemTest = elemTest |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
248 |
if name: self._str = name |
1197 | 249 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
250 |
def test(self, x): |
2120 | 251 |
if x is None: return True |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
252 |
return self._elemTest(x) |
1197 | 253 |
|
2424 | 254 |
class Auto(Validator): |
2426 | 255 |
def __init__(self,**kw): |
256 |
self.__dict__.update(kw) |
|
257 |
||
2424 | 258 |
def test(self,x): |
2426 | 259 |
return x is self.__class__ or isinstance(x,self.__class__) |
2424 | 260 |
|
2421 | 261 |
class AutoOr(NoneOr): |
262 |
def test(self,x): |
|
2426 | 263 |
return isAuto(x) or self._elemTest(x) |
2421 | 264 |
|
1248 | 265 |
class isInstanceOf(Validator): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
266 |
def __init__(self,klass=None): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
267 |
self._klass = klass |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
268 |
def test(self,x): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1559
diff
changeset
|
269 |
return isinstance(x,self._klass) |
1248 | 270 |
|
2573 | 271 |
class matchesPattern(Validator): |
272 |
"""Matches value, or its string representation, against regex""" |
|
273 |
def __init__(self, pattern): |
|
274 |
self._pattern = re.compile(pattern) |
|
275 |
||
276 |
def test(self,x): |
|
3762 | 277 |
x = str(x) |
3721 | 278 |
print('testing %s against %s' % (x, self._pattern)) |
3762 | 279 |
return (self._pattern.match(x) != None) |
2573 | 280 |
|
2547 | 281 |
class DerivedValue: |
282 |
"""This is used for magic values which work themselves out. |
|
283 |
An example would be an "inherit" property, so that one can have |
|
284 |
||
285 |
drawing.chart.categoryAxis.labels.fontName = inherit |
|
286 |
||
287 |
and pick up the value from the top of the drawing. |
|
288 |
Validators will permit this provided that a value can be pulled |
|
289 |
in which satisfies it. And the renderer will have special |
|
290 |
knowledge of these so they can evaluate themselves. |
|
291 |
""" |
|
292 |
def getValue(self, renderer, attr): |
|
293 |
"""Override this. The renderers will pass the renderer, |
|
294 |
and the attribute name. Algorithms can then backtrack up |
|
295 |
through all the stuff the renderer provides, including |
|
296 |
a correct stack of parent nodes.""" |
|
297 |
return None |
|
298 |
||
299 |
class Inherit(DerivedValue): |
|
300 |
def __repr__(self): |
|
301 |
return "inherit" |
|
3219
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
302 |
|
2547 | 303 |
def getValue(self, renderer, attr): |
304 |
return renderer.getStateValue(attr) |
|
3219
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
305 |
inherit = Inherit() |
2547 | 306 |
|
3219
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
307 |
class NumericAlign(str): |
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
308 |
'''for creating the numeric string value for anchors etc etc |
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
309 |
dp is the character to align on (the last occurrence will be used) |
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
310 |
dpLen is the length of characters after the dp |
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
311 |
''' |
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
312 |
def __new__(cls,dp='.',dpLen=0): |
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
313 |
self = str.__new__(cls,'numeric') |
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
314 |
self._dp=dp |
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
315 |
self._dpLen = dpLen |
b28d6eef8227
reportlab: add support for String anchor 'numeric'
rgbecker
parents:
3198
diff
changeset
|
316 |
return self |
2547 | 317 |
|
2424 | 318 |
isAuto = Auto() |
948 | 319 |
isBoolean = _isBoolean() |
320 |
isString = _isString() |
|
2616 | 321 |
isCodec = _isCodec() |
948 | 322 |
isNumber = _isNumber() |
1055
30ca3865f6ed
Added isInt and changed SequenceOf and OneOf to be more friendly
rgbecker
parents:
1022
diff
changeset
|
323 |
isInt = _isInt() |
1365 | 324 |
isNoneOrInt = NoneOr(isInt,'isNoneOrInt') |
948 | 325 |
isNumberOrNone = _isNumberOrNone() |
1807 | 326 |
isTextAnchor = OneOf('start','middle','end','boxauto') |
945 | 327 |
isListOfNumbers = SequenceOf(isNumber,'isListOfNumbers') |
948 | 328 |
isListOfNumbersOrNone = _isListOfNumbersOrNone() |
329 |
isListOfShapes = _isListOfShapes() |
|
945 | 330 |
isListOfStrings = SequenceOf(isString,'isListOfStrings') |
948 | 331 |
isListOfStringsOrNone = _isListOfStringsOrNone() |
332 |
isTransform = _isTransform() |
|
333 |
isColor = _isColor() |
|
1022 | 334 |
isListOfColors = SequenceOf(isColor,'isListOfColors') |
948 | 335 |
isColorOrNone = _isColorOrNone() |
1694 | 336 |
isShape = isValidChild = _isValidChild() |
337 |
isNoneOrShape = isValidChildOrNone = _isValidChildOrNone() |
|
948 | 338 |
isAnything = _isAnything() |
339 |
isNothing = _isNothing() |
|
1059 | 340 |
isXYCoord = SequenceOf(isNumber,lo=2,hi=2,emptyOK=0) |
2174 | 341 |
isBoxAnchor = OneOf('nw','n','ne','w','c','e','sw','s','se', 'autox', 'autoy') |
1197 | 342 |
isNoneOrString = NoneOr(isString,'NoneOrString') |
1199 | 343 |
isNoneOrListOfNoneOrStrings=SequenceOf(isNoneOrString,'isNoneOrListOfNoneOrStrings',NoneOK=1) |
1353 | 344 |
isListOfNoneOrString=SequenceOf(isNoneOrString,'isListOfNoneOrString',NoneOK=0) |
1199 | 345 |
isNoneOrListOfNoneOrNumbers=SequenceOf(isNumberOrNone,'isNoneOrListOfNoneOrNumbers',NoneOK=1) |
1391 | 346 |
isCallable = _isCallable() |
3645 | 347 |
isNoneOrCallable = NoneOr(isCallable) |
1391 | 348 |
isStringOrCallable=EitherOr((isString,isCallable),'isStringOrCallable') |
1694 | 349 |
isStringOrCallableOrNone=NoneOr(isStringOrCallable,'isStringOrCallableNone') |
1963 | 350 |
isStringOrNone=NoneOr(isString,'isStringOrNone') |
3339 | 351 |
isNormalDate=_isNormalDate() |