Skip to content

Commit

Permalink
Add new IP field.
Browse files Browse the repository at this point in the history
  • Loading branch information
strichter committed Mar 2, 2007
1 parent 5582610 commit d19aece
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/z3c/schema/ip/README.txt
@@ -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
20 changes: 20 additions & 0 deletions src/z3c/schema/ip/__init__.py
@@ -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
48 changes: 48 additions & 0 deletions src/z3c/schema/ip/field.py
@@ -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)
28 changes: 28 additions & 0 deletions src/z3c/schema/ip/interfaces.py
@@ -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.""")
32 changes: 32 additions & 0 deletions src/z3c/schema/ip/tests.py
@@ -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')

0 comments on commit d19aece

Please sign in to comment.