[Checkins] SVN: z3c.schema/trunk/src/z3c/schema/ip/ Add new IP field.

Stephan Richter srichter at cosmos.phy.tufts.edu
Fri Mar 2 08:13:34 EST 2007


Log message for revision 72956:
  Add new IP field.
  

Changed:
  A   z3c.schema/trunk/src/z3c/schema/ip/
  A   z3c.schema/trunk/src/z3c/schema/ip/README.txt
  A   z3c.schema/trunk/src/z3c/schema/ip/__init__.py
  A   z3c.schema/trunk/src/z3c/schema/ip/field.py
  A   z3c.schema/trunk/src/z3c/schema/ip/interfaces.py
  A   z3c.schema/trunk/src/z3c/schema/ip/tests.py

-=-
Added: z3c.schema/trunk/src/z3c/schema/ip/README.txt
===================================================================
--- z3c.schema/trunk/src/z3c/schema/ip/README.txt	2007-03-02 11:35:06 UTC (rev 72955)
+++ z3c.schema/trunk/src/z3c/schema/ip/README.txt	2007-03-02 13:13:33 UTC (rev 72956)
@@ -0,0 +1,57 @@
+================
+IP Address Field
+================
+
+This module provides a field for IP addresses. Let's first generate an IP
+field:
+
+  >>> from z3c.schema import ip
+  >>> myip = ip.IPAddress()
+
+Now make sure the IP addresses validate:
+
+  >>> myip.validate(u'10.0.0.1')
+  Traceback (most recent call last):
+  ...
+  WrongType: (u'10.0.0.1', <type 'str'>)
+
+  >>> myip.validate('12.123.231.wee')
+  Traceback (most recent call last):
+  ...
+  NotValidIPAdress: 12.123.231.wee
+
+  >>> myip.validate('10.0.0.1')
+
+Since the field uses a simple function to validate its IP addresses, it is
+easier to use it for the tests:
+
+  >>> from z3c.schema.ip import isValidIPAddress
+  >>> isValidIPAddress('0.0.0.0')
+  True
+  >>> isValidIPAddress('255.255.255.255')
+  True
+
+  # Number of pieces failures
+
+  >>> isValidIPAddress('12.3.1')
+  False
+  >>> isValidIPAddress('1.0.0.0.0')
+  False
+  >>> isValidIPAddress('1.0.0.0.')
+  False
+
+  # Not integers failures
+
+  >>> isValidIPAddress('x.0.0.0')
+  False
+  >>> isValidIPAddress('0x8.0.0.0')
+  False
+
+  # Not in range failures
+
+  >>> isValidIPAddress('-1.0.0.0')
+  False
+  >>> isValidIPAddress('256.0.0.0')
+  False
+  >>> isValidIPAddress('1.-1.256.0')
+  False


Property changes on: z3c.schema/trunk/src/z3c/schema/ip/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.schema/trunk/src/z3c/schema/ip/__init__.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/ip/__init__.py	2007-03-02 11:35:06 UTC (rev 72955)
+++ z3c.schema/trunk/src/z3c/schema/ip/__init__.py	2007-03-02 13:13:33 UTC (rev 72956)
@@ -0,0 +1,20 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+
+from z3c.schema.ip.interfaces import *
+from z3c.schema.ip.field import isValidIPAddress
+from z3c.schema.ip.field import IPAddress


Property changes on: z3c.schema/trunk/src/z3c/schema/ip/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.schema/trunk/src/z3c/schema/ip/field.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/ip/field.py	2007-03-02 11:35:06 UTC (rev 72955)
+++ z3c.schema/trunk/src/z3c/schema/ip/field.py	2007-03-02 13:13:33 UTC (rev 72956)
@@ -0,0 +1,48 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import zope.interface
+import zope.schema
+
+from z3c.schema.ip import interfaces
+
+def isValidIPAddress(addr):
+    """Returns True if the IP address is valid and False if not."""
+    # Check that we have four pieces separated by .
+    pieces = addr.split('.')
+    if len(pieces) != 4:
+        return False
+    # Now check that each piece is an integer between 0 and 255
+    for piece in pieces:
+        try:
+            num = int(piece)
+        except ValueError:
+            return False
+        if not (num >= 0 and num <= 255):
+            return False
+    return True
+
+
+class IPAddress(zope.schema.BytesLine):
+    """A valid IP address."""
+    zope.interface.implements(interfaces.IIPAddress)
+
+    def _validate(self, value):
+        super(IPAddress, self)._validate(value)
+
+        if not isValidIPAddress(value):
+            raise interfaces.NotValidIPAdress(value)


Property changes on: z3c.schema/trunk/src/z3c/schema/ip/field.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.schema/trunk/src/z3c/schema/ip/interfaces.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/ip/interfaces.py	2007-03-02 11:35:06 UTC (rev 72955)
+++ z3c.schema/trunk/src/z3c/schema/ip/interfaces.py	2007-03-02 13:13:33 UTC (rev 72956)
@@ -0,0 +1,28 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""IP Address Field Interfaces
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import zope.schema
+import zope.schema.interfaces
+from z3c.i18n import MessageFactory as _
+
+class IIPAddress(zope.schema.interfaces.IBytesLine):
+    """A valid IP address field."""
+
+class NotValidIPAdress(zope.schema.ValidationError):
+    __doc__ = _("""Not a valid IP address.""")


Property changes on: z3c.schema/trunk/src/z3c/schema/ip/interfaces.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.schema/trunk/src/z3c/schema/ip/tests.py
===================================================================
--- z3c.schema/trunk/src/z3c/schema/ip/tests.py	2007-03-02 11:35:06 UTC (rev 72955)
+++ z3c.schema/trunk/src/z3c/schema/ip/tests.py	2007-03-02 13:13:33 UTC (rev 72956)
@@ -0,0 +1,32 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import unittest
+from zope.testing import doctest
+from zope.testing.doctestunit import DocFileSuite
+
+
+def test_suite():
+    return unittest.TestSuite((
+        DocFileSuite('README.txt',
+            optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,),
+        ))
+
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: z3c.schema/trunk/src/z3c/schema/ip/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id



More information about the Checkins mailing list