On Thu, Oct 18, 2007 at 10:28:52PM -0400, Yannick Gingras wrote:
> Christoph Haas <[EMAIL PROTECTED]> writes:
>
> > I could contribute validators for:
> >
> > - valid IPv4 network/address specifications
> > (e.g. "10.0.0.0/8" or "192.168.25.1" but not "1.2.3.4/123")
>
> Here is what I use for IPv4 addr ranges:
>
> from formencode import validators
> import iplib
>
> class CIDRValidator(validators.UnicodeString):
> """ An IP range in CIDR notation. Single IP accepted without the
> '/32' qualifier. An iplib.CIDR object is returned. """
> def _to_python(self, value, state):
> if iplib.is_dot(value):
> return iplib.CIDR(value+"/32")
> try:
> return iplib.CIDR(value)
> except ValueError:
> raise validators.Invalid(
> "Must be an IP range in CIDR notation. Ex: '192.168.0.0/24'",
> val,
> state)
>
> It requires iplib which plays badly with easy_install but it's a
> single python file so I repackage it in my project.
I did it without iplib:
class Cidr(formencode.FancyValidator):
"""
Formencode validator to check whether a string is in correct CIDR
notation.
"""
messages = {
'not_cidr_format' : u'Please enter a valid IP address (a.b.c.d) or
IP network (a.b.c.d/e)',
'illegal_octets' : u'The octets must be within the range of 0-255',
'illegal_bits' : u'The network size (bits) must be within the range
of 8-32',
}
def validate_python(self, value, state):
try:
# Split into octets and bits
if '/' in value: # a.b.c.d/e
addr, bits = value.split('/')
else: # a.b.c.d
addr, bits = value, 32
octets = addr.split('.')
# Only 4 octets?
if len(octets) != 4:
raise formencode.Invalid(self.message("not_cidr_format",
state), value, state)
# Correct octets?
for octet in octets:
if int(octet) < 0 or int(octet) > 255:
raise formencode.Invalid(self.message("illegal_octets",
octet), value, state)
# Bits (netmask) correct?
if int(bits) < 8 or int(bits) > 32:
raise formencode.Invalid(self.message("illegal_bits",
bits), value, state)
# Splitting faild: wrong syntax
except ValueError:
raise formencode.Invalid(self.message("not_cidr_format", state),
value, state)
----------------------------------------
And my MAC/hardware address validator:
class MacAddress(formencode.FancyValidator):
"""
Formencode validator to check whether a string is a correct hardware
(MAC) address.
"""
messages = {
'wrong_format' : u'Please enter a valid hardware (MAC) address (12
hex digits)',
}
def validate_python(self, value, state):
address = value.replace(':','') # remove colons
if len(address)!=12:
raise formencode.Invalid(self.message("wrong_format", state),
address, state)
for char in address:
if char not in string.hexdigits:
raise formencode.Invalid(self.message("wrong_format", state),
address, state)
----------------------------------------
Feel free to use it if it's good enough. MIT license or whatever.
Cheers
Christoph
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---