mirror of
https://github.com/espressif/openthread.git
synced 2026-09-16 06:00:13 +00:00
[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:
@@ -74,18 +74,22 @@ def calculate_checksum(data):
|
||||
Returns:
|
||||
int: calculated checksum
|
||||
"""
|
||||
# Create halfwords from data bytes. Example: data[0] = 0x01, data[1] = 0xb2 => 0x01b2
|
||||
halfwords = [((byte0 << 8) | byte1) for byte0, byte1 in zip_longest(data[::2], data[1::2], fillvalue=0x00)]
|
||||
# Create halfwords from data bytes. Example: data[0] = 0x01, data[1] =
|
||||
# 0xb2 => 0x01b2
|
||||
halfwords = [
|
||||
((byte0 << 8) | byte1)
|
||||
for byte0, byte1 in zip_longest(data[::2], data[1::2], fillvalue=0x00)
|
||||
]
|
||||
|
||||
checksum = 0
|
||||
for halfword in halfwords:
|
||||
checksum += halfword
|
||||
checksum = (checksum & 0xFFFF) + (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16)
|
||||
|
||||
checksum ^= 0xFFFF
|
||||
checksum ^= 0xffff
|
||||
|
||||
if checksum == 0:
|
||||
return 0xFFFF
|
||||
return 0xffff
|
||||
else:
|
||||
return checksum
|
||||
|
||||
@@ -203,9 +207,13 @@ class IPv6PseudoHeader(ConvertibleToBytes):
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, source_address, destination_address, payload_length, next_header):
|
||||
def __init__(
|
||||
self, source_address, destination_address, payload_length, next_header
|
||||
):
|
||||
self._source_address = self._convert_to_ipaddress(source_address)
|
||||
self._destination_address = self._convert_to_ipaddress(destination_address)
|
||||
self._destination_address = self._convert_to_ipaddress(
|
||||
destination_address
|
||||
)
|
||||
self.payload_length = payload_length
|
||||
self.next_header = next_header
|
||||
|
||||
@@ -252,11 +260,21 @@ class IPv6Header(ConvertibleToBytes, BuildableFromBytes):
|
||||
|
||||
_header_length = 40
|
||||
|
||||
def __init__(self, source_address, destination_address, traffic_class=0, flow_label=0, hop_limit=64,
|
||||
payload_length=0, next_header=0):
|
||||
def __init__(
|
||||
self,
|
||||
source_address,
|
||||
destination_address,
|
||||
traffic_class=0,
|
||||
flow_label=0,
|
||||
hop_limit=64,
|
||||
payload_length=0,
|
||||
next_header=0,
|
||||
):
|
||||
self.version = self._version
|
||||
self._source_address = self._convert_to_ipaddress(source_address)
|
||||
self._destination_address = self._convert_to_ipaddress(destination_address)
|
||||
self._destination_address = self._convert_to_ipaddress(
|
||||
destination_address
|
||||
)
|
||||
self.traffic_class = traffic_class
|
||||
self.flow_label = flow_label
|
||||
self.hop_limit = hop_limit
|
||||
@@ -285,12 +303,16 @@ class IPv6Header(ConvertibleToBytes, BuildableFromBytes):
|
||||
return self._destination_address
|
||||
|
||||
def to_bytes(self):
|
||||
data = bytearray([
|
||||
((self.version & 0x0F) << 4) | ((self.traffic_class >> 4) & 0x0F),
|
||||
((self.traffic_class & 0x0F) << 4) | ((self.flow_label >> 16) & 0x0F),
|
||||
((self.flow_label >> 8) & 0xFF),
|
||||
((self.flow_label & 0xFF))
|
||||
])
|
||||
data = bytearray(
|
||||
[
|
||||
((self.version & 0x0F) << 4)
|
||||
| ((self.traffic_class >> 4) & 0x0F),
|
||||
((self.traffic_class & 0x0F) << 4)
|
||||
| ((self.flow_label >> 16) & 0x0F),
|
||||
((self.flow_label >> 8) & 0xff),
|
||||
((self.flow_label & 0xff)),
|
||||
]
|
||||
)
|
||||
data += struct.pack(">H", self.payload_length)
|
||||
data += bytearray([self.next_header, self.hop_limit])
|
||||
data += self.source_address.packed
|
||||
@@ -302,7 +324,7 @@ class IPv6Header(ConvertibleToBytes, BuildableFromBytes):
|
||||
def from_bytes(cls, data):
|
||||
b = bytearray(data.read(4))
|
||||
|
||||
version = (b[0] >> 4) & 0x0F
|
||||
(b[0] >> 4) & 0x0F
|
||||
traffic_class = ((b[0] & 0x0F) << 4) | ((b[1] >> 4) & 0x0F)
|
||||
flow_label = ((b[1] & 0x0F) << 16) | (b[2] << 8) | b[3]
|
||||
|
||||
@@ -312,23 +334,27 @@ class IPv6Header(ConvertibleToBytes, BuildableFromBytes):
|
||||
src_addr = bytearray(data.read(16))
|
||||
dst_addr = bytearray(data.read(16))
|
||||
|
||||
return cls(src_addr,
|
||||
dst_addr,
|
||||
traffic_class,
|
||||
flow_label,
|
||||
hop_limit,
|
||||
payload_length,
|
||||
next_header)
|
||||
return cls(
|
||||
src_addr,
|
||||
dst_addr,
|
||||
traffic_class,
|
||||
flow_label,
|
||||
hop_limit,
|
||||
payload_length,
|
||||
next_header,
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return "IPv6Header(source_address={}, destination_address={}, next_header={}, payload_length={}, \
|
||||
hop_limit={}, traffic_class={}, flow_label={})".format(self.source_address.compressed,
|
||||
self.destination_address.compressed,
|
||||
self.next_header,
|
||||
self.payload_length,
|
||||
self.hop_limit,
|
||||
self.traffic_class,
|
||||
self.flow_label)
|
||||
hop_limit={}, traffic_class={}, flow_label={})".format(
|
||||
self.source_address.compressed,
|
||||
self.destination_address.compressed,
|
||||
self.next_header,
|
||||
self.payload_length,
|
||||
self.hop_limit,
|
||||
self.traffic_class,
|
||||
self.flow_label,
|
||||
)
|
||||
|
||||
def __len__(self):
|
||||
return self._header_length
|
||||
@@ -380,12 +406,16 @@ class IPv6Packet(ConvertibleToBytes):
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, ipv6_header, upper_layer_protocol, extension_headers=None):
|
||||
def __init__(
|
||||
self, ipv6_header, upper_layer_protocol, extension_headers=None
|
||||
):
|
||||
self.ipv6_header = ipv6_header
|
||||
|
||||
self.upper_layer_protocol = upper_layer_protocol
|
||||
|
||||
self.extension_headers = extension_headers if extension_headers is not None else []
|
||||
self.extension_headers = (
|
||||
extension_headers if extension_headers is not None else []
|
||||
)
|
||||
|
||||
self._update_next_header_values_in_headers()
|
||||
|
||||
@@ -396,14 +426,22 @@ class IPv6Packet(ConvertibleToBytes):
|
||||
checksum = self.calculate_checksum()
|
||||
|
||||
if self.upper_layer_protocol.checksum != checksum:
|
||||
raise RuntimeError("Could not create IPv6 packet. "
|
||||
"Invalid checksum: {}!={}".format(self.upper_layer_protocol.checksum, checksum))
|
||||
raise RuntimeError(
|
||||
"Could not create IPv6 packet. "
|
||||
"Invalid checksum: {}!={}".format(
|
||||
self.upper_layer_protocol.checksum, checksum
|
||||
)
|
||||
)
|
||||
|
||||
self.upper_layer_protocol.checksum = checksum
|
||||
|
||||
def _update_payload_length_value_in_ipv6_header(self):
|
||||
self.ipv6_header.payload_length = len(self.upper_layer_protocol) + \
|
||||
sum([len(extension_header) for extension_header in self.extension_headers])
|
||||
self.ipv6_header.payload_length = len(self.upper_layer_protocol) + sum(
|
||||
[
|
||||
len(extension_header)
|
||||
for extension_header in self.extension_headers
|
||||
]
|
||||
)
|
||||
|
||||
def _update_next_header_values_in_headers(self):
|
||||
last_header = self.ipv6_header
|
||||
@@ -423,12 +461,16 @@ class IPv6Packet(ConvertibleToBytes):
|
||||
|
||||
self.upper_layer_protocol.checksum = saved_checksum
|
||||
|
||||
pseudo_header = IPv6PseudoHeader(self.ipv6_header.source_address,
|
||||
self.ipv6_header.destination_address,
|
||||
len(upper_layer_protocol_bytes),
|
||||
self.upper_layer_protocol.type)
|
||||
pseudo_header = IPv6PseudoHeader(
|
||||
self.ipv6_header.source_address,
|
||||
self.ipv6_header.destination_address,
|
||||
len(upper_layer_protocol_bytes),
|
||||
self.upper_layer_protocol.type,
|
||||
)
|
||||
|
||||
return calculate_checksum(pseudo_header.to_bytes() + upper_layer_protocol_bytes)
|
||||
return calculate_checksum(
|
||||
pseudo_header.to_bytes() + upper_layer_protocol_bytes
|
||||
)
|
||||
|
||||
def to_bytes(self):
|
||||
self._update_payload_length_value_in_ipv6_header()
|
||||
@@ -445,7 +487,9 @@ class IPv6Packet(ConvertibleToBytes):
|
||||
return ipv6_packet
|
||||
|
||||
def __repr__(self):
|
||||
return "IPv6Packet(header={}, upper_layer_protocol={})".format(self.ipv6_header, self.upper_layer_protocol)
|
||||
return "IPv6Packet(header={}, upper_layer_protocol={})".format(
|
||||
self.ipv6_header, self.upper_layer_protocol
|
||||
)
|
||||
|
||||
|
||||
class UDPHeader(ConvertibleToBytes, BuildableFromBytes):
|
||||
@@ -552,7 +596,9 @@ class ICMPv6Header(ConvertibleToBytes, BuildableFromBytes):
|
||||
self.checksum = checksum
|
||||
|
||||
def to_bytes(self):
|
||||
return bytearray([self.type, self.code]) + struct.pack(">H", self.checksum)
|
||||
return bytearray([self.type, self.code]) + struct.pack(
|
||||
">H", self.checksum
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_bytes(cls, data):
|
||||
@@ -581,6 +627,7 @@ class ICMPv6(UpperLayerProtocol):
|
||||
0x41, 0x41])))
|
||||
|
||||
"""
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
return 58
|
||||
@@ -606,6 +653,7 @@ class HopByHop(ExtensionHeader):
|
||||
- options
|
||||
|
||||
"""
|
||||
|
||||
_one_byte_padding = 0x00
|
||||
_many_bytes_padding = 0x01
|
||||
|
||||
@@ -698,7 +746,9 @@ class HopByHopOptionHeader(ConvertibleToBytes, BuildableFromBytes):
|
||||
return self._header_length
|
||||
|
||||
def __repr__(self):
|
||||
return "HopByHopOptionHeader(type={}, length={})".format(self.type, self.length)
|
||||
return "HopByHopOptionHeader(type={}, length={})".format(
|
||||
self.type, self.length
|
||||
)
|
||||
|
||||
|
||||
class HopByHopOption(ConvertibleToBytes):
|
||||
@@ -729,7 +779,9 @@ class HopByHopOption(ConvertibleToBytes):
|
||||
return len(self.header) + len(self.value)
|
||||
|
||||
def __repr__(self):
|
||||
return "HopByHopOption(header={}, value={})".format(self.header, self.value)
|
||||
return "HopByHopOption(header={}, value={})".format(
|
||||
self.header, self.value
|
||||
)
|
||||
|
||||
|
||||
class MPLOption(ConvertibleToBytes):
|
||||
@@ -738,12 +790,7 @@ class MPLOption(ConvertibleToBytes):
|
||||
|
||||
_header_length = 2
|
||||
|
||||
_seed_id_length = {
|
||||
0: 0,
|
||||
1: 2,
|
||||
2: 8,
|
||||
3: 16
|
||||
}
|
||||
_seed_id_length = {0: 0, 1: 2, 2: 8, 3: 16}
|
||||
|
||||
def __init__(self, S, M, V, sequence, seed_id):
|
||||
self.S = S
|
||||
@@ -753,7 +800,11 @@ class MPLOption(ConvertibleToBytes):
|
||||
self.seed_id = seed_id
|
||||
|
||||
def to_bytes(self):
|
||||
smv = ((self.S & 0x03) << 6) | ((self.M & 0x01) << 5) | ((self.V & 0x01) << 4)
|
||||
smv = (
|
||||
((self.S & 0x03) << 6)
|
||||
| ((self.M & 0x01) << 5)
|
||||
| ((self.V & 0x01) << 4)
|
||||
)
|
||||
|
||||
return bytearray([smv, self.sequence]) + self.seed_id
|
||||
|
||||
@@ -761,9 +812,9 @@ class MPLOption(ConvertibleToBytes):
|
||||
def from_bytes(cls, data):
|
||||
b = ord(data.read(1))
|
||||
|
||||
s = ((b >> 6) & 0x03)
|
||||
m = ((b >> 5) & 0x01)
|
||||
v = ((b >> 4) & 0x01)
|
||||
s = (b >> 6) & 0x03
|
||||
m = (b >> 5) & 0x01
|
||||
v = (b >> 4) & 0x01
|
||||
|
||||
sequence = ord(data.read(1))
|
||||
seed_id = data.read(cls._seed_id_length[s])
|
||||
@@ -774,7 +825,9 @@ class MPLOption(ConvertibleToBytes):
|
||||
return self._header_length + self._seed_id_length[self.S]
|
||||
|
||||
def __repr__(self):
|
||||
return "MPLOption(S={}, M={}, V={}, sequence={}, seed_id={})".format(self.S, self.M, self.V, self.sequence, hexlify(self.seed_id))
|
||||
return "MPLOption(S={}, M={}, V={}, sequence={}, seed_id={})".format(
|
||||
self.S, self.M, self.V, self.sequence, hexlify(self.seed_id)
|
||||
)
|
||||
|
||||
|
||||
class IPv6PacketFactory(PacketFactory):
|
||||
@@ -832,19 +885,27 @@ class IPv6PacketFactory(PacketFactory):
|
||||
self._ulpf = ulpf if ulpf is not None else {}
|
||||
|
||||
def _is_extension_header(self, header_type):
|
||||
return not header_type in UPPER_LAYER_PROTOCOLS
|
||||
return header_type not in UPPER_LAYER_PROTOCOLS
|
||||
|
||||
def _get_extension_header_factory_for(self, next_header):
|
||||
try:
|
||||
return self._ehf[next_header]
|
||||
except KeyError:
|
||||
raise RuntimeError("Could not get Extension Header factory for next_header={}.".format(next_header))
|
||||
raise RuntimeError(
|
||||
"Could not get Extension Header factory for next_header={}.".format(
|
||||
next_header
|
||||
)
|
||||
)
|
||||
|
||||
def _get_upper_layer_protocol_factory_for(self, next_header):
|
||||
try:
|
||||
return self._ulpf[next_header]
|
||||
except KeyError:
|
||||
raise RuntimeError("Could not get Upper Layer Protocol factory for next_header={}.".format(next_header))
|
||||
raise RuntimeError(
|
||||
"Could not get Upper Layer Protocol factory for next_header={}.".format(
|
||||
next_header
|
||||
)
|
||||
)
|
||||
|
||||
def _parse_extension_headers(self, data, next_header, message_info):
|
||||
extension_headers = []
|
||||
@@ -871,9 +932,13 @@ class IPv6PacketFactory(PacketFactory):
|
||||
message_info.source_ipv6 = ipv6_header.source_address
|
||||
message_info.destination_ipv6 = ipv6_header.destination_address
|
||||
|
||||
next_header, extension_headers = self._parse_extension_headers(data, ipv6_header.next_header, message_info)
|
||||
next_header, extension_headers = self._parse_extension_headers(
|
||||
data, ipv6_header.next_header, message_info
|
||||
)
|
||||
|
||||
upper_layer_protocol = self._parse_upper_layer_protocol(data, next_header, message_info)
|
||||
upper_layer_protocol = self._parse_upper_layer_protocol(
|
||||
data, next_header, message_info
|
||||
)
|
||||
|
||||
return IPv6Packet(ipv6_header, upper_layer_protocol, extension_headers)
|
||||
|
||||
@@ -886,13 +951,16 @@ class HopByHopOptionsFactory(object):
|
||||
_many_bytes_padding = 0x01
|
||||
|
||||
def __init__(self, options_factories=None):
|
||||
self._options_factories = options_factories if options_factories is not None else {}
|
||||
self._options_factories = (
|
||||
options_factories if options_factories is not None else {}
|
||||
)
|
||||
|
||||
def _get_HopByHopOption_value_factory(self, _type):
|
||||
try:
|
||||
return self._options_factories[_type]
|
||||
except KeyError:
|
||||
raise RuntimeError("Could not find HopByHopOption value factory for type={}.".format(_type))
|
||||
raise RuntimeError(
|
||||
"Could not find HopByHopOption value factory for type={}.".format(_type))
|
||||
|
||||
def parse(self, data, message_info):
|
||||
options = []
|
||||
@@ -909,11 +977,16 @@ class HopByHopOptionsFactory(object):
|
||||
data.read(option_header.length)
|
||||
|
||||
else:
|
||||
factory = self._get_HopByHopOption_value_factory(option_header.type)
|
||||
factory = self._get_HopByHopOption_value_factory(
|
||||
option_header.type
|
||||
)
|
||||
|
||||
option_data = data.read(option_header.length)
|
||||
|
||||
option = HopByHopOption(option_header, factory.parse(io.BytesIO(option_data), message_info))
|
||||
option = HopByHopOption(
|
||||
option_header,
|
||||
factory.parse(io.BytesIO(option_data), message_info),
|
||||
)
|
||||
|
||||
options.append(option)
|
||||
|
||||
@@ -935,12 +1008,17 @@ class HopByHopFactory(PacketFactory):
|
||||
|
||||
hdr_ext_len = ord(data.read(1))
|
||||
|
||||
# Note! Two bytes were read (next_header and hdr_ext_len) so they must be substracted from header length
|
||||
hop_by_hop_length = self._calculate_extension_header_length(hdr_ext_len) - 2
|
||||
# Note! Two bytes were read (next_header and hdr_ext_len) so they must
|
||||
# be substracted from header length
|
||||
hop_by_hop_length = (
|
||||
self._calculate_extension_header_length(hdr_ext_len) - 2
|
||||
)
|
||||
|
||||
hop_by_hop_data = data.read(hop_by_hop_length)
|
||||
|
||||
options = self._hop_by_hop_options_factory.parse(io.BytesIO(hop_by_hop_data), message_info)
|
||||
options = self._hop_by_hop_options_factory.parse(
|
||||
io.BytesIO(hop_by_hop_data), message_info
|
||||
)
|
||||
|
||||
hop_by_hop = HopByHop(next_header, options, hdr_ext_len)
|
||||
|
||||
@@ -974,7 +1052,9 @@ class UdpBasedOnSrcDstPortsPayloadFactory:
|
||||
def __init__(self, src_dst_port_based_payload_factories):
|
||||
"""
|
||||
Args:
|
||||
src_dst_port_based_payload_factories (PacketFactory): Factories parse UDP payload based on source or destination port.
|
||||
src_dst_port_based_payload_factories (PacketFactory):
|
||||
Factories parse UDP payload based on source or
|
||||
destination port.
|
||||
"""
|
||||
self._factories = src_dst_port_based_payload_factories
|
||||
|
||||
@@ -1007,7 +1087,9 @@ class UDPDatagramFactory(PacketFactory):
|
||||
header = self._udp_header_factory.parse(data, message_info)
|
||||
|
||||
# Update message payload length: UDP header (8B) + payload length
|
||||
message_info.payload_length += len(header) + (len(data.getvalue()) - data.tell())
|
||||
message_info.payload_length += len(header) + (
|
||||
len(data.getvalue()) - data.tell()
|
||||
)
|
||||
|
||||
message_info.src_port = header.src_port
|
||||
message_info.dst_port = header.dst_port
|
||||
@@ -1022,7 +1104,9 @@ class ICMPv6Factory(PacketFactory):
|
||||
""" Factory that produces ICMPv6 messages from data. """
|
||||
|
||||
def __init__(self, body_factories=None):
|
||||
self._body_factories = body_factories if body_factories is not None else {}
|
||||
self._body_factories = (
|
||||
body_factories if body_factories is not None else {}
|
||||
)
|
||||
|
||||
def _get_icmpv6_body_factory(self, _type):
|
||||
try:
|
||||
@@ -1030,13 +1114,17 @@ class ICMPv6Factory(PacketFactory):
|
||||
|
||||
except KeyError:
|
||||
if "default" not in self._body_factories:
|
||||
raise RuntimeError("Could not find specialized factory to parse ICMP body. "
|
||||
"Unsupported ICMP type: {}".format(_type))
|
||||
raise RuntimeError(
|
||||
"Could not find specialized factory to parse ICMP body. "
|
||||
"Unsupported ICMP type: {}".format(_type)
|
||||
)
|
||||
|
||||
default_factory = self._body_factories["default"]
|
||||
|
||||
print("Could not find specialized factory to parse ICMP body. "
|
||||
"Take the default one: {}".format(type(default_factory)))
|
||||
print(
|
||||
"Could not find specialized factory to parse ICMP body. "
|
||||
"Take the default one: {}".format(type(default_factory))
|
||||
)
|
||||
|
||||
return default_factory
|
||||
|
||||
@@ -1045,7 +1133,9 @@ class ICMPv6Factory(PacketFactory):
|
||||
|
||||
factory = self._get_icmpv6_body_factory(header.type)
|
||||
|
||||
message_info.payload_length += len(header) + (len(data.getvalue()) - data.tell())
|
||||
message_info.payload_length += len(header) + (
|
||||
len(data.getvalue()) - data.tell()
|
||||
)
|
||||
|
||||
return ICMPv6(header, factory.parse(data, message_info))
|
||||
|
||||
@@ -1140,7 +1230,8 @@ class ICMPv6DestinationUnreachable(ConvertibleToBytes, BuildableFromBytes):
|
||||
unused = struct.unpack(">I", data.read(4))[0]
|
||||
if unused != 0:
|
||||
raise RuntimeError(
|
||||
"Invalid value of unused field in the ICMPv6 Destination Unreachable data. Expected value: 0.")
|
||||
"Invalid value of unused field in the ICMPv6 Destination Unreachable data. Expected value: 0."
|
||||
)
|
||||
|
||||
return cls(bytearray(data.read()))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user