[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
+91 -28
View File
@@ -78,7 +78,7 @@ class CoapOptionHeader(object):
@property
def is_payload_marker(self):
return self.delta == 0xf and self.length == 0xf
return self.delta == 0xF and self.length == 0xF
@classmethod
def _read_extended_value(cls, data, value):
@@ -94,8 +94,8 @@ class CoapOptionHeader(object):
def from_bytes(cls, data):
initial_byte = ord(data.read(1))
delta = (initial_byte >> 4) & 0xf
length = initial_byte & 0xf
delta = (initial_byte >> 4) & 0xF
length = initial_byte & 0xF
delta = cls._read_extended_value(data, delta)
length = cls._read_extended_value(data, length)
@@ -120,7 +120,9 @@ class CoapOption(object):
return self._value
def __repr__(self):
return "CoapOption(type={}, value={})".format(self.type, hexlify(self.value))
return "CoapOption(type={}, value={})".format(
self.type, hexlify(self.value)
)
class CoapOptionsFactory(object):
@@ -162,11 +164,11 @@ class CoapCode(object):
@property
def detail(self):
return self.code & 0x1f
return self.code & 0x1F
@classmethod
def from_class_and_detail(cls, _class, detail):
return cls(((_class & 0x7) << 5) | (detail & 0x1f))
return cls(((_class & 0x7) << 5) | (detail & 0x1F))
@classmethod
def from_dotted(cls, dotted_str):
@@ -179,7 +181,9 @@ class CoapCode(object):
@property
def dotted(self):
return ".".join(["{:01d}".format(self._class), "{:02d}".format(self.detail)])
return ".".join(
["{:01d}".format(self._class), "{:02d}".format(self.detail)]
)
def __eq__(self, other):
if isinstance(other, int):
@@ -192,7 +196,9 @@ class CoapCode(object):
return self.code == other.code
else:
raise TypeError("Could not compare {} and {}".format(type(self), type(other)))
raise TypeError(
"Could not compare {} and {}".format(type(self), type(other))
)
def __repr__(self):
return self.dotted
@@ -202,7 +208,17 @@ class CoapMessage(object):
""" Class representing CoAP message. """
def __init__(self, version, _type, code, message_id, token, options, payload, uri_path=None):
def __init__(
self,
version,
_type,
code,
message_id,
token,
options,
payload,
uri_path=None,
):
self._version = version
self._type = _type
self._code = code
@@ -250,9 +266,17 @@ class CoapMessage(object):
def __repr__(self):
options_str = ", ".join([repr(opt) for opt in self.options])
return "CoapMessage(version={}, type={}, code={}, message_id={}, token={}, options=[{}], payload={}, uri-path='{}')".format(
self.version, CoapMessageType.name[self.type], self.code, self.message_id, hexlify(self.token),
options_str, self.payload, self.uri_path)
return ("CoapMessage(version={}, type={}, code={}, message_id={}, token={}, options=[{}], payload={},",
"uri-path='{}')").format(
self.version,
CoapMessageType.name[self.type],
self.code,
self.message_id,
hexlify(self.token),
options_str,
self.payload,
self.uri_path,
)
class CoapMessageProxy(object):
@@ -264,11 +288,19 @@ class CoapMessageProxy(object):
to get URI path to get proper payload parser.
"""
def __init__(self, coap_message, message_info, mid_to_uri_path_binder, uri_path_based_payload_factories):
def __init__(
self,
coap_message,
message_info,
mid_to_uri_path_binder,
uri_path_based_payload_factories,
):
self._coap_message = coap_message
self._message_info = message_info
self._mid_to_uri_path_binder = mid_to_uri_path_binder
self._uri_path_based_payload_factories = uri_path_based_payload_factories
self._uri_path_based_payload_factories = (
uri_path_based_payload_factories
)
@property
def version(self):
@@ -301,11 +333,15 @@ class CoapMessageProxy(object):
@property
def payload(self):
try:
binded_uri_path = self._mid_to_uri_path_binder.get_uri_path_for(self.message_id, self.token)
binded_uri_path = self._mid_to_uri_path_binder.get_uri_path_for(
self.message_id, self.token
)
factory = self._uri_path_based_payload_factories[binded_uri_path]
return factory.parse(io.BytesIO(self._coap_message.payload), self._message_info)
return factory.parse(
io.BytesIO(self._coap_message.payload), self._message_info
)
except RuntimeError:
return self._coap_message.payload
@@ -316,9 +352,10 @@ class CoapMessageProxy(object):
def __repr__(self):
options_str = ", ".join([repr(opt) for opt in self.options])
return "CoapMessageProxy(version={}, type={}, code={}, message_id={}, token={}, options=[{}], payload={}, uri-path='{}')".format(
self.version, self.type, self.code, self.message_id, hexlify(self.token),
options_str, self.payload, self.uri_path)
return ("CoapMessageProxy(version={}, type={}, code={}, message_id={}, token={}, options=[{}], payload={},",
"uri-path='{}')").format(
self.version, self.type, self.code, self.message_id, hexlify(self.token), options_str, self.payload,
self.uri_path, )
class CoapMessageIdToUriPathBinder:
@@ -335,17 +372,25 @@ class CoapMessageIdToUriPathBinder:
try:
return self._uri_path_binds[message_id][hexlify(token)]
except KeyError:
raise RuntimeError("Could not find URI PATH for message_id: {} and token: {}".format(
message_id, hexlify(token)))
raise RuntimeError(
"Could not find URI PATH for message_id: {} and token: {}".format(
message_id, hexlify(token)))
class CoapMessageFactory(object):
""" Factory that produces CoAP messages. """
def __init__(self, options_factory, uri_path_based_payload_factories, message_id_to_uri_path_binder):
def __init__(
self,
options_factory,
uri_path_based_payload_factories,
message_id_to_uri_path_binder,
):
self._options_factory = options_factory
self._uri_path_based_payload_factories = uri_path_based_payload_factories
self._uri_path_based_payload_factories = (
uri_path_based_payload_factories
)
self._mid_to_uri_path_binder = message_id_to_uri_path_binder
def _uri_path_from(self, options):
@@ -365,12 +410,14 @@ class CoapMessageFactory(object):
version = (initial_byte >> 6) & 0x3
_type = CoapMessageType((initial_byte >> 4) & 0x3)
token_length = initial_byte & 0xf
token_length = initial_byte & 0xF
return version, _type, token_length
def parse(self, data, message_info):
version, _type, token_length = self._parse_initial_byte(data, message_info)
version, _type, token_length = self._parse_initial_byte(
data, message_info
)
code = CoapCode(ord(data.read(1)))
message_id = struct.unpack(">H", data.read(2))[0]
@@ -380,8 +427,24 @@ class CoapMessageFactory(object):
uri_path = self._uri_path_from(options)
if uri_path is not None:
self._mid_to_uri_path_binder.add_uri_path_for(message_id, token, uri_path)
self._mid_to_uri_path_binder.add_uri_path_for(
message_id, token, uri_path
)
coap_message = CoapMessage(version, _type, code, message_id, token, options, data.read(), uri_path)
coap_message = CoapMessage(
version,
_type,
code,
message_id,
token,
options,
data.read(),
uri_path,
)
return CoapMessageProxy(coap_message, message_info, self._mid_to_uri_path_binder, self._uri_path_based_payload_factories)
return CoapMessageProxy(
coap_message,
message_info,
self._mid_to_uri_path_binder,
self._uri_path_based_payload_factories,
)