Skip to content

Commit 3ffa9d1

Browse files
committed
Cleanup pylint and run through black
1 parent b42e836 commit 3ffa9d1

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

adafruit_httpserver.py

+30-27
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__(
7676
raise ValueError("Unparseable raw_request: ", raw_request) from exc
7777

7878
# If ? is found in path, strip form data for matching route
79-
self.form = self.path.split('?')[0]
79+
self.form = self.path.split("?")[0]
8080

8181
def __hash__(self) -> int:
8282
return hash(self.method) ^ hash(self.form)
@@ -86,7 +86,7 @@ def __eq__(self, other: "_HTTPRequest") -> bool:
8686
return self.method == other.method and self.form == other.form
8787

8888
def __repr__(self) -> str:
89-
return f"_HTTPRequest(path={repr(self.path)}, method={repr(self.method)}, form={repr(self.form)})"
89+
return f"_HTTPRequest(path={repr(self.path)}, method={repr(self.method)})"
9090

9191

9292
class MIMEType:
@@ -218,9 +218,9 @@ def __init__(
218218
The data to return in the response body, if ``filename`` is not ``None``.
219219
:param str filename: If not ``None``,
220220
return the contents of the specified file, and ignore ``body``.
221-
:param int value of Cache-Control: max-age=<value> header.
222-
604800 (7 days) is is default for files served not using route decorator.
223-
0 if not specified in the route decorator HTTPResponse.
221+
:param int cache: sets Cache-Control: max-age=<value> header.
222+
604800 (7 days) is is default for files served. If using the route decorator,
223+
then the default is 0 unless specified HTTPResponse.
224224
:param str root: root directory for filename, without a trailing slash
225225
"""
226226
self.status = status
@@ -236,17 +236,15 @@ def send(self, conn: Any) -> None:
236236
if self.filename:
237237
try:
238238
file_length = os.stat(self.root + self.filename)[6]
239-
self._send_file_response(conn, self.filename, self.root, file_length, self.cache)
239+
self._send_file_response(conn, file_length)
240240
except OSError:
241-
self._send_response(
242-
conn,
243-
HTTPStatus.NOT_FOUND,
244-
MIMEType.TEXT_PLAIN,
245-
f"{HTTPStatus.NOT_FOUND} {self.filename}\r\n",
246-
0
247-
)
241+
self.status = HTTPStatus.NOT_FOUND
242+
self.content_type = MIMEType.TEXT_PLAIN
243+
self.body = f"{HTTPStatus.NOT_FOUND} {self.filename}\r\n".encode()
244+
self.cache = 0
245+
self._send_response(conn)
248246
else:
249-
self._send_response(conn, self.status, self.content_type, self.body, self.cache)
247+
self._send_response(conn)
250248

251249
def send_chunk_headers(self, conn: Any):
252250
"""Send Headers for a chunked response over the given socket."""
@@ -260,24 +258,27 @@ def send_body_chunk(self, conn: Any, chunk: str):
260258
:param Any conn: Current connection.
261259
:param str chunk: String data to be sent.
262260
"""
263-
size="%X\r\n".encode() % len(chunk)
261+
size = "%X\r\n".encode() % len(chunk)
264262
self._send_bytes(conn, size)
265-
self._send_bytes(conn, chunk.encode() + b'\r\n')
263+
self._send_bytes(conn, chunk.encode() + b"\r\n")
266264

267-
def _send_response(self, conn, status, content_type, body, cache):
265+
def _send_response(self, conn):
268266
self._send_bytes(
269-
conn, self._HEADERS_FORMAT.format(status, content_type, len(body), cache)
267+
conn,
268+
self._HEADERS_FORMAT.format(
269+
self.status, self.content_type, len(self.body), self.cache
270+
),
270271
)
271-
self._send_bytes(conn, body)
272+
self._send_bytes(conn, self.body)
272273

273-
def _send_file_response(self, conn, filename, root, file_length, cache):
274+
def _send_file_response(self, conn, file_length):
274275
self._send_bytes(
275276
conn,
276277
self._HEADERS_FORMAT.format(
277-
self.status, MIMEType.mime_type(filename), file_length, cache
278+
self.status, MIMEType.mime_type(self.filename), file_length, self.cache
278279
),
279280
)
280-
with open(root + filename, "rb") as file:
281+
with open(self.root + self.filename, "rb") as file:
281282
while bytes_read := file.read(2048):
282283
self._send_bytes(conn, bytes_read)
283284

@@ -325,7 +326,7 @@ def route_func(request):
325326
raw_text = request.raw_request.decode("utf8")
326327
print("Received a request of length", len(raw_text), "bytes")
327328
return HTTPResponse(body="hello world")
328-
329+
329330
@server.route(path, method)
330331
def route_func(request, conn):
331332
raw_text = request.raw_request.decode("utf8")
@@ -382,7 +383,7 @@ def poll(self, timeout=-1):
382383
Call this method inside your main event loop to get the server to
383384
check for new incoming client requests. When a request comes in,
384385
the application callable will be invoked.
385-
386+
386387
:param int timeout: Optional timeout value in seconds.
387388
"""
388389
try:
@@ -408,7 +409,9 @@ def poll(self, timeout=-1):
408409
if response is None:
409410
return
410411
elif request.method == "GET":
411-
response = HTTPResponse(filename=request.path, cache=604800, root=self.root_path)
412+
response = HTTPResponse(
413+
filename=request.path, cache=604800, root=self.root_path
414+
)
412415
else:
413416
response = HTTPResponse(status=HTTPStatus.INTERNAL_SERVER_ERROR)
414417

@@ -418,10 +421,10 @@ def poll(self, timeout=-1):
418421
if ex.errno == EAGAIN:
419422
# there is no data available right now, try again later.
420423
return
421-
elif ex.errno == ECONNRESET:
424+
if ex.errno == ECONNRESET:
422425
# connection reset by peer, try again later.
423426
return
424-
elif ex.errno == ETIMEDOUT:
427+
if ex.errno == ETIMEDOUT:
425428
# connection timed out
426429
return
427430
raise

0 commit comments

Comments
 (0)