Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.""") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |