3
# Copyright (C) 2005-2010 Michael Foord, Mark Andrews, Nicola Larosa
4
# E-mail: fuzzyman AT voidspace DOT org DOT uk
5
# mark AT la-la DOT com
6
# nico AT tekNico DOT net
3
# Copyright (C) 2005-2014:
5
# Michael Foord: fuzzyman AT voidspace DOT org DOT uk
6
# Mark Andrews: mark AT la-la DOT com
7
# Nicola Larosa: nico AT tekNico DOT net
8
# Rob Dennis: rdennis AT gmail DOT com
9
# Eli Courtwright: eli AT courtwright DOT org
8
11
# This software is licensed under the terms of the BSD license.
9
# http://www.voidspace.org.uk/python/license.shtml
10
# Basically you're free to copy, modify, distribute and relicense it,
11
# So long as you keep a copy of the license with it.
12
# http://opensource.org/licenses/BSD-3-Clause
13
# Scripts maintained at http://www.voidspace.org.uk/python/index.shtml
14
# For information about bugfixes, updates and support, please join the
15
# ConfigObj mailing list:
16
# http://lists.sourceforge.net/lists/listinfo/configobj-develop
17
# Comments, suggestions and bug reports welcome.
14
# ConfigObj 5 - main repository for documentation and issue tracking:
15
# https://github.com/DiffSK/configobj
20
18
The Validator object is used to check that supplied values
167
from pprint import pprint
169
#TODO - #21 - six is part of the repo now, but we didn't switch over to it here
170
# this could be replaced if six is used for compatibility, or there are no
171
# more assertions about items being a string
172
if sys.version_info < (3,):
173
string_type = basestring
176
# so tests that care about unicode on 2.x can specify unicode, and the same
177
# tests when run on 3.x won't complain about a undefined name "unicode"
178
# since all strings are unicode on 3.x we just want to pass it through
180
unicode = lambda x: x
181
# in python 3, all ints are equivalent to python 2 longs, and they'll
182
# never show "L" in the repr
170
185
_list_arg = re.compile(r'''
282
297
return struct.unpack('!L',
283
298
socket.inet_aton(ip.strip()))[0]
284
299
except socket.error:
285
# bug in inet_aton, corrected in Python 2.4
286
if ip.strip() == '255.255.255.255':
289
raise ValueError('Not a good dotted-quad IP: %s' % ip)
300
raise ValueError('Not a good dotted-quad IP: %s' % ip)
293
304
def numToDottedQuad(num):
295
Convert long int to dotted quad string
306
Convert int or long int to dotted quad string
297
>>> numToDottedQuad(-1L)
298
Traceback (most recent call last):
299
ValueError: Not a good numeric IP: -1
300
>>> numToDottedQuad(1L)
302
>>> numToDottedQuad(16777218L)
304
>>> numToDottedQuad(16908291L)
306
>>> numToDottedQuad(16909060L)
308
>>> numToDottedQuad(4294967295L)
310
>>> numToDottedQuad(4294967296L)
311
Traceback (most recent call last):
312
ValueError: Not a good numeric IP: 4294967296
308
>>> numToDottedQuad(long(-1))
309
Traceback (most recent call last):
310
ValueError: Not a good numeric IP: -1
311
>>> numToDottedQuad(long(1))
313
>>> numToDottedQuad(long(16777218))
315
>>> numToDottedQuad(long(16908291))
317
>>> numToDottedQuad(long(16909060))
319
>>> numToDottedQuad(long(4294967295))
321
>>> numToDottedQuad(long(4294967296))
322
Traceback (most recent call last):
323
ValueError: Not a good numeric IP: 4294967296
324
>>> numToDottedQuad(-1)
325
Traceback (most recent call last):
326
ValueError: Not a good numeric IP: -1
327
>>> numToDottedQuad(1)
329
>>> numToDottedQuad(16777218)
331
>>> numToDottedQuad(16908291)
333
>>> numToDottedQuad(16909060)
335
>>> numToDottedQuad(4294967295)
337
>>> numToDottedQuad(4294967296)
338
Traceback (most recent call last):
339
ValueError: Not a good numeric IP: 4294967296
315
343
# import here to avoid it when ip_addr values are not used
316
344
import socket, struct
318
346
# no need to intercept here, 4294967295L is fine
319
if num > 4294967295L or num < 0:
347
if num > long(4294967295) or num < 0:
320
348
raise ValueError('Not a good numeric IP: %s' % num)
322
350
return socket.inet_ntoa(
464
492
... # check that value is of the correct type.
465
493
... # possible valid inputs are integers or strings
466
494
... # that represent integers
467
... if not isinstance(value, (int, long, basestring)):
495
... if not isinstance(value, (int, long, string_type)):
468
496
... raise VdtTypeError(value)
469
... elif isinstance(value, basestring):
497
... elif isinstance(value, string_type):
470
498
... # if we are given a string
471
499
... # attempt to convert to an integer
615
643
fun_kwargs = dict(fun_kwargs)
617
645
fun_name, fun_args, fun_kwargs, default = self._parse_check(check)
618
fun_kwargs = dict([(str(key), value) for (key, value) in fun_kwargs.items()])
646
fun_kwargs = dict([(str(key), value) for (key, value) in list(fun_kwargs.items())])
619
647
self._cache[check] = fun_name, list(fun_args), dict(fun_kwargs), default
620
648
return fun_name, fun_args, fun_kwargs, default
736
764
for (name, val) in zip(names, values):
738
766
out_params.append(val)
739
elif isinstance(val, (int, long, float, basestring)):
767
elif isinstance(val, (int, long, float, string_type)):
741
769
out_params.append(fun(val))
742
except ValueError, e:
770
except ValueError as e:
743
771
raise VdtParamError(name, val)
745
773
raise VdtParamError(name, val)
795
823
(min_val, max_val) = _is_num_param(('min', 'max'), (min, max))
796
if not isinstance(value, (int, long, basestring)):
824
if not isinstance(value, (int, long, string_type)):
797
825
raise VdtTypeError(value)
798
if isinstance(value, basestring):
826
if isinstance(value, string_type):
799
827
# if it's a string - does it represent an integer ?
801
829
value = int(value)
846
874
(min_val, max_val) = _is_num_param(
847
875
('min', 'max'), (min, max), to_float=True)
848
if not isinstance(value, (int, long, float, basestring)):
876
if not isinstance(value, (int, long, float, string_type)):
849
877
raise VdtTypeError(value)
850
878
if not isinstance(value, float):
851
879
# if it's a string - does it represent a float ?
1064
1092
Traceback (most recent call last):
1065
1093
VdtValueTooLongError: the value "1234" is too long.
1067
if not isinstance(value, basestring):
1095
if not isinstance(value, string_type):
1068
1096
raise VdtTypeError(value)
1069
1097
(min_len, max_len) = _is_num_param(('min', 'max'), (min, max))
1266
1294
>>> vtor.check(mix_str, 0)
1267
1295
Traceback (most recent call last):
1268
1296
VdtTypeError: the value "0" is of the wrong type.
1270
This test requires an elaborate setup, because of a change in error string
1271
output from the interpreter between Python 2.2 and 2.3 .
1274
... 'passed an incorrect value "',
1276
... '" for parameter "mixed_list".',
1278
>>> res_str = "'".join(res_seq)
1280
... vtor.check('mixed_list("yoda")', ('a'))
1281
... except VdtParamError, err:
1282
... str(err) == res_str
1298
>>> vtor.check('mixed_list("yoda")', ('a'))
1299
Traceback (most recent call last):
1300
VdtParamError: passed an incorrect value "KeyError('yoda',)" for parameter "'mixed_list'"
1286
1303
length = len(value)
1339
1356
>>> v = Validator({'test': _test})
1340
1357
>>> for entry in checks:
1341
... print v.check(('test(%s)' % entry), 3)
1342
(3, ('3', '6'), {'test': ['a', 'b', 'c'], 'max': '3', 'min': '1'})
1358
... pprint(v.check(('test(%s)' % entry), 3))
1359
(3, ('3', '6'), {'max': '3', 'min': '1', 'test': ['a', 'b', 'c']})
1343
1360
(3, ('3',), {})
1344
1361
(3, ('3', '6'), {})
1345
1362
(3, ('3',), {})
1346
(3, (), {'test': 'a b c', 'min': '1'})
1347
(3, (), {'test': 'a, b, c', 'min': '5'})
1348
(3, (), {'test': 'a, b, c', 'max': '3', 'min': '1'})
1349
(3, (), {'test': '-99', 'min': '-100'})
1363
(3, (), {'min': '1', 'test': 'a b c'})
1364
(3, (), {'min': '5', 'test': 'a, b, c'})
1365
(3, (), {'max': '3', 'min': '1', 'test': 'a, b, c'})
1366
(3, (), {'min': '-100', 'test': '-99'})
1350
1367
(3, (), {'max': '3', 'min': '1'})
1351
1368
(3, ('3', '6'), {'test': '36'})
1352
1369
(3, ('3', '6'), {'test': 'a, b, c'})
1353
(3, ('3',), {'test': ['a', 'b', 'c'], 'max': '3'})
1354
(3, ('3',), {'test': ["'a'", 'b', 'x=(c)'], 'max': '3'})
1370
(3, ('3',), {'max': '3', 'test': ['a', 'b', 'c']})
1371
(3, ('3',), {'max': '3', 'test': ["'a'", 'b', 'x=(c)']})
1355
1372
(3, (), {'test': 'x=fish(3)'})
1357
1374
>>> v = Validator()
1384
1401
Bug test for unicode arguments
1385
1402
>>> v = Validator()
1386
>>> v.check(u'string(min=4)', u'test')
1403
>>> v.check(unicode('string(min=4)'), unicode('test')) == unicode('test')
1389
1406
>>> v = Validator()
1390
>>> v.get_default_value(u'string(min=4, default="1234")')
1392
>>> v.check(u'string(min=4, default="1234")', u'test')
1407
>>> v.get_default_value(unicode('string(min=4, default="1234")')) == unicode('1234')
1409
>>> v.check(unicode('string(min=4, default="1234")'), unicode('test')) == unicode('test')
1395
1412
>>> v = Validator()
1396
1413
>>> default = v.get_default_value('string(default=None)')