Skip to content

Commit 173350e

Browse files
committed
Minor style things for Zeroconf (use True/False instead of 1/0, etc.).
1 parent 3e718b5 commit 173350e

1 file changed

Lines changed: 46 additions & 59 deletions

File tree

‎Zeroconf.py‎

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
# hook for threads
9292

93-
globals()['_GLOBAL_DONE'] = 0
93+
_GLOBAL_DONE = False
9494

9595
# Some timing constants
9696

@@ -218,11 +218,10 @@ def __init__(self, name, type, clazz):
218218

219219
def __eq__(self, other):
220220
"""Equality test on name, type, and class"""
221-
if isinstance(other, DNSEntry):
222-
return (self.name == other.name and
223-
self.type == other.type and
224-
self.clazz == other.clazz)
225-
return 0
221+
return (isinstance(other, DNSEntry) and
222+
self.name == other.name and
223+
self.type == other.type and
224+
self.clazz == other.clazz)
226225

227226
def __ne__(self, other):
228227
"""Non-equality test"""
@@ -286,24 +285,20 @@ def __init__(self, name, type, clazz, ttl):
286285

287286
def __eq__(self, other):
288287
"""Tests equality as per DNSRecord"""
289-
if isinstance(other, DNSRecord):
290-
return DNSEntry.__eq__(self, other)
291-
return 0
288+
return isinstance(other, DNSRecord) and DNSEntry.__eq__(self, other)
292289

293290
def suppressedBy(self, msg):
294291
"""Returns true if any answer in a message can suffice for the
295292
information held in this record."""
296293
for record in msg.answers:
297294
if self.suppressedByAnswer(record):
298-
return 1
299-
return 0
295+
return True
296+
return False
300297

301298
def suppressedByAnswer(self, other):
302299
"""Returns true if another record has same name, type and class,
303300
and if its TTL is at least half of this record's."""
304-
if self == other and other.ttl > (self.ttl / 2):
305-
return 1
306-
return 0
301+
return self == other and other.ttl > (self.ttl / 2)
307302

308303
def getExpirationTime(self, percent):
309304
"""Returns the time at which this record will have expired
@@ -351,9 +346,7 @@ def write(self, out):
351346

352347
def __eq__(self, other):
353348
"""Tests equality on address"""
354-
if isinstance(other, DNSAddress):
355-
return self.address == other.address
356-
return 0
349+
return isinstance(other, DNSAddress) and self.address == other.address
357350

358351
def __repr__(self):
359352
"""String representation"""
@@ -377,9 +370,8 @@ def write(self, out):
377370

378371
def __eq__(self, other):
379372
"""Tests equality on cpu and os"""
380-
if isinstance(other, DNSHinfo):
381-
return self.cpu == other.cpu and self.os == other.os
382-
return 0
373+
return (isinstance(other, DNSHinfo) and
374+
self.cpu == other.cpu and self.os == other.os)
383375

384376
def __repr__(self):
385377
"""String representation"""
@@ -398,9 +390,7 @@ def write(self, out):
398390

399391
def __eq__(self, other):
400392
"""Tests equality on alias"""
401-
if isinstance(other, DNSPointer):
402-
return self.alias == other.alias
403-
return 0
393+
return isinstance(other, DNSPointer) and self.alias == other.alias
404394

405395
def __repr__(self):
406396
"""String representation"""
@@ -419,9 +409,7 @@ def write(self, out):
419409

420410
def __eq__(self, other):
421411
"""Tests equality on text"""
422-
if isinstance(other, DNSText):
423-
return self.text == other.text
424-
return 0
412+
return isinstance(other, DNSText) and self.text == other.text
425413

426414
def __repr__(self):
427415
"""String representation"""
@@ -449,12 +437,11 @@ def write(self, out):
449437

450438
def __eq__(self, other):
451439
"""Tests equality on priority, weight, port and server"""
452-
if isinstance(other, DNSService):
453-
return (self.priority == other.priority and
454-
self.weight == other.weight and
455-
self.port == other.port and
456-
self.server == other.server)
457-
return 0
440+
return (isinstance(other, DNSService) and
441+
self.priority == other.priority and
442+
self.weight == other.weight and
443+
self.port == other.port and
444+
self.server == other.server)
458445

459446
def __repr__(self):
460447
"""String representation"""
@@ -580,7 +567,7 @@ def readName(self):
580567
next = -1
581568
first = off
582569

583-
while 1:
570+
while True:
584571
len = ord(self.data[off])
585572
off += 1
586573
if len == 0:
@@ -611,7 +598,7 @@ class DNSOutgoing(object):
611598
"""Object representation of an outgoing packet"""
612599

613600
def __init__(self, flags, multicast = 1):
614-
self.finished = 0
601+
self.finished = False
615602
self.id = 0
616603
self.multicast = multicast
617604
self.flags = flags
@@ -743,7 +730,7 @@ def packet(self):
743730
No further parts should be added to the packet once this
744731
is done."""
745732
if not self.finished:
746-
self.finished = 1
733+
self.finished = True
747734
for question in self.questions:
748735
self.writeQuestion(question)
749736
for answer, time in self.answers:
@@ -839,7 +826,7 @@ def __init__(self, zc):
839826
self.start()
840827

