10
10
XSI_NIL = '{%s}nil' % XSI_NAMESPACE
11
11
# ---------------------------------------------------------
12
12
13
+
13
14
class NotValid (Exception ):
14
15
pass
15
16
17
+
16
18
class OutsideCardinality (Exception ):
17
19
pass
18
20
21
+
19
22
class MustValueError (ValueError ):
20
23
pass
21
24
25
+
22
26
class ShouldValueError (ValueError ):
23
27
pass
24
28
@@ -27,6 +31,7 @@ class ShouldValueError(ValueError):
27
31
28
32
NCNAME = re .compile ("(?P<NCName>[a-zA-Z_](\w|[_.-])*)" )
29
33
34
+
30
35
def valid_ncname (name ):
31
36
match = NCNAME .match (name )
32
37
if not match :
@@ -45,7 +50,7 @@ def valid_any_uri(item):
45
50
except Exception :
46
51
raise NotValid ("AnyURI" )
47
52
48
- if part [0 ] == "urn" and part [1 ] == "" : # A urn
53
+ if part [0 ] == "urn" and part [1 ] == "" : # A urn
49
54
return True
50
55
# elif part[1] == "localhost" or part[1] == "127.0.0.1":
51
56
# raise NotValid("AnyURI")
@@ -83,6 +88,7 @@ def validate_on_or_after(not_on_or_after, slack):
83
88
else :
84
89
return False
85
90
91
+
86
92
def validate_before (not_before , slack ):
87
93
if not_before :
88
94
now = time_util .utc_now ()
@@ -92,11 +98,13 @@ def validate_before(not_before, slack):
92
98
93
99
return True
94
100
101
+
95
102
def valid_address (address ):
96
103
if not (valid_ipv4 (address ) or valid_ipv6 (address )):
97
104
raise NotValid ("address" )
98
105
return True
99
106
107
+
100
108
def valid_ipv4 (address ):
101
109
parts = address .split ("." )
102
110
if len (parts ) != 4 :
@@ -137,24 +145,28 @@ def valid_ipv4(address):
137
145
$
138
146
""" , re .VERBOSE | re .IGNORECASE | re .DOTALL )
139
147
148
+
140
149
def valid_ipv6 (address ):
141
150
"""Validates IPv6 addresses. """
142
151
return IPV6_PATTERN .match (address ) is not None
143
152
153
+
144
154
def valid_boolean (val ):
145
155
vall = val .lower ()
146
156
if vall in ["true" , "false" , "0" , "1" ]:
147
157
return True
148
158
else :
149
159
raise NotValid ("boolean" )
150
160
161
+
151
162
def valid_duration (val ):
152
163
try :
153
164
time_util .parse_duration (val )
154
165
except Exception :
155
166
raise NotValid ("duration" )
156
167
return True
157
168
169
+
158
170
def valid_string (val ):
159
171
""" Expects unicode
160
172
Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
@@ -167,16 +179,17 @@ def valid_string(val):
167
179
raise NotValid ("string" )
168
180
if char == 0x09 or char == 0x0A or char == 0x0D :
169
181
continue
170
- elif char >= 0x20 and char <= 0xD7FF :
182
+ elif 0x20 <= char <= 0xD7FF :
171
183
continue
172
- elif char >= 0xE000 and char <= 0xFFFD :
184
+ elif 0xE000 <= char <= 0xFFFD :
173
185
continue
174
- elif char >= 0x10000 and char <= 0x10FFFF :
186
+ elif 0x10000 <= char <= 0x10FFFF :
175
187
continue
176
188
else :
177
189
raise NotValid ("string" )
178
190
return True
179
191
192
+
180
193
def valid_unsigned_short (val ):
181
194
try :
182
195
struct .pack ("H" , int (val ))
@@ -187,6 +200,7 @@ def valid_unsigned_short(val):
187
200
188
201
return True
189
202
203
+
190
204
def valid_non_negative_integer (val ):
191
205
try :
192
206
integer = int (val )
@@ -197,20 +211,23 @@ def valid_non_negative_integer(val):
197
211
raise NotValid ("non negative integer" )
198
212
return True
199
213
214
+
200
215
def valid_integer (val ):
201
216
try :
202
217
int (val )
203
218
except ValueError :
204
219
raise NotValid ("integer" )
205
220
return True
206
221
222
+
207
223
def valid_base64 (val ):
208
224
try :
209
225
base64 .b64decode (val )
210
226
except Exception :
211
227
raise NotValid ("base64" )
212
228
return True
213
229
230
+
214
231
def valid_qname (val ):
215
232
""" A qname is either
216
233
NCName or
@@ -223,6 +240,7 @@ def valid_qname(val):
223
240
except ValueError :
224
241
return valid_ncname (val )
225
242
243
+
226
244
def valid_anytype (val ):
227
245
""" Goes through all known type validators
228
246
@@ -261,9 +279,11 @@ def valid_anytype(val):
261
279
262
280
# -----------------------------------------------------------------------------
263
281
282
+
264
283
def validate_value_type (value , spec ):
265
284
"""
266
- c_value_type = {'base': 'string', 'enumeration': ['Permit', 'Deny', 'Indeterminate']}
285
+ c_value_type = {'base': 'string', 'enumeration': ['Permit', 'Deny',
286
+ 'Indeterminate']}
267
287
{'member': 'anyURI', 'base': 'list'}
268
288
{'base': 'anyURI'}
269
289
{'base': 'NCName'}
@@ -278,14 +298,15 @@ def validate_value_type(value, spec):
278
298
raise NotValid ("value not in enumeration" )
279
299
else :
280
300
return valid_string (value )
281
- elif spec ["base" ] == "list" : # comma separated list of values
301
+ elif spec ["base" ] == "list" : # comma separated list of values
282
302
for val in [v .strip () for v in value .split ("," )]:
283
303
valid (spec ["member" ], val )
284
304
else :
285
305
return valid (spec ["base" ], value )
286
306
287
307
return True
288
308
309
+
289
310
def valid (typ , value ):
290
311
try :
291
312
return VALIDATOR [typ ](value )
@@ -297,33 +318,34 @@ def valid(typ, value):
297
318
typ = "string"
298
319
return VALIDATOR [typ ](value )
299
320
321
+
300
322
def _valid_instance (instance , val ):
301
323
try :
302
324
val .verify ()
303
325
except NotValid , exc :
304
- raise NotValid ("Class '%s' instance: %s" % \
305
- (instance .__class__ .__name__ ,
306
- exc .args [0 ]))
326
+ raise NotValid ("Class '%s' instance: %s" % (
327
+ instance .__class__ .__name__ , exc .args [0 ]))
307
328
except OutsideCardinality , exc :
308
329
raise NotValid (
309
- "Class '%s' instance cardinality error: %s" % \
310
- ( instance .__class__ .__name__ , exc .args [0 ]))
330
+ "Class '%s' instance cardinality error: %s" % (
331
+ instance .__class__ .__name__ , exc .args [0 ]))
311
332
312
333
ERROR_TEXT = "Wrong type of value '%s' on attribute '%s' expected it to be %s"
313
334
335
+
314
336
def valid_instance (instance ):
315
337
instclass = instance .__class__
316
338
class_name = instclass .__name__
317
339
318
- if instance .text :
319
- _has_val = True
320
- else :
321
- _has_val = False
340
+ # if instance.text:
341
+ # _has_val = True
342
+ # else:
343
+ # _has_val = False
322
344
323
345
if instclass .c_value_type and instance .text :
324
346
try :
325
347
validate_value_type (instance .text .strip (),
326
- instclass .c_value_type )
348
+ instclass .c_value_type )
327
349
except NotValid , exc :
328
350
raise NotValid ("Class '%s' instance: %s" % (class_name ,
329
351
exc .args [0 ]))
@@ -340,15 +362,14 @@ def valid_instance(instance):
340
362
if typ .c_value_type :
341
363
spec = typ .c_value_type
342
364
else :
343
- spec = {"base" : "string" } # doI need a default
365
+ spec = {"base" : "string" } # do I need a default
344
366
345
367
validate_value_type (value , spec )
346
368
else :
347
369
valid (typ , value )
348
370
except (NotValid , ValueError ), exc :
349
371
txt = ERROR_TEXT % (value , name , exc .args [0 ])
350
- raise NotValid (
351
- "Class '%s' instance: %s" % (class_name , txt ))
372
+ raise NotValid ("Class '%s' instance: %s" % (class_name , txt ))
352
373
353
374
for (name , _spec ) in instclass .c_children .values ():
354
375
value = getattr (instance , name , '' )
@@ -367,7 +388,7 @@ def valid_instance(instance):
367
388
_cmin = _cmax = _card = None
368
389
369
390
if value :
370
- _has_val = True
391
+ # _has_val = True
371
392
if isinstance (value , list ):
372
393
_list = True
373
394
vlen = len (value )
@@ -378,14 +399,14 @@ def valid_instance(instance):
378
399
if _card :
379
400
if _cmin is not None and _cmin > vlen :
380
401
raise NotValid (
381
- "Class '%s' instance cardinality error: %s" % \
382
- ( class_name , "less then min (%s<%s)" % (vlen ,
383
- _cmin )))
402
+ "Class '%s' instance cardinality error: %s" % (
403
+ class_name , "less then min (%s<%s)" % (vlen ,
404
+ _cmin )))
384
405
if _cmax is not None and vlen > _cmax :
385
406
raise NotValid (
386
- "Class '%s' instance cardinality error: %s" % \
387
- ( class_name , "more then max (%s>%s)" % (vlen ,
388
- _cmax )))
407
+ "Class '%s' instance cardinality error: %s" % (
408
+ class_name , "more then max (%s>%s)" % (vlen ,
409
+ _cmax )))
389
410
390
411
if _list :
391
412
for val in value :
@@ -396,8 +417,8 @@ def valid_instance(instance):
396
417
else :
397
418
if _cmin :
398
419
raise NotValid (
399
- "Class '%s' instance cardinality error: %s" % \
400
- ( class_name , "too few values on %s" % name ))
420
+ "Class '%s' instance cardinality error: %s" % (
421
+ class_name , "too few values on %s" % name ))
401
422
402
423
# if not _has_val:
403
424
# if class_name != "RequestedAttribute":
@@ -407,9 +428,10 @@ def valid_instance(instance):
407
428
408
429
return True
409
430
431
+
410
432
def valid_domain_name (dns_name ):
411
433
m = re .match (
412
- "^[a-z0-9]+([-.]{1 }[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$" ,
413
- dns_name , "ix" )
434
+ "^[a-z0-9]+([-.]{ 1 }[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$" ,
435
+ dns_name , "ix" )
414
436
if not m :
415
437
raise ValueError ("Not a proper domain name" )
0 commit comments