comparison hgext/zeroconf/Zeroconf.py @ 28300:15c5f50e7e13

zeroconf: add whitespace around operator
author timeless <timeless@mozdev.org>
date Tue, 01 Mar 2016 09:48:11 +0000
parents acea9dd8cf7e
children fd8a4d2d6541
comparison
equal deleted inserted replaced
28299:acea9dd8cf7e 28300:15c5f50e7e13
488 488
489 def readHeader(self): 489 def readHeader(self):
490 """Reads header portion of packet""" 490 """Reads header portion of packet"""
491 format = '!HHHHHH' 491 format = '!HHHHHH'
492 length = struct.calcsize(format) 492 length = struct.calcsize(format)
493 info = struct.unpack(format, self.data[self.offset:self.offset+length]) 493 info = struct.unpack(format,
494 self.data[self.offset:self.offset + length])
494 self.offset += length 495 self.offset += length
495 496
496 self.id = info[0] 497 self.id = info[0]
497 self.flags = info[1] 498 self.flags = info[1]
498 self.numQuestions = info[2] 499 self.numQuestions = info[2]
505 format = '!HH' 506 format = '!HH'
506 length = struct.calcsize(format) 507 length = struct.calcsize(format)
507 for i in range(0, self.numQuestions): 508 for i in range(0, self.numQuestions):
508 name = self.readName() 509 name = self.readName()
509 info = struct.unpack(format, 510 info = struct.unpack(format,
510 self.data[self.offset:self.offset+length]) 511 self.data[self.offset:self.offset + length])
511 self.offset += length 512 self.offset += length
512 513
513 try: 514 try:
514 question = DNSQuestion(name, info[0], info[1]) 515 question = DNSQuestion(name, info[0], info[1])
515 self.questions.append(question) 516 self.questions.append(question)
518 519
519 def readInt(self): 520 def readInt(self):
520 """Reads an integer from the packet""" 521 """Reads an integer from the packet"""
521 format = '!I' 522 format = '!I'
522 length = struct.calcsize(format) 523 length = struct.calcsize(format)
523 info = struct.unpack(format, self.data[self.offset:self.offset+length]) 524 info = struct.unpack(format,
525 self.data[self.offset:self.offset + length])
524 self.offset += length 526 self.offset += length
525 return info[0] 527 return info[0]
526 528
527 def readCharacterString(self): 529 def readCharacterString(self):
528 """Reads a character string from the packet""" 530 """Reads a character string from the packet"""
532 534
533 def readString(self, len): 535 def readString(self, len):
534 """Reads a string of a given length from the packet""" 536 """Reads a string of a given length from the packet"""
535 format = '!' + str(len) + 's' 537 format = '!' + str(len) + 's'
536 length = struct.calcsize(format) 538 length = struct.calcsize(format)
537 info = struct.unpack(format, self.data[self.offset:self.offset+length]) 539 info = struct.unpack(format,
540 self.data[self.offset:self.offset + length])
538 self.offset += length 541 self.offset += length
539 return info[0] 542 return info[0]
540 543
541 def readUnsignedShort(self): 544 def readUnsignedShort(self):
542 """Reads an unsigned short from the packet""" 545 """Reads an unsigned short from the packet"""
543 format = '!H' 546 format = '!H'
544 length = struct.calcsize(format) 547 length = struct.calcsize(format)
545 info = struct.unpack(format, self.data[self.offset:self.offset+length]) 548 info = struct.unpack(format,
549 self.data[self.offset:self.offset + length])
546 self.offset += length 550 self.offset += length
547 return info[0] 551 return info[0]
548 552
549 def readOthers(self): 553 def readOthers(self):
550 """Reads answers, authorities and additionals section of the packet""" 554 """Reads answers, authorities and additionals section of the packet"""
552 length = struct.calcsize(format) 556 length = struct.calcsize(format)
553 n = self.numAnswers + self.numAuthorities + self.numAdditionals 557 n = self.numAnswers + self.numAuthorities + self.numAdditionals
554 for i in range(0, n): 558 for i in range(0, n):
555 domain = self.readName() 559 domain = self.readName()
556 info = struct.unpack(format, 560 info = struct.unpack(format,
557 self.data[self.offset:self.offset+length]) 561 self.data[self.offset:self.offset + length])
558 self.offset += length 562 self.offset += length
559 563
560 rec = None 564 rec = None
561 if info[0] == _TYPE_A: 565 if info[0] == _TYPE_A:
562 rec = DNSAddress(domain, info[0], info[1], info[2], 566 rec = DNSAddress(domain, info[0], info[1], info[2],
602 """Returns true if this is a response""" 606 """Returns true if this is a response"""
603 return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE 607 return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE
604 608
605 def readUTF(self, offset, len): 609 def readUTF(self, offset, len):
606 """Reads a UTF-8 string of a given length from the packet""" 610 """Reads a UTF-8 string of a given length from the packet"""
607 return self.data[offset:offset+len].decode('utf-8') 611 return self.data[offset:offset + len].decode('utf-8')
608 612
609 def readName(self): 613 def readName(self):
610 """Reads a domain name from the packet""" 614 """Reads a domain name from the packet"""
611 result = '' 615 result = ''
612 off = self.offset 616 off = self.offset
846 except KeyError: 850 except KeyError:
847 return [] 851 return []
848 852
849 def entries(self): 853 def entries(self):
850 """Returns a list of all entries""" 854 """Returns a list of all entries"""
851 def add(x, y): return x+y 855 def add(x, y): return x + y
852 try: 856 try:
853 return reduce(add, self.cache.values()) 857 return reduce(add, self.cache.values())
854 except Exception: 858 except Exception:
855 return [] 859 return []
856 860
1121 index = 0 1125 index = 0
1122 strs = [] 1126 strs = []
1123 while index < end: 1127 while index < end:
1124 length = ord(text[index]) 1128 length = ord(text[index])
1125 index += 1 1129 index += 1
1126 strs.append(text[index:index+length]) 1130 strs.append(text[index:index + length])
1127 index += length 1131 index += length
1128 1132
1129 for s in strs: 1133 for s in strs:
1130 eindex = s.find('=') 1134 eindex = s.find('=')
1131 if eindex == -1: 1135 if eindex == -1:
1132 # No equals sign at all 1136 # No equals sign at all
1133 key = s 1137 key = s
1134 value = 0 1138 value = 0
1135 else: 1139 else:
1136 key = s[:eindex] 1140 key = s[:eindex]
1137 value = s[eindex+1:] 1141 value = s[eindex + 1:]
1138 if value == 'true': 1142 if value == 'true':
1139 value = 1 1143 value = 1
1140 elif value == 'false' or not value: 1144 elif value == 'false' or not value:
1141 value = 0 1145 value = 0
1142 1146
1344 1348
1345 def wait(self, timeout): 1349 def wait(self, timeout):
1346 """Calling thread waits for a given number of milliseconds or 1350 """Calling thread waits for a given number of milliseconds or
1347 until notified.""" 1351 until notified."""
1348 self.condition.acquire() 1352 self.condition.acquire()
1349 self.condition.wait(timeout/1000) 1353 self.condition.wait(timeout / 1000)
1350 self.condition.release() 1354 self.condition.release()
1351 1355
1352 def notifyAll(self): 1356 def notifyAll(self):
1353 """Notifies all waiting threads""" 1357 """Notifies all waiting threads"""
1354 self.condition.acquire() 1358 self.condition.acquire()
1384 information for that service. The name of the service may be 1388 information for that service. The name of the service may be
1385 changed if needed to make it unique on the network.""" 1389 changed if needed to make it unique on the network."""
1386 self.checkService(info) 1390 self.checkService(info)
1387 self.services[info.name.lower()] = info 1391 self.services[info.name.lower()] = info
1388 if self.servicetypes.has_key(info.type): 1392 if self.servicetypes.has_key(info.type):
1389 self.servicetypes[info.type]+=1 1393 self.servicetypes[info.type] += 1
1390 else: 1394 else:
1391 self.servicetypes[info.type]=1 1395 self.servicetypes[info.type] = 1
1392 now = currentTimeMillis() 1396 now = currentTimeMillis()
1393 nextTime = now 1397 nextTime = now
1394 i = 0 1398 i = 0
1395 while i < 3: 1399 while i < 3:
1396 if now < nextTime: 1400 if now < nextTime:
1418 1422
1419 def unregisterService(self, info): 1423 def unregisterService(self, info):
1420 """Unregister a service.""" 1424 """Unregister a service."""
1421 try: 1425 try:
1422 del(self.services[info.name.lower()]) 1426 del(self.services[info.name.lower()])
1423 if self.servicetypes[info.type]>1: 1427 if self.servicetypes[info.type] > 1:
1424 self.servicetypes[info.type]-=1 1428 self.servicetypes[info.type] -= 1
1425 else: 1429 else:
1426 del self.servicetypes[info.type] 1430 del self.servicetypes[info.type]
1427 except KeyError: 1431 except KeyError:
1428 pass 1432 pass
1429 now = currentTimeMillis() 1433 now = currentTimeMillis()