1 # vim: set encoding=utf-8 et sw=4 sts=4 :
4 from formencode import Invalid
5 from formencode.validators import *
6 from formencode.compound import *
8 from pymin.item import Item
9 from pymin.validatedclass import Field
37 min = -9223372036854775808
38 max = +9223372036854775807
42 max = 18446744073709551615
47 Same as :class:`OneOf` but values are uppercased before validation.
51 >>> uoo = UpOneOf(['A', 'B', 'C'])
52 >>> uoo.to_python('a')
54 >>> uoo.to_python('B')
56 >>> uoo.to_python('x')
57 Traceback (most recent call last):
59 Invalid: Value must be one of: A; B; C (not 'X')
62 def _to_python(self, value, state):
66 class HostName(FancyValidator):
68 Formencode validator to check whether a string is a correct host name
73 >>> n.to_python('host-name')
75 >>> n.to_python('host name')
76 Traceback (most recent call last):
78 Invalid: Not a valid host name
79 >>> n.to_python('hostname' * 8)
80 Traceback (most recent call last):
82 Invalid: Host name is too long (maximum length is 63 characters)
86 empty = u'Please enter a host name',
87 bad_format = u'Not a valid host name',
88 too_long = u'Host name is too long (maximum length is '
89 u'%(max_len)d characters)',
92 max_len = 63 # official limit for a label
93 hostnameRE = re.compile(r"^[a-zA-Z0-9][\w\-]*$")
95 def validate_python(self, value, state):
96 if len(value) > self.max_len:
97 raise Invalid(self.message('host_too_long', state, value=value,
98 max_len=self.max_len),
100 if not self.hostnameRE.search(value):
101 raise Invalid(self.message('bad_format', state, value=value),
105 class FullyQualifiedHostName(HostName):
107 Formencode validator to check whether a string is a correct host name
111 >>> n = FullyQualifiedHostName()
112 >>> n.to_python('example.com')
114 >>> n.to_python('example')
115 Traceback (most recent call last):
117 Invalid: Not a valid host name
118 >>> n.to_python('example.' * 32 + 'com')
119 Traceback (most recent call last):
121 Invalid: Host name is too long (maximum length is 253 characters)
124 messages = dict(HostName._messages,
125 empty = u'Please enter a fully qualified host name',
126 bad_format = u'Not a fully qualified host name',
130 hostnameRE = re.compile(r"^[a-zA-Z0-9][\w\-\.]*\.[a-zA-Z]+$")
133 class IPAddress(FancyValidator):
135 Formencode validator to check whether a string is a correct IP address
140 >>> ip.to_python('127.0.0.1')
142 >>> ip.to_python('299.0.0.1')
143 Traceback (most recent call last):
145 Invalid: The octets must be within the range of 0-255 (not '299')
146 >>> ip.to_python('192.168.0.1/1')
147 Traceback (most recent call last):
149 Invalid: Please enter a valid IP address (a.b.c.d)
150 >>> ip.to_python('asdf')
151 Traceback (most recent call last):
153 Invalid: Please enter a valid IP address (a.b.c.d)
156 'bad_format' : u'Please enter a valid IP address (a.b.c.d)',
157 'illegal_octets' : u'The octets must be within the range of 0-255 (not %(octet)r)',
160 def validate_python(self, value, state):
162 octets = value.split('.')
166 raise Invalid(self.message("bad_format", state, value=value), value, state)
170 if int(octet) < 0 or int(octet) > 255:
171 raise Invalid(self.message("illegal_octets", state, octet=octet), value, state)
173 # Splitting faild: wrong syntax
175 raise Invalid(self.message("bad_format", state), value, state)