Author: ianb
Date: 2008-11-14 16:57:15 -0700 (Fri, 14 Nov 2008)
New Revision: 3662

Modified:
   FormEncode/trunk/docs/modules/validators.txt
   FormEncode/trunk/docs/news.txt
   FormEncode/trunk/formencode/validators.py
Log:
Added formencode.validators.IPAddress from Leandro Lucarella (SF 2001367)

Modified: FormEncode/trunk/docs/modules/validators.txt
===================================================================
--- FormEncode/trunk/docs/modules/validators.txt        2008-11-14 23:48:20 UTC 
(rev 3661)
+++ FormEncode/trunk/docs/modules/validators.txt        2008-11-14 23:57:15 UTC 
(rev 3662)
@@ -68,6 +68,7 @@
 
 .. autoclass:: Email
 .. autoclass:: URL
+.. autoclass:: IPAddress
 .. autoclass:: CIDR
 .. autoclass:: MACAddress
 

Modified: FormEncode/trunk/docs/news.txt
===================================================================
--- FormEncode/trunk/docs/news.txt      2008-11-14 23:48:20 UTC (rev 3661)
+++ FormEncode/trunk/docs/news.txt      2008-11-14 23:57:15 UTC (rev 3662)
@@ -6,6 +6,9 @@
 svn trunk
 ---------
 
+* Added :class:`formencode.validators.IPAddress`, validating IP
+  addresses, from Leandro Lucarella.
+
 * Added :meth:`formencode.api.Invalid.__unicode__`
 
 * In :mod:`formencode.htmlfill` use a default encoding of utf8 when

Modified: FormEncode/trunk/formencode/validators.py
===================================================================
--- FormEncode/trunk/formencode/validators.py   2008-11-14 23:48:20 UTC (rev 
3661)
+++ FormEncode/trunk/formencode/validators.py   2008-11-14 23:57:15 UTC (rev 
3662)
@@ -2424,8 +2424,52 @@
             chr(random.randrange(256))
             for i in range(self.nonce_length)])
 
-class CIDR(FancyValidator):
+class IPAddress(FancyValidator):
     """
+    Formencode validator to check whether a string is a correct IP address
+
+    Examples::
+
+        >>> ip = IPAddress()
+        >>> ip.to_python('127.0.0.1')
+        '127.0.0.1'
+        >>> ip.to_python('299.0.0.1')
+        Traceback (most recent call last):
+            ...
+        Invalid: The octets must be within the range of 0-255 (not '299')
+        >>> ip.to_python('192.168.0.1/1')
+        Traceback (most recent call last):
+            ...
+        Invalid: Please enter a valid IP address (a.b.c.d)
+        >>> ip.to_python('asdf')
+        Traceback (most recent call last):
+            ...
+        Invalid: Please enter a valid IP address (a.b.c.d)
+    """
+    messages = {
+            'bad_format' : u'Please enter a valid IP address (a.b.c.d)',
+            'illegal_octets' : u'The octets must be within the range of 0-255 
(not %(octet)r)',
+            }
+
+    def validate_python(self, value, state):
+        try:
+            octets = value.split('.')
+
+            # Only 4 octets?
+            if len(octets) != 4:
+                raise Invalid(self.message("bad_format", state, value=value), 
value, state)
+
+            # Correct octets?
+            for octet in octets:
+                if int(octet) < 0 or int(octet) > 255:
+                    raise Invalid(self.message("illegal_octets", state, 
octet=octet), value, state)
+
+        # Splitting faild: wrong syntax
+        except ValueError:
+            raise Invalid(self.message("bad_format", state), value, state)
+
+class CIDR(IPAddress):
+    """
     Formencode validator to check whether a string is in correct CIDR
     notation (IP address, or IP address plus /mask)
 
@@ -2447,11 +2491,10 @@
             ...
         Invalid: Please enter a valid IP address (a.b.c.d) or IP network 
(a.b.c.d/e)
     """
-    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 
(not %(octet)r)',
-            'illegal_bits' : u'The network size (bits) must be within the 
range of 8-32 (not %(bits)r)',
-            }
+    messages = dict(IPAddress._messages,
+            bad_format = u'Please enter a valid IP address (a.b.c.d) or IP 
network (a.b.c.d/e)',
+            illegal_bits = u'The network size (bits) must be within the range 
of 8-32 (not %(bits)r)',
+            )
 
     def validate_python(self, value, state):
         try:
@@ -2461,24 +2504,16 @@
             else: # a.b.c.d
                 addr, bits = value, 32
 
-            octets = addr.split('.')
+            # Use IPAddress validator to validate the IP part
+            IPAddress.validate_python(self, addr, state)
 
-            # Only 4 octets?
-            if len(octets) != 4:
-                raise Invalid(self.message("not_cidr_format", state, 
value=value), value, state)
-
-            # Correct octets?
-            for octet in octets:
-                if int(octet) < 0 or int(octet) > 255:
-                    raise Invalid(self.message("illegal_octets", state, 
octet=octet), value, state)
-
             # Bits (netmask) correct?
             if int(bits) < 8 or int(bits) > 32:
                     raise Invalid(self.message("illegal_bits", state, 
bits=bits), value, state)
 
         # Splitting faild: wrong syntax
         except ValueError:
-            raise Invalid(self.message("not_cidr_format", state), value, state)
+            raise Invalid(self.message("bad_format", state), value, state)
 
 class MACAddress(FancyValidator):
     """


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
FormEncode-CVS mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/formencode-cvs

Reply via email to