Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
*.egg-info
*.pyc
.coverage
.venv
build
dist
extras
iptools.egg-info
/.coverage
/.tox/
/.venv/
/build/
/dist/
/extras/
setuptools-*.egg
20 changes: 13 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
language: python
python:
- "2.6"
- "2.7"
- "3.2"
- "3.3"
- "3.4"
- "3.5"
- "pypy"
- "pypy3"
sudo: false
matrix:
fast_finish: true

install:
- pip install .
- pip install -r tests/requirements.txt
- pip install wheel tox-travis
- python setup.py install bdist_wheel
- pip install ./dist/iptools-*.whl
script:
- flake8
- nosetests
- tox
- tox --installpkg ./dist/iptools-*.whl

notifications:
email:
- travis-ci+python-iptools@bd808.com
15 changes: 7 additions & 8 deletions iptools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
__version__ = '0.7.0-dev'

__all__ = (
'IpRange',
'IpRangeList',
)


# sniff for python2.x / python3k compatibility "fixes'
try:
Expand All @@ -51,10 +44,16 @@ def next(iterable):
Sequence = object
# end compatibility "fixes'


from . import ipv4
from . import ipv6

__version__ = '0.7.0-dev'

__all__ = (
'IpRange',
'IpRangeList',
)


def _address2long(address):
"""
Expand Down
78 changes: 40 additions & 38 deletions iptools/ipv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,40 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import re

# sniff for python2.x / python3k compatibility "fixes'
try:
basestring = basestring
except NameError:
# 'basestring' is undefined, must be python3k
basestring = str

try:
bin = bin
except NameError:
# builtin bin function doesn't exist
def bin(x):
"""
From http://code.activestate.com/recipes/219300/#c7
"""
if x < 0:
return '-' + bin(-x)
out = []
if x == 0:
out.append('0')
while x > 0:
out.append('01'[x & 1])
x >>= 1
pass
try:
return '0b' + ''.join(reversed(out))
except NameError:
out.reverse()
return '0b' + ''.join(out)
# end bin
# end compatibility "fixes'

__all__ = (
'cidr2block',
'hex2ip',
Expand Down Expand Up @@ -60,43 +94,6 @@
'TEST_NET_3',
)


import re


# sniff for python2.x / python3k compatibility "fixes'
try:
basestring = basestring
except NameError:
# 'basestring' is undefined, must be python3k
basestring = str

try:
bin = bin
except NameError:
# builtin bin function doesn't exist
def bin(x):
"""
From http://code.activestate.com/recipes/219300/#c7
"""
if x < 0:
return '-' + bin(-x)
out = []
if x == 0:
out.append('0')
while x > 0:
out.append('01'[x & 1])
x >>= 1
pass
try:
return '0b' + ''.join(reversed(out))
except NameError:
out.reverse()
return '0b' + ''.join(out)
# end bin
# end compatibility "fixes'


#: Regex for validating an IPv4 address
_DOTTED_QUAD_RE = re.compile(r'^(\d{1,3}\.){0,3}\d{1,3}$')

Expand Down Expand Up @@ -285,6 +282,10 @@ def validate_netmask(s):
True
>>> validate_netmask('128.0.0.1')
False
>>> validate_netmask('1.255.255.0')
False
>>> validate_netmask('0.255.255.0')
False


:param s: String to validate as a dotted-quad notation netmask.
Expand All @@ -293,7 +294,8 @@ def validate_netmask(s):
:raises: TypeError
"""
if validate_ip(s):
mask = bin(ip2network(s))[2:]
# Convert to binary string, strip '0b' prefix, 0 pad to 32 bits
mask = bin(ip2network(s))[2:].zfill(32)
# all left most bits must be 1, all right most must be 0
seen0 = False
for c in mask:
Expand Down
8 changes: 3 additions & 5 deletions iptools/ipv6.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import re
from . import ipv4

__all__ = (
'cidr2block',
'ip2long',
Expand Down Expand Up @@ -52,11 +55,6 @@
'UNSPECIFIED_ADDRESS',
)


import re
from . import ipv4


#: Regex for validating an IPv6 in hex notation
_HEX_RE = re.compile(r'^([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}$')

Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
[nosetests]
verbosity=2
detailed-errors=1
with-coverage=1
with-doctest=1
cover-package=iptools
cover-html=1
cover-html-dir=docs/_build/cover
cover-branches=1

[flake8]
ignore=F821,E402
count=1
show-pep8=1
show-source=1
statistics=1
exclude=build,dist,docs,*.egg,*.egg-info
exclude=.tox,.venv,build,dist,docs,*.egg,*.egg-info

[wheel]
universal = 1
8 changes: 8 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tox]
envlist = py27, py34, py35, pypy, pypy3

[testenv]
deps = -r{toxinidir}/tests/requirements.txt
commands =
flake8
nosetests