Skip to content

Commit b8eb507

Browse files
authored
Improved Py3 support (Rework #63) (#70)
Adds flake8 checks to test runs Updates example/confbuilder.py file for Py3 syntax Changes to type checking in IPy.py
1 parent b02fb5d commit b8eb507

File tree

3 files changed

+42
-41
lines changed

3 files changed

+42
-41
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ python:
99
- "3.7-dev"
1010
- "3.8-dev"
1111
- "nightly"
12+
before_install: pip install flake8
13+
before_script: flake8 . --count --select=E9,F63,F72,F82 --show-source --statistics
1214
install: pip install pytest
1315
script: pytest -k "not fuzz"

IPy.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
__version__ = '1.00'
1010

1111
import bisect
12-
import sys
1312
import types
1413
try:
1514
import collections.abc as collections_abc
@@ -124,13 +123,14 @@
124123
IPV6_TEST_MAP = 0xffffffffffffffffffffffff00000000
125124
IPV6_MAP_MASK = 0x00000000000000000000ffff00000000
126125

127-
if sys.version_info >= (3,):
126+
try:
127+
INT_TYPES = (int, long)
128+
STR_TYPES = (str, unicode)
129+
xrange
130+
except NameError:
128131
INT_TYPES = (int,)
129132
STR_TYPES = (str,)
130133
xrange = range
131-
else:
132-
INT_TYPES = (int, long)
133-
STR_TYPES = (str, unicode)
134134

135135

136136
class IPint(object):

example/confbuilder.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
# This is a hack I use to generate my tinydns configuration
23
# It serves as e test for converting from Perl Net::IP to
34
# Python and IPy
@@ -15,11 +16,11 @@
1516
'ns.dorsch.org': '195.143.234.25',
1617
'ns.c0re.jp': '217.6.214.130'}
1718

18-
print "# *** nameservers ***"
19-
for x in ns.keys():
20-
print "=%s:%s" % (x, ns[x])
19+
print("# *** nameservers ***")
20+
for x in ns:
21+
print("=%s:%s" % (x, ns[x]))
2122

22-
print "\n# *** domains ***"
23+
print("\n# *** domains ***")
2324

2425
fd = open('domains')
2526

@@ -28,13 +29,13 @@
2829
if x[-1] == '\n':
2930
x = x[:-1]
3031
(domain, owner) = x.split(':')
31-
print "'%s:Contact for this domain is %s" % (domain, owner)
32-
for y in ns.keys():
33-
print ".%s::%s" % (domain, y)
32+
print("'%s:Contact for this domain is %s" % (domain, owner))
33+
for y in ns:
34+
print(".%s::%s" % (domain, y))
3435

3536
fd.close()
3637

37-
print "\n# *** Networks ***"
38+
print("\n# *** Networks ***")
3839

3940
fd = open('networks')
4041
ip6map = {}
@@ -47,12 +48,12 @@
4748
if len(x) > 0 and x[0] != '#':
4849
nets = x.split(',')
4950
name = nets.pop(0)
50-
print "# Network: %s" % name
51+
print("# Network: %s" % name)
5152
for y in nets:
5253
ip = IPy.IP(y)
53-
print "# Address range: %s (%s), %d addresses" % (ip.strCompressed(), ip.iptype(), ip.len())
54-
print "=net.%s:%s" % (name, ip.net())
55-
print "=broadcast.%s:%s" % (name, ip.broadcast())
54+
print("# Address range: %s (%s), %d addresses" % (ip.strCompressed(), ip.iptype(), ip.len()))
55+
print("=net.%s:%s" % (name, ip.net()))
56+
print("=broadcast.%s:%s" % (name, ip.broadcast()))
5657

5758
if ip.version() == 4:
5859
for z in ip:
@@ -61,14 +62,14 @@
6162
rmap[z.int()] = z.strBin() + "." + name
6263
else:
6364
# IPv6
64-
for z in ns.keys():
65+
for z in ns:
6566
for v in ip.reverseName():
66-
print ".%s::%s" % (v, z)
67+
print(".%s::%s" % (v, z))
6768
ip6map[ip.strFullsize(0)] = name
6869

6970
fd.close()
7071

71-
print "\n# *** hosts ***"
72+
print("\n# *** hosts ***")
7273

7374
fd = open('hosts')
7475

@@ -77,12 +78,12 @@
7778
x = x[:-1]
7879
if x != '' and x[0] != '#':
7980
if "@Z'.".find(x[0]) >= 0:
80-
print x
81+
print(x)
8182
else:
8283
if "=+'".find(x[0]) >= 0:
8384
i = x.split(':')
8485
rmap[IPy.IP(i[1]).int()] = ''
85-
print x
86+
print(x)
8687
else:
8788
x = x[1:]
8889
x += '||||'
@@ -107,35 +108,33 @@
107108
ip = IPy.IP(y)
108109
if ip.version() == 4:
109110
# IPv4 is easy
110-
if not nmap.has_key(ip.int()):
111-
print >>sys.stderr, "*** warning: no network for %s (%s) - ignoring" % (y, name)
112-
print "# no network for %s (%s)" % (y, name)
111+
if ip.int() not in nmap:
112+
print("*** warning: no network for %s (%s) - ignoring" % (y, name), file=sys.stderr)
113+
print("# no network for %s (%s)" % (y, name))
113114
else:
114-
print "=%s.%s:%s" % (name, nmap[ip.int()], y)
115-
print "'%s.%s:Host contact is %s" % (name, nmap[ip.int()], admin)
115+
print("=%s.%s:%s" % (name, nmap[ip.int()], y))
116+
print("'%s.%s:Host contact is %s" % (name, nmap[ip.int()], admin))
116117
rmap[ip.int()] = ''
117118
for z in aliases:
118-
print "+%s:%s" % (z, ip)
119-
print "'%s:Host contact is %s" % (z, admin)
119+
print("+%s:%s" % (z, ip))
120+
print("'%s:Host contact is %s" % (z, admin))
120121
else:
121122
#IPv6 here
122123
net = ip.strFullsize(0)
123124
net = net[:19] + ':0000:0000:0000:0000'
124-
if ip6map.has_key(net):
125-
print >>sys.stderr, "*** warning: no network for %s (%s) - ignoring" % (ip, name)
126-
print "# no network for %s (%s) - ignoring" % (ip, name)
125+
if net in ip6map:
126+
print("*** warning: no network for %s (%s) - ignoring" % (ip, name), file=sys.stderr)
127+
print("# no network for %s (%s) - ignoring" % (ip, name))
127128
else:
128-
print "6%s.%s:%s"; (name, ip6map[net], ip.strHex()[2:])
129+
print("6%s.%s:%s" % (name, ip6map[net], ip.strHex()[2:]))
129130
for z in aliases:
130-
print "3%s:%s" % (name, ip.strHex()[2:])
131-
print "'%s:Host contact is %s" % (name, admin)
131+
print("3%s:%s" % (name, ip.strHex()[2:]))
132+
print("'%s:Host contact is %s" % (name, admin))
132133

133134
fd.close()
134135

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

0 commit comments

Comments
 (0)