Skip to content

Commit

Permalink
Improved Py3 support (Rework #63) (#70)
Browse files Browse the repository at this point in the history
Adds flake8 checks to test runs
Updates example/confbuilder.py file for Py3 syntax
Changes to type checking in IPy.py
  • Loading branch information
autocracy authored Jun 23, 2020
1 parent b02fb5d commit b8eb507
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ python:
- "3.7-dev"
- "3.8-dev"
- "nightly"
before_install: pip install flake8
before_script: flake8 . --count --select=E9,F63,F72,F82 --show-source --statistics
install: pip install pytest
script: pytest -k "not fuzz"
10 changes: 5 additions & 5 deletions IPy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
__version__ = '1.00'

import bisect
import sys
import types
try:
import collections.abc as collections_abc
Expand Down Expand Up @@ -124,13 +123,14 @@
IPV6_TEST_MAP = 0xffffffffffffffffffffffff00000000
IPV6_MAP_MASK = 0x00000000000000000000ffff00000000

if sys.version_info >= (3,):
try:
INT_TYPES = (int, long)
STR_TYPES = (str, unicode)
xrange
except NameError:
INT_TYPES = (int,)
STR_TYPES = (str,)
xrange = range
else:
INT_TYPES = (int, long)
STR_TYPES = (str, unicode)


class IPint(object):
Expand Down
71 changes: 35 additions & 36 deletions example/confbuilder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# This is a hack I use to generate my tinydns configuration
# It serves as e test for converting from Perl Net::IP to
# Python and IPy
Expand All @@ -15,11 +16,11 @@
'ns.dorsch.org': '195.143.234.25',
'ns.c0re.jp': '217.6.214.130'}

print "# *** nameservers ***"
for x in ns.keys():
print "=%s:%s" % (x, ns[x])
print("# *** nameservers ***")
for x in ns:
print("=%s:%s" % (x, ns[x]))

print "\n# *** domains ***"
print("\n# *** domains ***")

fd = open('domains')

Expand All @@ -28,13 +29,13 @@
if x[-1] == '\n':
x = x[:-1]
(domain, owner) = x.split(':')
print "'%s:Contact for this domain is %s" % (domain, owner)
for y in ns.keys():
print ".%s::%s" % (domain, y)
print("'%s:Contact for this domain is %s" % (domain, owner))
for y in ns:
print(".%s::%s" % (domain, y))

fd.close()

print "\n# *** Networks ***"
print("\n# *** Networks ***")

fd = open('networks')
ip6map = {}
Expand All @@ -47,12 +48,12 @@
if len(x) > 0 and x[0] != '#':
nets = x.split(',')
name = nets.pop(0)
print "# Network: %s" % name
print("# Network: %s" % name)
for y in nets:
ip = IPy.IP(y)
print "# Address range: %s (%s), %d addresses" % (ip.strCompressed(), ip.iptype(), ip.len())
print "=net.%s:%s" % (name, ip.net())
print "=broadcast.%s:%s" % (name, ip.broadcast())
print("# Address range: %s (%s), %d addresses" % (ip.strCompressed(), ip.iptype(), ip.len()))
print("=net.%s:%s" % (name, ip.net()))
print("=broadcast.%s:%s" % (name, ip.broadcast()))

if ip.version() == 4:
for z in ip:
Expand All @@ -61,14 +62,14 @@
rmap[z.int()] = z.strBin() + "." + name
else:
# IPv6
for z in ns.keys():
for z in ns:
for v in ip.reverseName():
print ".%s::%s" % (v, z)
print(".%s::%s" % (v, z))
ip6map[ip.strFullsize(0)] = name

fd.close()

print "\n# *** hosts ***"
print("\n# *** hosts ***")

fd = open('hosts')

Expand All @@ -77,12 +78,12 @@
x = x[:-1]
if x != '' and x[0] != '#':
if "@Z'.".find(x[0]) >= 0:
print x
print(x)
else:
if "=+'".find(x[0]) >= 0:
i = x.split(':')
rmap[IPy.IP(i[1]).int()] = ''
print x
print(x)
else:
x = x[1:]
x += '||||'
Expand All @@ -107,35 +108,33 @@
ip = IPy.IP(y)
if ip.version() == 4:
# IPv4 is easy
if not nmap.has_key(ip.int()):
print >>sys.stderr, "*** warning: no network for %s (%s) - ignoring" % (y, name)
print "# no network for %s (%s)" % (y, name)
if ip.int() not in nmap:
print("*** warning: no network for %s (%s) - ignoring" % (y, name), file=sys.stderr)
print("# no network for %s (%s)" % (y, name))
else:
print "=%s.%s:%s" % (name, nmap[ip.int()], y)
print "'%s.%s:Host contact is %s" % (name, nmap[ip.int()], admin)
print("=%s.%s:%s" % (name, nmap[ip.int()], y))
print("'%s.%s:Host contact is %s" % (name, nmap[ip.int()], admin))
rmap[ip.int()] = ''
for z in aliases:
print "+%s:%s" % (z, ip)
print "'%s:Host contact is %s" % (z, admin)
print("+%s:%s" % (z, ip))
print("'%s:Host contact is %s" % (z, admin))
else:
#IPv6 here
net = ip.strFullsize(0)
net = net[:19] + ':0000:0000:0000:0000'
if ip6map.has_key(net):
print >>sys.stderr, "*** warning: no network for %s (%s) - ignoring" % (ip, name)
print "# no network for %s (%s) - ignoring" % (ip, name)
if net in ip6map:
print("*** warning: no network for %s (%s) - ignoring" % (ip, name), file=sys.stderr)
print("# no network for %s (%s) - ignoring" % (ip, name))
else:
print "6%s.%s:%s"; (name, ip6map[net], ip.strHex()[2:])
print("6%s.%s:%s" % (name, ip6map[net], ip.strHex()[2:]))
for z in aliases:
print "3%s:%s" % (name, ip.strHex()[2:])
print "'%s:Host contact is %s" % (name, admin)
print("3%s:%s" % (name, ip.strHex()[2:]))
print("'%s:Host contact is %s" % (name, admin))

fd.close()

print "\n# *** reverse lookup ***"
k = nmap.keys()
k.sort()
for x in k:
if rmap.has_key(x) and rmap[x] != '':
print "=%s:%s" % (rmap[x], str(IPy.IP(x)))
print("\n# *** reverse lookup ***")
for x in sorted(nmap):
if rmap.get(x):
print("=%s:%s" % (rmap[x], str(IPy.IP(x))))

0 comments on commit b8eb507

Please sign in to comment.