[style] update python style to conform to PEP 8 (#3951)

With the exception of line length set to 119 vs. 79.

Add tests/ and tools/ to py-pretty-check.
This commit is contained in:
Jonathan Hui
2019-07-11 11:45:55 -04:00
committed by GitHub
parent a938ee2845
commit f924adcb60
341 changed files with 8702 additions and 4460 deletions
+165 -144
View File
@@ -28,13 +28,11 @@
#
import io
import ipaddress
import struct
from binascii import hexlify
import common
import network_data
from enum import IntEnum
@@ -92,7 +90,6 @@ class TlvType(IntEnum):
class SourceAddress(object):
def __init__(self, address):
self._address = address
@@ -110,14 +107,12 @@ class SourceAddress(object):
class SourceAddressFactory:
def parse(self, data, message_info):
address = struct.unpack(">H", data.read(2))[0]
return SourceAddress(address)
class Mode(object):
def __init__(self, receiver, secure, device_type, network_data):
self._receiver = receiver
self._secure = secure
@@ -143,20 +138,19 @@ class Mode(object):
def __eq__(self, other):
common.expect_the_same_class(self, other)
return self.receiver == other.receiver and \
self.secure == other.secure and \
self.device_type == other.device_type and \
self.network_data == other.network_data
return (
self.receiver == other.receiver
and self.secure == other.secure
and self.device_type == other.device_type
and self.network_data == other.network_data
)
def __repr__(self):
return "Mode(receiver={}, secure={}, device_type={}, network_data={})".format(self.receiver,
self.secure,
self.device_type,
self.network_data)
return "Mode(receiver={}, secure={}, device_type={}, network_data={})".format(
self.receiver, self.secure, self.device_type, self.network_data)
class ModeFactory:
def parse(self, data, message_info):
mode = ord(data.read(1))
receiver = (mode >> 3) & 0x01
@@ -167,7 +161,6 @@ class ModeFactory:
class Timeout(object):
def __init__(self, timeout):
self._timeout = timeout
@@ -185,14 +178,12 @@ class Timeout(object):
class TimeoutFactory:
def parse(self, data, message_info):
timeout = struct.unpack(">I", data.read(4))[0]
return Timeout(timeout)
class Challenge(object):
def __init__(self, challenge):
self._challenge = challenge
@@ -210,14 +201,12 @@ class Challenge(object):
class ChallengeFactory:
def parse(self, data, message_info):
challenge = data.read()
return Challenge(challenge)
class Response(object):
def __init__(self, response):
self._response = response
@@ -235,14 +224,12 @@ class Response(object):
class ResponseFactory:
def parse(self, data, message_info):
response = data.read()
return Response(response)
class LinkLayerFrameCounter(object):
def __init__(self, frame_counter):
self._frame_counter = frame_counter
@@ -256,18 +243,18 @@ class LinkLayerFrameCounter(object):
return self.frame_counter == other.frame_counter
def __repr__(self):
return "LinkLayerFrameCounter(frame_counter={})".format(self.frame_counter)
return "LinkLayerFrameCounter(frame_counter={})".format(
self.frame_counter
)
class LinkLayerFrameCounterFactory:
def parse(self, data, message_info):
frame_counter = struct.unpack(">I", data.read(4))[0]
return LinkLayerFrameCounter(frame_counter)
class MleFrameCounter(object):
def __init__(self, frame_counter):
self._frame_counter = frame_counter
@@ -285,14 +272,12 @@ class MleFrameCounter(object):
class MleFrameCounterFactory:
def parse(self, data, message_info):
frame_counter = struct.unpack(">I", data.read(4))[0]
return MleFrameCounter(frame_counter)
class LinkQualityAndRouteData(object):
def __init__(self, output, _input, route):
self._output = output
self._input = _input
@@ -313,25 +298,31 @@ class LinkQualityAndRouteData(object):
def __eq__(self, other):
common.expect_the_same_class(self, other)
return self.output == other.output and self.input == other.input and self.route == other.route
return (
self.output == other.output
and self.input == other.input
and self.route == other.route
)
def __repr__(self):
return "LinkQualityAndRouteData(ouput={}, input={}, route={})".format(self.output, self.input, self.route)
return "LinkQualityAndRouteData(ouput={}, input={}, route={})".format(
self.output, self.input, self.route
)
class LinkQualityAndRouteDataFactory:
def parse(self, data, message_info):
lqrd = ord(data.read(1))
output = (lqrd >> 6) & 0x3
_input = (lqrd >> 4) & 0x3
route = lqrd & 0x0f
route = lqrd & 0x0F
return LinkQualityAndRouteData(output, _input, route)
class Route64(object):
def __init__(self, id_sequence, router_id_mask, link_quality_and_route_data):
def __init__(
self, id_sequence, router_id_mask, link_quality_and_route_data
):
self._id_sequence = id_sequence
self._router_id_mask = router_id_mask
self._link_quality_and_route_data = link_quality_and_route_data
@@ -351,18 +342,22 @@ class Route64(object):
def __eq__(self, other):
common.expect_the_same_class(self, other)
return self.id_sequence == other.id_sequence and \
self.router_id_mask == other.router_id_mask and \
self.link_quality_and_route_data == other.link_quality_and_route_data
return (
self.id_sequence == other.id_sequence
and self.router_id_mask == other.router_id_mask
and self.link_quality_and_route_data
== other.link_quality_and_route_data
)
def __repr__(self):
lqrd_str = ", ".join(["{}".format(lqrd) for lqrd in self.link_quality_and_route_data])
lqrd_str = ", ".join(
["{}".format(lqrd) for lqrd in self.link_quality_and_route_data]
)
return "Route64(id_sequence={}, router_id_mask={}, link_quality_and_route_data=[{}])".format(
self.id_sequence, hex(self.router_id_mask), lqrd_str)
class Route64Factory:
def __init__(self, link_quality_and_route_data_factory):
self._lqrd_factory = link_quality_and_route_data_factory
@@ -373,13 +368,16 @@ class Route64Factory:
link_quality_and_route_data = []
while data.tell() < len(data.getvalue()):
link_quality_and_route_data.append(self._lqrd_factory.parse(data, message_info))
link_quality_and_route_data.append(
self._lqrd_factory.parse(data, message_info)
)
return Route64(id_sequence, router_id_mask, link_quality_and_route_data)
return Route64(
id_sequence, router_id_mask, link_quality_and_route_data
)
class Address16(object):
def __init__(self, address):
self._address = address
@@ -397,15 +395,20 @@ class Address16(object):
class Address16Factory:
def parse(self, data, message_info):
address = struct.unpack(">H", data.read(2))[0]
return Address16(address)
class LeaderData(object):
def __init__(self, partition_id, weighting, data_version, stable_data_version, leader_router_id):
def __init__(
self,
partition_id,
weighting,
data_version,
stable_data_version,
leader_router_id,
):
self._partition_id = partition_id
self._weighting = weighting
self._data_version = data_version
@@ -435,30 +438,37 @@ class LeaderData(object):
def __eq__(self, other):
common.expect_the_same_class(self, other)
return self.partition_id == other.partition_id and \
self.weighting == other.weighting and \
self.data_version == other.data_version and \
self.stable_data_version == other.stable_data_version and \
self.leader_router_id == other.leader_router_id
return (
self.partition_id == other.partition_id
and self.weighting == other.weighting
and self.data_version == other.data_version
and self.stable_data_version == other.stable_data_version
and self.leader_router_id == other.leader_router_id
)
def __repr__(self):
return "LeaderData(partition_id={}, weighting={}, data_version={}, stable_data_version={}, leader_router_id={})".format(
self.partition_id, self.weighting, self.data_version, self.stable_data_version, self.leader_router_id)
return ("LeaderData(partition_id={}, weighting={}, data_version={}, stable_data_version={},",
"leader_router_id={})").format(
self.partition_id, self.weighting, self.data_version, self.stable_data_version, self.leader_router_id, )
class LeaderDataFactory:
def parse(self, data, message_info):
partition_id = struct.unpack(">I", data.read(4))[0]
weighting = ord(data.read(1))
data_version = ord(data.read(1))
stable_data_version = ord(data.read(1))
leader_router_id = ord(data.read(1))
return LeaderData(partition_id, weighting, data_version, stable_data_version, leader_router_id)
return LeaderData(
partition_id,
weighting,
data_version,
stable_data_version,
leader_router_id,
)
class NetworkData(object):
def __init__(self, tlvs):
self._tlvs = tlvs
@@ -477,7 +487,6 @@ class NetworkData(object):
class NetworkDataFactory:
def __init__(self, network_data_tlvs_factory):
self._tlvs_factory = network_data_tlvs_factory
@@ -487,7 +496,6 @@ class NetworkDataFactory:
class TlvRequest(object):
def __init__(self, tlvs):
self._tlvs = tlvs
@@ -506,14 +514,12 @@ class TlvRequest(object):
class TlvRequestFactory:
def parse(self, data, message_info):
tlvs = [b for b in bytearray(data.read())]
return TlvRequest(tlvs)
class ScanMask(object):
def __init__(self, router, end_device):
self._router = router
self._end_device = end_device
@@ -529,14 +535,17 @@ class ScanMask(object):
def __eq__(self, other):
common.expect_the_same_class(self, other)
return self.router == other.router and self.end_device == other.end_device
return (
self.router == other.router and self.end_device == other.end_device
)
def __repr__(self):
return "ScanMask(router={}, end_device={})".format(self.router, self.end_device)
return "ScanMask(router={}, end_device={})".format(
self.router, self.end_device
)
class ScanMaskFactory:
def parse(self, data, message_info):
scan_mask = ord(data.read(1))
router = (scan_mask >> 7) & 0x01
@@ -545,17 +554,18 @@ class ScanMaskFactory:
class Connectivity(object):
def __init__(self,
pp,
link_quality_3,
link_quality_2,
link_quality_1,
leader_cost,
id_sequence,
active_routers,
sed_buffer_size=None,
sed_datagram_count=None):
def __init__(
self,
pp,
link_quality_3,
link_quality_2,
link_quality_1,
leader_cost,
id_sequence,
active_routers,
sed_buffer_size=None,
sed_datagram_count=None,
):
self._pp = pp
self._link_quality_3 = link_quality_3
self._link_quality_2 = link_quality_2
@@ -605,15 +615,17 @@ class Connectivity(object):
def __eq__(self, other):
common.expect_the_same_class(self, other)
return self.pp == other.pp and \
self.link_quality_3 == other.link_quality_3 and \
self.link_quality_2 == other.link_quality_2 and \
self.link_quality_1 == other.link_quality_1 and \
self.leader_cost == other.leader_cost and \
self.id_sequence == other.id_sequence and \
self.active_routers == other.active_routers and \
self.sed_buffer_size == other.sed_buffer_size and \
self.sed_datagram_count == other.sed_datagram_count
return (
self.pp == other.pp
and self.link_quality_3 == other.link_quality_3
and self.link_quality_2 == other.link_quality_2
and self.link_quality_1 == other.link_quality_1
and self.leader_cost == other.leader_cost
and self.id_sequence == other.id_sequence
and self.active_routers == other.active_routers
and self.sed_buffer_size == other.sed_buffer_size
and self.sed_datagram_count == other.sed_datagram_count
)
def __repr__(self):
return r"Connectivity(pp={}, \
@@ -633,12 +645,11 @@ class Connectivity(object):
self.id_sequence,
self.active_routers,
self.sed_buffer_size,
self.sed_datagram_count
self.sed_datagram_count,
)
class ConnectivityFactory:
def parse(self, data, message_info):
pp = ord(data.read(1)) & 0x03
link_quality_3 = ord(data.read(1))
@@ -657,19 +668,20 @@ class ConnectivityFactory:
sed_buffer_size = None
sed_datagram_count = None
return Connectivity(pp,
link_quality_3,
link_quality_2,
link_quality_1,
leader_cost,
id_sequence,
active_routers,
sed_buffer_size,
sed_datagram_count)
return Connectivity(
pp,
link_quality_3,
link_quality_2,
link_quality_1,
leader_cost,
id_sequence,
active_routers,
sed_buffer_size,
sed_datagram_count,
)
class LinkMargin(object):
def __init__(self, link_margin):
self._link_margin = link_margin
@@ -687,14 +699,12 @@ class LinkMargin(object):
class LinkMarginFactory:
def parse(self, data, message_info):
link_margin = ord(data.read(1))
return LinkMargin(link_margin)
class Status(object):
def __init__(self, status):
self._status = status
@@ -712,14 +722,12 @@ class Status(object):
class StatusFactory:
def parse(self, data, message_info):
status = ord(data.read(1))
return Status(status)
class Version(object):
def __init__(self, version):
self._version = version
@@ -737,14 +745,12 @@ class Version(object):
class VersionFactory:
def parse(self, data, message_info):
version = struct.unpack(">H", data.read(2))[0]
return Version(version)
class AddressFull(object):
def __init__(self, ipv6_address):
self._ipv6_address = ipv6_address
@@ -758,11 +764,12 @@ class AddressFull(object):
return self.ipv6_address == other.ipv6_address
def __repr__(self):
return "AddressFull(ipv6_address={}')".format(hexlify(self.ipv6_address))
return "AddressFull(ipv6_address={}')".format(
hexlify(self.ipv6_address)
)
class AddressFullFactory:
def parse(self, data, message_info):
data.read(1) # first byte is ignored
ipv6_address = data.read(16)
@@ -770,7 +777,6 @@ class AddressFullFactory:
class AddressCompressed(object):
def __init__(self, cid, iid):
self._cid = cid
self._iid = iid
@@ -789,19 +795,19 @@ class AddressCompressed(object):
return self.cid == other.cid and self.iid == other.iid
def __repr__(self):
return "AddressCompressed(cid={}, iid={}')".format(self.cid, hexlify(self.iid))
return "AddressCompressed(cid={}, iid={}')".format(
self.cid, hexlify(self.iid)
)
class AddressCompressedFactory:
def parse(self, data, message_info):
cid = (ord(data.read(1)) & 0x0F)
cid = ord(data.read(1)) & 0x0F
iid = bytearray(data.read(8))
return AddressCompressed(cid, iid)
class AddressRegistration(object):
def __init__(self, addresses):
self._addresses = addresses
@@ -815,12 +821,13 @@ class AddressRegistration(object):
return self.addresses == other.addresses
def __repr__(self):
addresses_str = ", ".join(["{}".format(address) for address in self.addresses])
addresses_str = ", ".join(
["{}".format(address) for address in self.addresses]
)
return "AddressRegistration(addresses=[{}])".format(addresses_str)
class AddressRegistrationFactory:
def __init__(self, addr_compressed_factory, addr_full_factory):
self._addr_compressed_factory = addr_compressed_factory
self._addr_full_factory = addr_full_factory
@@ -833,15 +840,18 @@ class AddressRegistrationFactory:
data.seek(-1, io.SEEK_CUR)
if compressed:
addresses.append(self._addr_compressed_factory.parse(data, message_info))
addresses.append(
self._addr_compressed_factory.parse(data, message_info)
)
else:
addresses.append(self._addr_full_factory.parse(data, message_info))
addresses.append(
self._addr_full_factory.parse(data, message_info)
)
return AddressRegistration(addresses)
class Channel(object):
def __init__(self, channel_page, channel):
self._channel_page = channel_page
self._channel = channel
@@ -857,15 +867,18 @@ class Channel(object):
def __eq__(self, other):
common.expect_the_same_class(self, other)
return self.channel_page == other.channel_page and \
self.channel == other.channel
return (
self.channel_page == other.channel_page
and self.channel == other.channel
)
def __repr__(self):
return "Channel(channel_page={}, channel={})".format(self.channel_page, channel)
return "Channel(channel_page={}, channel={})".format(
self.channel_page, self.channel
)
class ChannelFactory:
def parse(self, data, message_info):
channel_page = ord(data.read(1))
channel = struct.unpack(">H", data.read(2))[0]
@@ -873,7 +886,6 @@ class ChannelFactory:
class PanId:
def __init__(self, pan_id):
self._pan_id = pan_id
@@ -891,14 +903,12 @@ class PanId:
class PanIdFactory:
def parse(self, data, message_info):
pan_id = struct.unpack(">H", data.read(2))[0]
return PanId(pan_id)
class ActiveTimestamp(object):
def __init__(self, timestamp_seconds, timestamp_ticks, u):
self._timestamp_seconds = timestamp_seconds
self._timestamp_ticks = timestamp_ticks
@@ -919,9 +929,11 @@ class ActiveTimestamp(object):
def __eq__(self, other):
common.expect_the_same_class(self, other)
return self.timestamp_seconds == other.timestamp_seconds and \
self.timestamp_ticks == other.timestamp_ticks and \
self.u == other.u
return (
self.timestamp_seconds == other.timestamp_seconds
and self.timestamp_ticks == other.timestamp_ticks
and self.u == other.u
)
def __repr__(self):
return "ActiveTimestamp(timestamp_seconds={}, timestamp_ticks={}, u={})".format(
@@ -929,19 +941,17 @@ class ActiveTimestamp(object):
class ActiveTimestampFactory:
def parse(self, data, message_info):
seconds = bytearray([0x00, 0x00]) + bytearray(data.read(6))
ticks = struct.unpack(">H", data.read(2))[0]
timestamp_seconds = struct.unpack(">Q", bytes(seconds))[0]
timestamp_ticks = (ticks >> 1)
timestamp_ticks = ticks >> 1
u = ticks & 0x01
return ActiveTimestamp(timestamp_seconds, timestamp_ticks, u)
class PendingTimestamp(object):
def __init__(self, timestamp_seconds, timestamp_ticks, u):
self._timestamp_seconds = timestamp_seconds
self._timestamp_ticks = timestamp_ticks
@@ -962,9 +972,11 @@ class PendingTimestamp(object):
def __eq__(self, other):
common.expect_the_same_class(self, other)
return self.timestamp_seconds == other.timestamp_seconds and \
self.timestamp_ticks == other.timestamp_ticks and \
self.u == other.u
return (
self.timestamp_seconds == other.timestamp_seconds
and self.timestamp_ticks == other.timestamp_ticks
and self.u == other.u
)
def __repr__(self):
return "PendingTimestamp(timestamp_seconds={}, timestamp_ticks={}, u={})".format(
@@ -972,13 +984,12 @@ class PendingTimestamp(object):
class PendingTimestampFactory:
def parse(self, data, message_info):
seconds = bytearray([0x00, 0x00]) + bytearray(data.read(6))
ticks = struct.unpack(">H", data.read(2))[0]
timestamp_seconds = struct.unpack(">Q", bytes(seconds))[0]
timestamp_ticks = (ticks >> 1)
timestamp_ticks = ticks >> 1
u = ticks & 0x01
return PendingTimestamp(timestamp_seconds, timestamp_ticks, u)
@@ -991,7 +1002,6 @@ class ActiveOperationalDataset:
class ActiveOperationalDatasetFactory:
def parse(self, data, message_info):
return ActiveOperationalDataset()
@@ -1004,13 +1014,11 @@ class PendingOperationalDataset:
class PendingOperationalDatasetFactory:
def parse(self, data, message_info):
return PendingOperationalDataset()
class ThreadDiscovery(object):
def __init__(self, tlvs):
self._tlvs = tlvs
@@ -1024,8 +1032,8 @@ class ThreadDiscovery(object):
def __repr__(self):
return "ThreadDiscovery(tlvs={})".format(self.tlvs)
class ThreadDiscoveryFactory:
class ThreadDiscoveryFactory:
def __init__(self, thread_discovery_tlvs_factory):
self._tlvs_factory = thread_discovery_tlvs_factory
@@ -1040,11 +1048,12 @@ class TimeRequest:
def __init__(self):
print("TimeRequest is not implemented yet.")
class TimeRequestFactory:
class TimeRequestFactory:
def parse(self, data, message_info):
return TimeRequest()
class TimeParameter:
# TODO: Not implemented yet
@@ -1053,13 +1062,11 @@ class TimeParameter:
class TimeParameterFactory:
def parse(self, data, message_info):
return TimeParameter()
class MleCommand(object):
def __init__(self, _type, tlvs):
self._type = _type
self._tlvs = tlvs
@@ -1074,7 +1081,9 @@ class MleCommand(object):
def __repr__(self):
tlvs_str = ", ".join(["{}".format(tlv) for tlv in self.tlvs])
return "MleCommand(type={}, tlvs=[{}])".format(self.type.name, tlvs_str)
return "MleCommand(type={}, tlvs=[{}])".format(
self.type.name, tlvs_str
)
class MleCommandFactory:
@@ -1096,7 +1105,11 @@ class MleCommandFactory:
try:
return self._tlvs_factories[_type]
except KeyError:
raise KeyError("Could not find TLV factory. Unsupported TLV type: {}".format(_type))
raise KeyError(
"Could not find TLV factory. Unsupported TLV type: {}".format(
_type
)
)
def _parse_tlv(self, data, message_info):
_type = TlvType(ord(data.read(1)))
@@ -1119,7 +1132,6 @@ class MleCommandFactory:
class MleMessage(object):
def __init__(self, command):
self._command = command
@@ -1132,7 +1144,6 @@ class MleMessage(object):
class MleMessageSecured(MleMessage):
def __init__(self, aux_sec_hdr, command, mic):
super(MleMessageSecured, self).__init__(command)
self._aux_sec_hdr = aux_sec_hdr
@@ -1152,8 +1163,9 @@ class MleMessageSecured(MleMessage):
class MleMessageFactory:
def __init__(self, aux_sec_hdr_factory, mle_command_factory, crypto_engine):
def __init__(
self, aux_sec_hdr_factory, mle_command_factory, crypto_engine
):
self._aux_sec_hdr_factory = aux_sec_hdr_factory
self._mle_command_factory = mle_command_factory
self._crypto_engine = crypto_engine
@@ -1163,12 +1175,18 @@ class MleMessageFactory:
enc_data_length = len(data.getvalue())
enc_data = bytearray(data.read(enc_data_length - data.tell() - self._crypto_engine.mic_length))
enc_data = bytearray(
data.read(
enc_data_length - data.tell() - self._crypto_engine.mic_length
)
)
mic = bytearray(data.read())
dec_data = self._crypto_engine.decrypt(enc_data, mic, message_info)
command = self._mle_command_factory.parse(io.BytesIO(dec_data), message_info)
command = self._mle_command_factory.parse(
io.BytesIO(dec_data), message_info
)
return MleMessageSecured(aux_sec_hdr, command, mic)
@@ -1188,4 +1206,7 @@ class MleMessageFactory:
else:
raise RuntimeError(
"Could not create MLE message. Unknown security indicator value: {}".format(security_indicator))
"Could not create MLE message. Unknown security indicator value: {}".format(
security_indicator
)
)