Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib/python3..../site-pac...
File: validate.py
# validate.py
[0] Fix | Delete
# A Validator object
[1] Fix | Delete
# Copyright (C) 2005-2014:
[2] Fix | Delete
# (name) : (email)
[3] Fix | Delete
# Michael Foord: fuzzyman AT voidspace DOT org DOT uk
[4] Fix | Delete
# Mark Andrews: mark AT la-la DOT com
[5] Fix | Delete
# Nicola Larosa: nico AT tekNico DOT net
[6] Fix | Delete
# Rob Dennis: rdennis AT gmail DOT com
[7] Fix | Delete
# Eli Courtwright: eli AT courtwright DOT org
[8] Fix | Delete
[9] Fix | Delete
# This software is licensed under the terms of the BSD license.
[10] Fix | Delete
# http://opensource.org/licenses/BSD-3-Clause
[11] Fix | Delete
[12] Fix | Delete
# ConfigObj 5 - main repository for documentation and issue tracking:
[13] Fix | Delete
# https://github.com/DiffSK/configobj
[14] Fix | Delete
[15] Fix | Delete
"""
[16] Fix | Delete
The Validator object is used to check that supplied values
[17] Fix | Delete
conform to a specification.
[18] Fix | Delete
[19] Fix | Delete
The value can be supplied as a string - e.g. from a config file.
[20] Fix | Delete
In this case the check will also *convert* the value to
[21] Fix | Delete
the required type. This allows you to add validation
[22] Fix | Delete
as a transparent layer to access data stored as strings.
[23] Fix | Delete
The validation checks that the data is correct *and*
[24] Fix | Delete
converts it to the expected type.
[25] Fix | Delete
[26] Fix | Delete
Some standard checks are provided for basic data types.
[27] Fix | Delete
Additional checks are easy to write. They can be
[28] Fix | Delete
provided when the ``Validator`` is instantiated or
[29] Fix | Delete
added afterwards.
[30] Fix | Delete
[31] Fix | Delete
The standard functions work with the following basic data types :
[32] Fix | Delete
[33] Fix | Delete
* integers
[34] Fix | Delete
* floats
[35] Fix | Delete
* booleans
[36] Fix | Delete
* strings
[37] Fix | Delete
* ip_addr
[38] Fix | Delete
[39] Fix | Delete
plus lists of these datatypes
[40] Fix | Delete
[41] Fix | Delete
Adding additional checks is done through coding simple functions.
[42] Fix | Delete
[43] Fix | Delete
The full set of standard checks are :
[44] Fix | Delete
[45] Fix | Delete
* 'integer': matches integer values (including negative)
[46] Fix | Delete
Takes optional 'min' and 'max' arguments : ::
[47] Fix | Delete
[48] Fix | Delete
integer()
[49] Fix | Delete
integer(3, 9) # any value from 3 to 9
[50] Fix | Delete
integer(min=0) # any positive value
[51] Fix | Delete
integer(max=9)
[52] Fix | Delete
[53] Fix | Delete
* 'float': matches float values
[54] Fix | Delete
Has the same parameters as the integer check.
[55] Fix | Delete
[56] Fix | Delete
* 'boolean': matches boolean values - ``True`` or ``False``
[57] Fix | Delete
Acceptable string values for True are :
[58] Fix | Delete
true, on, yes, 1
[59] Fix | Delete
Acceptable string values for False are :
[60] Fix | Delete
false, off, no, 0
[61] Fix | Delete
[62] Fix | Delete
Any other value raises an error.
[63] Fix | Delete
[64] Fix | Delete
* 'ip_addr': matches an Internet Protocol address, v.4, represented
[65] Fix | Delete
by a dotted-quad string, i.e. '1.2.3.4'.
[66] Fix | Delete
[67] Fix | Delete
* 'string': matches any string.
[68] Fix | Delete
Takes optional keyword args 'min' and 'max'
[69] Fix | Delete
to specify min and max lengths of the string.
[70] Fix | Delete
[71] Fix | Delete
* 'list': matches any list.
[72] Fix | Delete
Takes optional keyword args 'min', and 'max' to specify min and
[73] Fix | Delete
max sizes of the list. (Always returns a list.)
[74] Fix | Delete
[75] Fix | Delete
* 'tuple': matches any tuple.
[76] Fix | Delete
Takes optional keyword args 'min', and 'max' to specify min and
[77] Fix | Delete
max sizes of the tuple. (Always returns a tuple.)
[78] Fix | Delete
[79] Fix | Delete
* 'int_list': Matches a list of integers.
[80] Fix | Delete
Takes the same arguments as list.
[81] Fix | Delete
[82] Fix | Delete
* 'float_list': Matches a list of floats.
[83] Fix | Delete
Takes the same arguments as list.
[84] Fix | Delete
[85] Fix | Delete
* 'bool_list': Matches a list of boolean values.
[86] Fix | Delete
Takes the same arguments as list.
[87] Fix | Delete
[88] Fix | Delete
* 'ip_addr_list': Matches a list of IP addresses.
[89] Fix | Delete
Takes the same arguments as list.
[90] Fix | Delete
[91] Fix | Delete
* 'string_list': Matches a list of strings.
[92] Fix | Delete
Takes the same arguments as list.
[93] Fix | Delete
[94] Fix | Delete
* 'mixed_list': Matches a list with different types in
[95] Fix | Delete
specific positions. List size must match
[96] Fix | Delete
the number of arguments.
[97] Fix | Delete
[98] Fix | Delete
Each position can be one of :
[99] Fix | Delete
'integer', 'float', 'ip_addr', 'string', 'boolean'
[100] Fix | Delete
[101] Fix | Delete
So to specify a list with two strings followed
[102] Fix | Delete
by two integers, you write the check as : ::
[103] Fix | Delete
[104] Fix | Delete
mixed_list('string', 'string', 'integer', 'integer')
[105] Fix | Delete
[106] Fix | Delete
* 'pass': This check matches everything ! It never fails
[107] Fix | Delete
and the value is unchanged.
[108] Fix | Delete
[109] Fix | Delete
It is also the default if no check is specified.
[110] Fix | Delete
[111] Fix | Delete
* 'option': This check matches any from a list of options.
[112] Fix | Delete
You specify this check with : ::
[113] Fix | Delete
[114] Fix | Delete
option('option 1', 'option 2', 'option 3')
[115] Fix | Delete
[116] Fix | Delete
You can supply a default value (returned if no value is supplied)
[117] Fix | Delete
using the default keyword argument.
[118] Fix | Delete
[119] Fix | Delete
You specify a list argument for default using a list constructor syntax in
[120] Fix | Delete
the check : ::
[121] Fix | Delete
[122] Fix | Delete
checkname(arg1, arg2, default=list('val 1', 'val 2', 'val 3'))
[123] Fix | Delete
[124] Fix | Delete
A badly formatted set of arguments will raise a ``VdtParamError``.
[125] Fix | Delete
"""
[126] Fix | Delete
[127] Fix | Delete
__version__ = '1.0.1'
[128] Fix | Delete
[129] Fix | Delete
[130] Fix | Delete
__all__ = (
[131] Fix | Delete
'__version__',
[132] Fix | Delete
'dottedQuadToNum',
[133] Fix | Delete
'numToDottedQuad',
[134] Fix | Delete
'ValidateError',
[135] Fix | Delete
'VdtUnknownCheckError',
[136] Fix | Delete
'VdtParamError',
[137] Fix | Delete
'VdtTypeError',
[138] Fix | Delete
'VdtValueError',
[139] Fix | Delete
'VdtValueTooSmallError',
[140] Fix | Delete
'VdtValueTooBigError',
[141] Fix | Delete
'VdtValueTooShortError',
[142] Fix | Delete
'VdtValueTooLongError',
[143] Fix | Delete
'VdtMissingValue',
[144] Fix | Delete
'Validator',
[145] Fix | Delete
'is_integer',
[146] Fix | Delete
'is_float',
[147] Fix | Delete
'is_boolean',
[148] Fix | Delete
'is_list',
[149] Fix | Delete
'is_tuple',
[150] Fix | Delete
'is_ip_addr',
[151] Fix | Delete
'is_string',
[152] Fix | Delete
'is_int_list',
[153] Fix | Delete
'is_bool_list',
[154] Fix | Delete
'is_float_list',
[155] Fix | Delete
'is_string_list',
[156] Fix | Delete
'is_ip_addr_list',
[157] Fix | Delete
'is_mixed_list',
[158] Fix | Delete
'is_option',
[159] Fix | Delete
'__docformat__',
[160] Fix | Delete
)
[161] Fix | Delete
[162] Fix | Delete
[163] Fix | Delete
import re
[164] Fix | Delete
import sys
[165] Fix | Delete
from pprint import pprint
[166] Fix | Delete
[167] Fix | Delete
#TODO - #21 - six is part of the repo now, but we didn't switch over to it here
[168] Fix | Delete
# this could be replaced if six is used for compatibility, or there are no
[169] Fix | Delete
# more assertions about items being a string
[170] Fix | Delete
if sys.version_info < (3,):
[171] Fix | Delete
string_type = basestring
[172] Fix | Delete
else:
[173] Fix | Delete
string_type = str
[174] Fix | Delete
# so tests that care about unicode on 2.x can specify unicode, and the same
[175] Fix | Delete
# tests when run on 3.x won't complain about a undefined name "unicode"
[176] Fix | Delete
# since all strings are unicode on 3.x we just want to pass it through
[177] Fix | Delete
# unchanged
[178] Fix | Delete
unicode = lambda x: x
[179] Fix | Delete
# in python 3, all ints are equivalent to python 2 longs, and they'll
[180] Fix | Delete
# never show "L" in the repr
[181] Fix | Delete
long = int
[182] Fix | Delete
[183] Fix | Delete
_list_arg = re.compile(r'''
[184] Fix | Delete
(?:
[185] Fix | Delete
([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*list\(
[186] Fix | Delete
(
[187] Fix | Delete
(?:
[188] Fix | Delete
\s*
[189] Fix | Delete
(?:
[190] Fix | Delete
(?:".*?")| # double quotes
[191] Fix | Delete
(?:'.*?')| # single quotes
[192] Fix | Delete
(?:[^'",\s\)][^,\)]*?) # unquoted
[193] Fix | Delete
)
[194] Fix | Delete
\s*,\s*
[195] Fix | Delete
)*
[196] Fix | Delete
(?:
[197] Fix | Delete
(?:".*?")| # double quotes
[198] Fix | Delete
(?:'.*?')| # single quotes
[199] Fix | Delete
(?:[^'",\s\)][^,\)]*?) # unquoted
[200] Fix | Delete
)? # last one
[201] Fix | Delete
)
[202] Fix | Delete
\)
[203] Fix | Delete
)
[204] Fix | Delete
''', re.VERBOSE | re.DOTALL) # two groups
[205] Fix | Delete
[206] Fix | Delete
_list_members = re.compile(r'''
[207] Fix | Delete
(
[208] Fix | Delete
(?:".*?")| # double quotes
[209] Fix | Delete
(?:'.*?')| # single quotes
[210] Fix | Delete
(?:[^'",\s=][^,=]*?) # unquoted
[211] Fix | Delete
)
[212] Fix | Delete
(?:
[213] Fix | Delete
(?:\s*,\s*)|(?:\s*$) # comma
[214] Fix | Delete
)
[215] Fix | Delete
''', re.VERBOSE | re.DOTALL) # one group
[216] Fix | Delete
[217] Fix | Delete
_paramstring = r'''
[218] Fix | Delete
(?:
[219] Fix | Delete
(
[220] Fix | Delete
(?:
[221] Fix | Delete
[a-zA-Z_][a-zA-Z0-9_]*\s*=\s*list\(
[222] Fix | Delete
(?:
[223] Fix | Delete
\s*
[224] Fix | Delete
(?:
[225] Fix | Delete
(?:".*?")| # double quotes
[226] Fix | Delete
(?:'.*?')| # single quotes
[227] Fix | Delete
(?:[^'",\s\)][^,\)]*?) # unquoted
[228] Fix | Delete
)
[229] Fix | Delete
\s*,\s*
[230] Fix | Delete
)*
[231] Fix | Delete
(?:
[232] Fix | Delete
(?:".*?")| # double quotes
[233] Fix | Delete
(?:'.*?')| # single quotes
[234] Fix | Delete
(?:[^'",\s\)][^,\)]*?) # unquoted
[235] Fix | Delete
)? # last one
[236] Fix | Delete
\)
[237] Fix | Delete
)|
[238] Fix | Delete
(?:
[239] Fix | Delete
(?:".*?")| # double quotes
[240] Fix | Delete
(?:'.*?')| # single quotes
[241] Fix | Delete
(?:[^'",\s=][^,=]*?)| # unquoted
[242] Fix | Delete
(?: # keyword argument
[243] Fix | Delete
[a-zA-Z_][a-zA-Z0-9_]*\s*=\s*
[244] Fix | Delete
(?:
[245] Fix | Delete
(?:".*?")| # double quotes
[246] Fix | Delete
(?:'.*?')| # single quotes
[247] Fix | Delete
(?:[^'",\s=][^,=]*?) # unquoted
[248] Fix | Delete
)
[249] Fix | Delete
)
[250] Fix | Delete
)
[251] Fix | Delete
)
[252] Fix | Delete
(?:
[253] Fix | Delete
(?:\s*,\s*)|(?:\s*$) # comma
[254] Fix | Delete
)
[255] Fix | Delete
)
[256] Fix | Delete
'''
[257] Fix | Delete
[258] Fix | Delete
_matchstring = '^%s*' % _paramstring
[259] Fix | Delete
[260] Fix | Delete
# Python pre 2.2.1 doesn't have bool
[261] Fix | Delete
try:
[262] Fix | Delete
bool
[263] Fix | Delete
except NameError:
[264] Fix | Delete
def bool(val):
[265] Fix | Delete
"""Simple boolean equivalent function. """
[266] Fix | Delete
if val:
[267] Fix | Delete
return 1
[268] Fix | Delete
else:
[269] Fix | Delete
return 0
[270] Fix | Delete
[271] Fix | Delete
[272] Fix | Delete
def dottedQuadToNum(ip):
[273] Fix | Delete
"""
[274] Fix | Delete
Convert decimal dotted quad string to long integer
[275] Fix | Delete
[276] Fix | Delete
>>> int(dottedQuadToNum('1 '))
[277] Fix | Delete
1
[278] Fix | Delete
>>> int(dottedQuadToNum(' 1.2'))
[279] Fix | Delete
16777218
[280] Fix | Delete
>>> int(dottedQuadToNum(' 1.2.3 '))
[281] Fix | Delete
16908291
[282] Fix | Delete
>>> int(dottedQuadToNum('1.2.3.4'))
[283] Fix | Delete
16909060
[284] Fix | Delete
>>> dottedQuadToNum('255.255.255.255')
[285] Fix | Delete
4294967295
[286] Fix | Delete
>>> dottedQuadToNum('255.255.255.256')
[287] Fix | Delete
Traceback (most recent call last):
[288] Fix | Delete
ValueError: Not a good dotted-quad IP: 255.255.255.256
[289] Fix | Delete
"""
[290] Fix | Delete
[291] Fix | Delete
# import here to avoid it when ip_addr values are not used
[292] Fix | Delete
import socket, struct
[293] Fix | Delete
[294] Fix | Delete
try:
[295] Fix | Delete
return struct.unpack('!L',
[296] Fix | Delete
socket.inet_aton(ip.strip()))[0]
[297] Fix | Delete
except socket.error:
[298] Fix | Delete
raise ValueError('Not a good dotted-quad IP: %s' % ip)
[299] Fix | Delete
return
[300] Fix | Delete
[301] Fix | Delete
[302] Fix | Delete
def numToDottedQuad(num):
[303] Fix | Delete
"""
[304] Fix | Delete
Convert int or long int to dotted quad string
[305] Fix | Delete
[306] Fix | Delete
>>> numToDottedQuad(long(-1))
[307] Fix | Delete
Traceback (most recent call last):
[308] Fix | Delete
ValueError: Not a good numeric IP: -1
[309] Fix | Delete
>>> numToDottedQuad(long(1))
[310] Fix | Delete
'0.0.0.1'
[311] Fix | Delete
>>> numToDottedQuad(long(16777218))
[312] Fix | Delete
'1.0.0.2'
[313] Fix | Delete
>>> numToDottedQuad(long(16908291))
[314] Fix | Delete
'1.2.0.3'
[315] Fix | Delete
>>> numToDottedQuad(long(16909060))
[316] Fix | Delete
'1.2.3.4'
[317] Fix | Delete
>>> numToDottedQuad(long(4294967295))
[318] Fix | Delete
'255.255.255.255'
[319] Fix | Delete
>>> numToDottedQuad(long(4294967296))
[320] Fix | Delete
Traceback (most recent call last):
[321] Fix | Delete
ValueError: Not a good numeric IP: 4294967296
[322] Fix | Delete
>>> numToDottedQuad(-1)
[323] Fix | Delete
Traceback (most recent call last):
[324] Fix | Delete
ValueError: Not a good numeric IP: -1
[325] Fix | Delete
>>> numToDottedQuad(1)
[326] Fix | Delete
'0.0.0.1'
[327] Fix | Delete
>>> numToDottedQuad(16777218)
[328] Fix | Delete
'1.0.0.2'
[329] Fix | Delete
>>> numToDottedQuad(16908291)
[330] Fix | Delete
'1.2.0.3'
[331] Fix | Delete
>>> numToDottedQuad(16909060)
[332] Fix | Delete
'1.2.3.4'
[333] Fix | Delete
>>> numToDottedQuad(4294967295)
[334] Fix | Delete
'255.255.255.255'
[335] Fix | Delete
>>> numToDottedQuad(4294967296)
[336] Fix | Delete
Traceback (most recent call last):
[337] Fix | Delete
ValueError: Not a good numeric IP: 4294967296
[338] Fix | Delete
[339] Fix | Delete
"""
[340] Fix | Delete
[341] Fix | Delete
# import here to avoid it when ip_addr values are not used
[342] Fix | Delete
import socket, struct
[343] Fix | Delete
[344] Fix | Delete
# no need to intercept here, 4294967295L is fine
[345] Fix | Delete
if num > long(4294967295) or num < 0:
[346] Fix | Delete
raise ValueError('Not a good numeric IP: %s' % num)
[347] Fix | Delete
try:
[348] Fix | Delete
return socket.inet_ntoa(
[349] Fix | Delete
struct.pack('!L', long(num)))
[350] Fix | Delete
except (socket.error, struct.error, OverflowError):
[351] Fix | Delete
raise ValueError('Not a good numeric IP: %s' % num)
[352] Fix | Delete
[353] Fix | Delete
[354] Fix | Delete
class ValidateError(Exception):
[355] Fix | Delete
"""
[356] Fix | Delete
This error indicates that the check failed.
[357] Fix | Delete
It can be the base class for more specific errors.
[358] Fix | Delete
[359] Fix | Delete
Any check function that fails ought to raise this error.
[360] Fix | Delete
(or a subclass)
[361] Fix | Delete
[362] Fix | Delete
>>> raise ValidateError
[363] Fix | Delete
Traceback (most recent call last):
[364] Fix | Delete
ValidateError
[365] Fix | Delete
"""
[366] Fix | Delete
[367] Fix | Delete
[368] Fix | Delete
class VdtMissingValue(ValidateError):
[369] Fix | Delete
"""No value was supplied to a check that needed one."""
[370] Fix | Delete
[371] Fix | Delete
[372] Fix | Delete
class VdtUnknownCheckError(ValidateError):
[373] Fix | Delete
"""An unknown check function was requested"""
[374] Fix | Delete
[375] Fix | Delete
def __init__(self, value):
[376] Fix | Delete
"""
[377] Fix | Delete
>>> raise VdtUnknownCheckError('yoda')
[378] Fix | Delete
Traceback (most recent call last):
[379] Fix | Delete
VdtUnknownCheckError: the check "yoda" is unknown.
[380] Fix | Delete
"""
[381] Fix | Delete
ValidateError.__init__(self, 'the check "%s" is unknown.' % (value,))
[382] Fix | Delete
[383] Fix | Delete
[384] Fix | Delete
class VdtParamError(SyntaxError):
[385] Fix | Delete
"""An incorrect parameter was passed"""
[386] Fix | Delete
[387] Fix | Delete
def __init__(self, name, value):
[388] Fix | Delete
"""
[389] Fix | Delete
>>> raise VdtParamError('yoda', 'jedi')
[390] Fix | Delete
Traceback (most recent call last):
[391] Fix | Delete
VdtParamError: passed an incorrect value "jedi" for parameter "yoda".
[392] Fix | Delete
"""
[393] Fix | Delete
SyntaxError.__init__(self, 'passed an incorrect value "%s" for parameter "%s".' % (value, name))
[394] Fix | Delete
[395] Fix | Delete
[396] Fix | Delete
class VdtTypeError(ValidateError):
[397] Fix | Delete
"""The value supplied was of the wrong type"""
[398] Fix | Delete
[399] Fix | Delete
def __init__(self, value):
[400] Fix | Delete
"""
[401] Fix | Delete
>>> raise VdtTypeError('jedi')
[402] Fix | Delete
Traceback (most recent call last):
[403] Fix | Delete
VdtTypeError: the value "jedi" is of the wrong type.
[404] Fix | Delete
"""
[405] Fix | Delete
ValidateError.__init__(self, 'the value "%s" is of the wrong type.' % (value,))
[406] Fix | Delete
[407] Fix | Delete
[408] Fix | Delete
class VdtValueError(ValidateError):
[409] Fix | Delete
"""The value supplied was of the correct type, but was not an allowed value."""
[410] Fix | Delete
[411] Fix | Delete
def __init__(self, value):
[412] Fix | Delete
"""
[413] Fix | Delete
>>> raise VdtValueError('jedi')
[414] Fix | Delete
Traceback (most recent call last):
[415] Fix | Delete
VdtValueError: the value "jedi" is unacceptable.
[416] Fix | Delete
"""
[417] Fix | Delete
ValidateError.__init__(self, 'the value "%s" is unacceptable.' % (value,))
[418] Fix | Delete
[419] Fix | Delete
[420] Fix | Delete
class VdtValueTooSmallError(VdtValueError):
[421] Fix | Delete
"""The value supplied was of the correct type, but was too small."""
[422] Fix | Delete
[423] Fix | Delete
def __init__(self, value):
[424] Fix | Delete
"""
[425] Fix | Delete
>>> raise VdtValueTooSmallError('0')
[426] Fix | Delete
Traceback (most recent call last):
[427] Fix | Delete
VdtValueTooSmallError: the value "0" is too small.
[428] Fix | Delete
"""
[429] Fix | Delete
ValidateError.__init__(self, 'the value "%s" is too small.' % (value,))
[430] Fix | Delete
[431] Fix | Delete
[432] Fix | Delete
class VdtValueTooBigError(VdtValueError):
[433] Fix | Delete
"""The value supplied was of the correct type, but was too big."""
[434] Fix | Delete
[435] Fix | Delete
def __init__(self, value):
[436] Fix | Delete
"""
[437] Fix | Delete
>>> raise VdtValueTooBigError('1')
[438] Fix | Delete
Traceback (most recent call last):
[439] Fix | Delete
VdtValueTooBigError: the value "1" is too big.
[440] Fix | Delete
"""
[441] Fix | Delete
ValidateError.__init__(self, 'the value "%s" is too big.' % (value,))
[442] Fix | Delete
[443] Fix | Delete
[444] Fix | Delete
class VdtValueTooShortError(VdtValueError):
[445] Fix | Delete
"""The value supplied was of the correct type, but was too short."""
[446] Fix | Delete
[447] Fix | Delete
def __init__(self, value):
[448] Fix | Delete
"""
[449] Fix | Delete
>>> raise VdtValueTooShortError('jed')
[450] Fix | Delete
Traceback (most recent call last):
[451] Fix | Delete
VdtValueTooShortError: the value "jed" is too short.
[452] Fix | Delete
"""
[453] Fix | Delete
ValidateError.__init__(
[454] Fix | Delete
self,
[455] Fix | Delete
'the value "%s" is too short.' % (value,))
[456] Fix | Delete
[457] Fix | Delete
[458] Fix | Delete
class VdtValueTooLongError(VdtValueError):
[459] Fix | Delete
"""The value supplied was of the correct type, but was too long."""
[460] Fix | Delete
[461] Fix | Delete
def __init__(self, value):
[462] Fix | Delete
"""
[463] Fix | Delete
>>> raise VdtValueTooLongError('jedie')
[464] Fix | Delete
Traceback (most recent call last):
[465] Fix | Delete
VdtValueTooLongError: the value "jedie" is too long.
[466] Fix | Delete
"""
[467] Fix | Delete
ValidateError.__init__(self, 'the value "%s" is too long.' % (value,))
[468] Fix | Delete
[469] Fix | Delete
[470] Fix | Delete
class Validator(object):
[471] Fix | Delete
"""
[472] Fix | Delete
Validator is an object that allows you to register a set of 'checks'.
[473] Fix | Delete
These checks take input and test that it conforms to the check.
[474] Fix | Delete
[475] Fix | Delete
This can also involve converting the value from a string into
[476] Fix | Delete
the correct datatype.
[477] Fix | Delete
[478] Fix | Delete
The ``check`` method takes an input string which configures which
[479] Fix | Delete
check is to be used and applies that check to a supplied value.
[480] Fix | Delete
[481] Fix | Delete
An example input string would be:
[482] Fix | Delete
'int_range(param1, param2)'
[483] Fix | Delete
[484] Fix | Delete
You would then provide something like:
[485] Fix | Delete
[486] Fix | Delete
>>> def int_range_check(value, min, max):
[487] Fix | Delete
... # turn min and max from strings to integers
[488] Fix | Delete
... min = int(min)
[489] Fix | Delete
... max = int(max)
[490] Fix | Delete
... # check that value is of the correct type.
[491] Fix | Delete
... # possible valid inputs are integers or strings
[492] Fix | Delete
... # that represent integers
[493] Fix | Delete
... if not isinstance(value, (int, long, string_type)):
[494] Fix | Delete
... raise VdtTypeError(value)
[495] Fix | Delete
... elif isinstance(value, string_type):
[496] Fix | Delete
... # if we are given a string
[497] Fix | Delete
... # attempt to convert to an integer
[498] Fix | Delete
... try:
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function