Skip to content

Commit

Permalink
Fix Pull Request #57 to work under Python 2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
paulc committed Jan 2, 2024
1 parent fd4e6f2 commit cc39f47
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions dnslib/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -2264,9 +2264,9 @@ class LOC(RD):
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1456
;; flags: rd; QUERY: 0, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; ANSWER SECTION:
area51.local. 60 IN LOC 37 14 12.094 N 115 48 14.649 W 1381.00m
area51.local. 60 IN LOC 37 N 115 48 W 1381.00m
area51.local. 60 IN LOC 37 14 12.094 N 115 48 14.649 W 1381.00m
area51.local. 60 IN LOC 37 14 12.094 N 115 48 14.649 W 1381.00m
area51.local. 60 IN LOC 37 N 115 48 W 1381.00m
area51.local. 60 IN LOC 37 14 12.094 N 115 48 14.649 W 1381.00m
"""

@classmethod
Expand All @@ -2289,10 +2289,11 @@ def parse(cls, buffer, length):
@classmethod
def fromZone(cls, rd, origin=None):
args = []
idx = 0
# We still support Python 2.7 so use nonlocal workaround
class context:
idx = 0
tofloat = lambda x: float(x[:-1]) # get float from "100.0m"
def todecimal(chars):
nonlocal idx
decimal = 0.0
multiplier = 1
for c in chars:
Expand All @@ -2302,19 +2303,19 @@ def todecimal(chars):
multiplier = -1
break
else:
raise DNSError(f'Missing cardinality [{chars}]')
for n, d in zip(rd[idx:nxt], (1, 60, 3600)):
raise DNSError('Missing cardinality [{chars}]'.format(chars=chars))
for n, d in zip(rd[context.idx:nxt], (1, 60, 3600)):
decimal += float(n) / d
idx = nxt + 1
context.idx = nxt + 1
return decimal * multiplier

args.append(todecimal('NS'))
args.append(todecimal('EW'))

try:
while True:
args.append(tofloat(rd[idx]))
idx += 1
args.append(tofloat(rd[context.idx]))
context.idx += 1
except IndexError:
return cls(*args)

Expand Down Expand Up @@ -2361,28 +2362,28 @@ def _reprcoord(value, c):
base = abs(pow(2, 31) - value)
d = base // 3600000
m = base % 3600000 // 60000
s = base % 3600000 % 60000 / 1000
s = base % 3600000 % 60000 / 1000.0

if int(s) == 0:
if m == 0:
return f'{d} {c}'
return '{d} {c}'.format(c=c,d=d)
else:
return f'{d} {m} {c}'
return f'{d} {m} {s:.3f} {c}'
return '{d} {m} {c}'.format(d=d,m=m,c=c)
return '{d} {m} {s:.3f} {c}'.format(d=d,m=m,s=s,c=c)

def __repr__(self):
DEFAULT_SIZ = 0x12 # 1m
DEFAULT_HP = 0x16 # 10,000m
DEFAULT_VP = 0x13 # 10m

result = f'{self.lat} {self.lon} {self.alt:.2f}m'
result = '{self.lat} {self.lon} {self.alt:.2f}m'.format(self=self)

if self._vp != DEFAULT_VP:
result += f' {self.siz:.2f}m {self.hp:.2f}m {self.vp:.2f}m'
result += ' {self.siz:.2f}m {self.hp:.2f}m {self.vp:.2f}m'.format(self=self)
elif self._hp != DEFAULT_HP:
result += f' {self.siz:.2f}m {self.hp:.2f}m'
result += ' {self.siz:.2f}m {self.hp:.2f}m'.format(self=self)
elif self._siz != DEFAULT_SIZ:
result += f' {self.siz:.2f}m'
result += ' {self.siz:.2f}m'.format(self=self)

return result

Expand All @@ -2395,7 +2396,7 @@ def __tosiz(v):
while v >= 10 and e < 9:
v /= 10
e += 1
v = round(v)
v = int(round(v))
if v >= 10:
raise DNSError("Value out of range")
return v << 4 | e
Expand Down

0 comments on commit cc39f47

Please sign in to comment.