841828
def run(self):
842-
while not globals()['_GLOBAL_DONE']:
829+
while not _GLOBAL_DONE:
843830
rs = self.getReaders()
844831
if len(rs) == 0:
845832
# No sockets to manage, but we wait for the timeout
@@ -933,9 +920,9 @@ def __init__(self, zc):
933920
self.start()
934921

935922
def run(self):
936-
while 1:
923+
while True:
937924
self.zc.wait(10 * 1000)
938-
if globals()['_GLOBAL_DONE']:
925+
if _GLOBAL_DONE:
939926
return
940927
now = currentTimeMillis()
941928
for record in self.zc.cache.entries():
@@ -962,7 +949,7 @@ def __init__(self, zc, type, listener):
962949
self.delay = _BROWSER_TIME
963950
self.list = []
964951

965-
self.done = 0
952+
self.done = False
966953

967954
self.zc.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN))
968955
self.start()
@@ -995,16 +982,16 @@ def updateRecord(self, zc, now, record):
995982
self.nextTime = expires
996983

997984
def cancel(self):
998-
self.done = 1
985+
self.done = True
999986
self.zc.notifyAll()
1000987

1001988
def run(self):
1002-
while 1:
989+
while True:
1003990
event = None
1004991
now = currentTimeMillis()
1005992
if len(self.list) == 0 and self.nextTime > now:
1006993
self.zc.wait(self.nextTime - now)
1007-
if globals()['_GLOBAL_DONE'] or self.done:
994+
if _GLOBAL_DONE or self.done:
1008995
return
1009996
now = currentTimeMillis()
1010997

@@ -1097,18 +1084,16 @@ def setText(self, text):
10971084
index += length
10981085

10991086
for s in strs:
1100-
eindex = s.find('=')
1101-
if eindex == -1:
1102-
# No equals sign at all
1103-
key = s
1104-
value = 0
1105-
else:
1106-
key = s[:eindex]
1107-
value = s[eindex+1:]
1087+
try:
1088+
key, value = s.split('=', 1)
11081089
if value == 'true':
1109-
value = 1
1090+
value = True
11101091
elif value == 'false' or not value:
1111-
value = 0
1092+
value = False
1093+
except:
1094+
# No equals sign at all
1095+
key = s
1096+
value = False
11121097

11131098
# Only update non-existent properties
11141099
if key and result.get(key) == None:
@@ -1185,13 +1170,13 @@ def request(self, zc, timeout):
11851170
delay = _LISTENER_TIME
11861171
next = now + delay
11871172
last = now + timeout
1188-
result = 0
1173+
result = False
11891174
try:
11901175
zc.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN))
11911176
while (self.server is None or self.address is None or
11921177
self.text is None):
11931178
if last <= now:
1194-
return 0
1179+
return False
11951180
if next <= now:
11961181
out = DNSOutgoing(_FLAGS_QR_QUERY)
11971182
out.addQuestion(DNSQuestion(self.name, _TYPE_SRV,
@@ -1213,7 +1198,7 @@ def request(self, zc, timeout):
12131198

12141199
zc.wait(min(next, last) - now)
12151200
now = currentTimeMillis()
1216-
result = 1
1201+
result = True
12171202
finally:
12181203
zc.removeListener(self)
12191204

@@ -1223,7 +1208,7 @@ def __eq__(self, other):
12231208
"""Tests equality of service name"""
12241209
if isinstance(other, ServiceInfo):
12251210
return other.name == self.name
1226-
return 0
1211+
return False
12271212

12281213
def __ne__(self, other):
12291214
"""Non-equality test"""
@@ -1252,7 +1237,8 @@ class Zeroconf(object):
12521237
def __init__(self, bindaddress=None):
12531238
"""Creates an instance of the Zeroconf class, establishing
12541239
multicast communications, listening and reaping threads."""
1255-
globals()['_GLOBAL_DONE'] = 0
1240+
global _GLOBAL_DONE
1241+
_GLOBAL_DONE = False
12561242
if bindaddress is None:
12571243
self.intf = socket.gethostbyname(socket.gethostname())
12581244
else:
@@ -1590,8 +1576,9 @@ def send(self, out, addr = _MDNS_ADDR, port = _MDNS_PORT):
15901576
def close(self):
15911577
"""Ends the background threads, and prevent this instance from
15921578
servicing further queries."""
1593-
if globals()['_GLOBAL_DONE'] == 0:
1594-
globals()['_GLOBAL_DONE'] = 1
1579+
global _GLOBAL_DONE
1580+
if not _GLOBAL_DONE:
1581+
_GLOBAL_DONE = True
15951582
self.notifyAll()
15961583
self.engine.notify()
15971584
self.unregisterAllServices()

0 commit comments

Comments
 (0)