Skip to content

Commit d372f8e

Browse files
committed
CI fixes, reformating etc.
1 parent 20a4eda commit d372f8e

File tree

6 files changed

+40
-32
lines changed

6 files changed

+40
-32
lines changed

adafruit_httpserver/response.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -548,12 +548,12 @@ def route_func(request: Request):
548548
FIN = 0b10000000 # FIN bit indicating the final fragment
549549

550550
# opcodes
551-
CONT = 0 # Continuation frame, TODO: Currently not supported
552-
TEXT = 1 # Frame contains UTF-8 text
553-
BINARY = 2 # Frame contains binary data
554-
CLOSE = 8 # Frame closes the connection
555-
PING = 9 # Frame is a ping, expecting a pong
556-
PONG = 10 # Frame is a pong, in response to a ping
551+
CONT = 0 # Continuation frame, TODO: Currently not supported
552+
TEXT = 1 # Frame contains UTF-8 text
553+
BINARY = 2 # Frame contains binary data
554+
CLOSE = 8 # Frame closes the connection
555+
PING = 9 # Frame is a ping, expecting a pong
556+
PONG = 10 # Frame is a pong, in response to a ping
557557

558558
@staticmethod
559559
def _check_request_initiates_handshake(request: Request):
@@ -573,7 +573,7 @@ def _process_sec_websocket_key(request: Request) -> str:
573573
if key is None:
574574
raise ValueError("Request does not have Sec-WebSocket-Key header")
575575

576-
response_key = hashlib.new('sha1', key.encode())
576+
response_key = hashlib.new("sha1", key.encode())
577577
response_key.update(Websocket.GUID)
578578

