[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
+86 -42
View File
@@ -36,12 +36,12 @@ import coap
import common
import dtls
import ipv6
import lowpan
import mac802154
import mle
from enum import IntEnum
class MessageType(IntEnum):
MLE = 0
COAP = 1
@@ -54,7 +54,6 @@ class MessageType(IntEnum):
class Message(object):
def __init__(self):
self._type = None
self._channel = None
@@ -70,7 +69,9 @@ class Message(object):
self._type = MessageType.MLE
self._mle = udp_datagram.payload
elif isinstance(udp_datagram.payload, (coap.CoapMessage, coap.CoapMessageProxy)):
elif isinstance(
udp_datagram.payload, (coap.CoapMessage, coap.CoapMessageProxy)
):
self._type = MessageType.COAP
self._coap = udp_datagram.payload
@@ -145,10 +146,15 @@ class Message(object):
elif self._mac_header.frame_type == mac802154.MacHeader.FrameType.DATA:
self._type = MessageType.DATA
elif self._mac_header.frame_type == mac802154.MacHeader.FrameType.COMMAND:
elif (
self._mac_header.frame_type
== mac802154.MacHeader.FrameType.COMMAND
):
self._type = MessageType.COMMAND
else:
raise ValueError('Invalid mac frame type %d' % self._mac_header.frame_type)
raise ValueError(
'Invalid mac frame type %d' % self._mac_header.frame_type
)
@property
def ipv6_packet(self):
@@ -191,7 +197,7 @@ class Message(object):
if self.type != MessageType.MLE:
raise ValueError("Invalid message type. Expected MLE message.")
assert(self.mle.command.type == command_type)
assert self.mle.command.type == command_type
def assertMleMessageContainsTlv(self, tlv_class_type):
"""To confirm if Mle message contains the TLV type.
@@ -211,7 +217,7 @@ class Message(object):
contains_tlv = True
break
assert(contains_tlv == True)
assert contains_tlv
return tlv
def assertAssignedRouterQuantity(self, router_quantity):
@@ -225,9 +231,9 @@ class Message(object):
count = 0
for i in range(1, 65):
count += (router_id_mask & 1)
router_id_mask = (router_id_mask >> 1)
assert(count == router_quantity)
count += router_id_mask & 1
router_id_mask = router_id_mask >> 1
assert count == router_quantity
def assertMleMessageDoesNotContainTlv(self, tlv_class_type):
if self.type != MessageType.MLE:
@@ -239,7 +245,7 @@ class Message(object):
contains_tlv = True
break
assert(contains_tlv == False)
assert contains_tlv is False
def assertMleMessageContainsOptionalTlv(self, tlv_class_type):
if self.type != MessageType.MLE:
@@ -251,10 +257,16 @@ class Message(object):
contains_tlv = True
break
if contains_tlv == True:
print("MleMessage contains optional TLV: {}".format(tlv_class_type))
if contains_tlv:
print(
"MleMessage contains optional TLV: {}".format(tlv_class_type)
)
else:
print("MleMessage doesn't contain optional TLV: {}".format(tlv_class_type))
print(
"MleMessage doesn't contain optional TLV: {}".format(
tlv_class_type
)
)
def get_coap_message_tlv(self, tlv_class_type):
if self.type != MessageType.COAP:
@@ -274,7 +286,7 @@ class Message(object):
contains_tlv = True
break
assert(contains_tlv == True)
assert contains_tlv
def assertCoapMessageDoesNotContainTlv(self, tlv_class_type):
if self.type != MessageType.COAP:
@@ -286,31 +298,33 @@ class Message(object):
contains_tlv = True
break
assert(contains_tlv == False)
assert contains_tlv is False
def assertCoapMessageContainsOptionalTlv(self, tlv_class_type):
if self.type != MessageType.COAP:
raise ValueError("Invalid message type. Expected CoAP message.")
contains_tlv = False
for tlv in self.coap.payload:
if isinstance(tlv, tlv_class_type):
contains_tlv = True
break
print("CoapMessage doesn't contain optional TLV: {}".format(tlv_class_type))
print(
"CoapMessage doesn't contain optional TLV: {}".format(
tlv_class_type
)
)
def assertCoapMessageRequestUriPath(self, uri_path):
if self.type != MessageType.COAP:
raise ValueError("Invalid message type. Expected CoAP message.")
assert(uri_path == self.coap.uri_path)
assert uri_path == self.coap.uri_path
def assertCoapMessageCode(self, code):
if self.type != MessageType.COAP:
raise ValueError("Invalid message type. Expected CoAP message.")
assert(code == self.coap.code)
assert code == self.coap.code
def assertSentToNode(self, node):
sent_to_node = False
@@ -326,17 +340,22 @@ class Message(object):
sent_to_node = True
elif self.mac_header.dest_address.type == common.MacAddressType.LONG:
mac_address = common.MacAddress.from_eui64(bytearray(node.get_addr64(), encoding="utf-8"))
mac_address = common.MacAddress.from_eui64(
bytearray(node.get_addr64(), encoding="utf-8")
)
if self.mac_header.dest_address == mac_address:
sent_to_node = True
assert sent_to_node == True
assert sent_to_node
def assertSentToDestinationAddress(self, ipv6_address):
if sys.version_info[0] == 2:
ipv6_address = ipv6_address.decode("utf-8")
assert self.ipv6_packet.ipv6_header.destination_address == ipaddress.ip_address(ipv6_address)
assert (
self.ipv6_packet.ipv6_header.destination_address
== ipaddress.ip_address(ipv6_address)
)
def assertSentWithHopLimit(self, hop_limit):
assert self.ipv6_packet.ipv6_header.hop_limit == hop_limit
@@ -345,17 +364,21 @@ class Message(object):
return self.mac_header.dest_address.type == common.MacAddressType.LONG
def get_dst_udp_port(self):
assert isinstance(self.ipv6_packet.upper_layer_protocol, ipv6.UDPDatagram)
assert isinstance(
self.ipv6_packet.upper_layer_protocol, ipv6.UDPDatagram
)
return self.ipv6_packet.upper_layer_protocol.header.dst_port
def __repr__(self):
if self.type == MessageType.DTLS and self.dtls.content_type == dtls.ContentType.HANDSHAKE:
if (
self.type == MessageType.DTLS
and self.dtls.content_type == dtls.ContentType.HANDSHAKE
):
return "Message(type={})".format(str(self.dtls.handshake_type))
return "Message(type={})".format(MessageType(self.type).name)
class MessagesSet(object):
def __init__(self, messages, commissioning_messages=[]):
self._messages = messages
self._commissioning_messages = commissioning_messages
@@ -388,11 +411,13 @@ class MessagesSet(object):
break
if assert_enabled:
assert message is not None, "Could not find CoapMessage with code: {}".format(code)
assert (
message is not None
), "Could not find CoapMessage with code: {}".format(code)
return message
def last_mle_message(self, command_type, assert_enabled = True):
def last_mle_message(self, command_type, assert_enabled=True):
"""Get the last Mle Message with specified type from existing capture.
Args:
@@ -411,23 +436,29 @@ class MessagesSet(object):
if m.type != MessageType.MLE:
continue
#for command_type in command_types:
# for command_type in command_types:
if m.mle.command.type == command_type:
message = m
break
if assert_enabled:
assert message is not None, "Could not find MleMessage with type: {}".format(command_type)
assert (
message is not None
), "Could not find MleMessage with type: {}".format(command_type)
return message
def next_mle_message(self, command_type, assert_enabled=True, sent_to_node=None):
def next_mle_message(
self, command_type, assert_enabled=True, sent_to_node=None
):
message = self.next_mle_message_of_one_of_command_types(command_type)
if assert_enabled:
assert message is not None, "Could not find MleMessage of the type: {}".format(command_type)
assert (
message is not None
), "Could not find MleMessage of the type: {}".format(command_type)
if sent_to_node != None:
if sent_to_node is not None:
message.assertSentToNode(sent_to_node)
return message
@@ -472,7 +503,9 @@ class MessagesSet(object):
break
if assert_enabled:
assert message is not None, "Could not find Message of the type: {}".format(message_type)
assert (
message is not None
), "Could not find Message of the type: {}".format(message_type)
return message
@@ -489,13 +522,21 @@ class MessagesSet(object):
continue
if msg.dtls.content_type != content_type:
continue
if (content_type == dtls.ContentType.HANDSHAKE and
msg.dtls.handshake_type != handshake_type):
if (
content_type == dtls.ContentType.HANDSHAKE
and msg.dtls.handshake_type != handshake_type
):
continue
return msg
t = handshake_type if content_type == dtls.ContentType.HANDSHAKE else content_type
raise ValueError("Could not find DTLS message of type: {}".format(str(t)))
t = (
handshake_type
if content_type == dtls.ContentType.HANDSHAKE
else content_type
)
raise ValueError(
"Could not find DTLS message of type: {}".format(str(t))
)
def contains_icmp_message(self):
for m in self.messages:
@@ -543,7 +584,6 @@ class MessagesSet(object):
class MessageFactory:
def __init__(self, lowpan_parser):
self._lowpan_parser = lowpan_parser
@@ -551,10 +591,14 @@ class MessageFactory:
for tlv in message.mle.command.tlvs:
if isinstance(tlv, mle.SourceAddress):
mac802154.DeviceDescriptors.add(tlv.address, message.mac_header.src_address)
mac802154.DeviceDescriptors.add(
tlv.address, message.mac_header.src_address
)
if isinstance(tlv, mle.Address16):
mac802154.DeviceDescriptors.add(tlv.address, message.mac_header.dest_address)
mac802154.DeviceDescriptors.add(
tlv.address, message.mac_header.dest_address
)
def _parse_mac_frame(self, data):
mac_frame = mac802154.MacFrame()