579579
return b2a_base64(response_key.digest()).strip().decode()
@@ -607,7 +607,6 @@ def __init__( # pylint: disable=too-many-arguments
607607

608608
request.connection.setblocking(False)
609609

610-
611610
@staticmethod
612611
def _parse_frame_header(header):
613612
fin = header[0] & Websocket.FIN
@@ -626,7 +625,7 @@ def _read_frame(self):
626625
buffer = bytearray(self._buffer_size)
627626

628627
header_length = self._request.connection.recv_into(buffer, 2)
629-
header_bytes = buffer[:header_length]
628+
header_bytes = buffer[:header_length]
630629

631630
fin, opcode, has_mask, length = self._parse_frame_header(header_bytes)
632631

@@ -640,23 +639,23 @@ def _read_frame(self):
640639

641640
if length < 0:
642641
length = self._request.connection.recv_into(buffer, -length)
643-
length = int.from_bytes(buffer[:length], 'big')
642+
length = int.from_bytes(buffer[:length], "big")
644643

645644
if has_mask:
646645
mask_length = self._request.connection.recv_into(buffer, 4)
647646
mask = buffer[:mask_length]
648647

649648
while 0 < length:
650649
payload_length = self._request.connection.recv_into(buffer, length)
651-
payload += buffer[:min(payload_length, length)]
650+
payload += buffer[: min(payload_length, length)]
652651
length -= min(payload_length, length)
653652

654653
if has_mask:
655654
payload = bytes(x ^ mask[i % 4] for i, x in enumerate(payload))
656655

657656
return opcode, payload
658657

659-
def _handle_frame(self, opcode: int, payload: bytes):
658+
def _handle_frame(self, opcode: int, payload: bytes) -> Union[str, bytes, None]:
660659
# TODO: Handle continuation frames, currently not supported
661660
if opcode == Websocket.CONT:
662661
return None
@@ -667,14 +666,13 @@ def _handle_frame(self, opcode: int, payload: bytes):
667666

668667
if opcode == Websocket.PONG:
669668
return None
670-
elif opcode == Websocket.PING:
669+
if opcode == Websocket.PING:
671670
self.send_message(payload, Websocket.PONG)
672671
return payload
673672

674673
try:
675674
payload = payload.decode() if opcode == Websocket.TEXT else payload
676-
except UnicodeError as error:
677-
print("Payload UnicodeError: ", error, payload)
675+
except UnicodeError:
678676
pass
679677

680678
return payload
@@ -688,7 +686,9 @@ def receive(self, fail_silently: bool = False) -> Union[str, bytes, None]:
688686
if self.closed:
689687
if fail_silently:
690688
return None
691-
raise RuntimeError("Websocket connection is closed, cannot receive messages")
689+
raise RuntimeError(
690+
"Websocket connection is closed, cannot receive messages"
691+
)
692692

693693
try:
694694
opcode, payload = self._read_frame()
@@ -700,7 +700,7 @@ def receive(self, fail_silently: bool = False) -> Union[str, bytes, None]:
700700
return None
701701
if error.errno == ETIMEDOUT: # Connection timed out
702702
return None
703-
if error.errno == ENOTCONN: # Client disconnected without closing connection
703+
if error.errno == ENOTCONN: # Client disconnected
704704
self.close()
705705
return None
706706
raise error
@@ -720,12 +720,12 @@ def _prepare_frame(opcode: int, message: bytes) -> bytearray:
720720
# Message between 126 and 65535 bytes, use 2 bytes for length
721721
elif payload_length < 65536:
722722
frame.append(126)
723-
frame.extend(payload_length.to_bytes(2, 'big'))
723+
frame.extend(payload_length.to_bytes(2, "big"))
724724

725725
# Message over 65535 bytes, use 8 bytes for length
726726
else:
727727
frame.append(127)
728-
frame.extend(payload_length.to_bytes(8, 'big'))
728+
frame.extend(payload_length.to_bytes(8, "big"))
729729

730730
frame.extend(message)
731731
return frame
@@ -734,7 +734,7 @@ def send_message(
734734
self,
735735
message: Union[str, bytes],
736736
opcode: int = None,
737-
fail_silently: bool = False
737+
fail_silently: bool = False,
738738
):
739739
"""
740740
Send a message to the client.
@@ -746,7 +746,7 @@ def send_message(
746746
"""
747747
if self.closed:
748748
if fail_silently:
749-
return None
749+
return
750750
raise RuntimeError("Websocket connection is closed, cannot send message")
751751

752752
determined_opcode = opcode or (
@@ -762,7 +762,7 @@ def send_message(
762762
self._send_bytes(self._request.connection, frame)
763763
except BrokenPipeError as error:
764764
if fail_silently:
765-
return None
765+
return
766766
raise error
767767

768768
def _send(self) -> None:
@@ -775,6 +775,6 @@ def close(self):
775775
**Always call this method when you are done sending events.**
776776
"""
777777
if not self.closed:
778-
self.send_message(b'', Websocket.CLOSE, fail_silently=True)
778+
self.send_message(b"", Websocket.CLOSE, fail_silently=True)
779779
self._close_connection()
780780
self.closed = True

adafruit_httpserver/route.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def __init__(
3939
self.path = re.sub(r"<\w+>", r"([^/]+)", path).replace("....", r".+").replace(
4040
"...", r"[^/]+"
4141
) + ("/?" if append_slash else "")
42-
self.methods = set(methods) if isinstance(methods, (set, list)) else set([methods])
42+
self.methods = (
43+
set(methods) if isinstance(methods, (set, list)) else set([methods])
44+
)
4345

4446
self.handler = handler
4547

@@ -98,11 +100,11 @@ def match(self, other: "Route") -> Tuple[bool, List[str]]:
98100
"""
99101

100102
if not other.methods.issubset(self.methods):
101-
return False, dict()
103+
return False, {}
102104

103105
regex_match = re.match(f"^{self.path}$", other.path)
104106
if regex_match is None:
105-
return False, dict()
107+
return False, {}
106108

107109
return True, dict(zip(self.parameters_names, regex_match.groups()))
108110

adafruit_httpserver/server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def route_func(request, my_parameter):
117117
def route_func(request):
118118
...
119119
"""
120+
120121
def route_decorator(func: Callable) -> Callable:
121122
self._routes.add(Route(path, methods, func, append_slash=append_slash))
122123
return func

examples/httpserver_neopixel.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,20 @@ def change_neopixel_color_handler_url_params(
6565

6666
return Response(request, f"Changed NeoPixel to color ({r}, {g}, {b})")
6767

68+
6869
url_params_route = Route(
6970
"/change-neopixel-color/<r>/<g>/<b>", GET, change_neopixel_color_handler_url_params
7071
)
7172

7273
# Alternative way of registering routes.
73-
server.add_routes([
74-
Route("/change-neopixel-color/json", GET, change_neopixel_color_handler_post_json),
75-
url_params_route,
76-
])
74+
server.add_routes(
75+
[
76+
Route(
77+
"/change-neopixel-color/json", GET, change_neopixel_color_handler_post_json
78+
),
79+
url_params_route,
80+
]
81+
)
7782

7883

7984
server.serve_forever(str(wifi.radio.ipv4_address))

examples/httpserver_sse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def client(request: Request):
4141

4242
@server.route("/connect-client", GET)
4343
def connect_client(request: Request):
44-
global sse_response
44+
global sse_response # pylint: disable=global-statement
4545

4646
if sse_response is not None:
4747
sse_response.close() # Close any existing connection

examples/httpserver_websocket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def client(request: Request):
5757

5858
@server.route("/connect-websocket", GET)
5959
def connect_client(request: Request):
60-
global websocket
60+
global websocket # pylint: disable=global-statement
6161

6262
if websocket is not None:
6363
websocket.close() # Close any existing connection

0 commit comments

Comments
 (0)