From e6630b59f7272d448796ab814672b363c52f2807 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Fri, 17 Nov 2023 11:18:28 -0500 Subject: [PATCH] Fix mypy complaints, simplifying code while we're at it --- src/allmydata/storage/http_server.py | 2 +- src/allmydata/web/common.py | 29 +++++++++++----------------- src/allmydata/web/introweb.py | 4 ++-- src/allmydata/web/root.py | 4 ++-- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/allmydata/storage/http_server.py b/src/allmydata/storage/http_server.py index 5b4e02288..78c5ac5d2 100644 --- a/src/allmydata/storage/http_server.py +++ b/src/allmydata/storage/http_server.py @@ -695,7 +695,7 @@ class HTTPServer(BaseApp): if accept.best == CBOR_MIME_TYPE: request.setHeader("Content-Type", CBOR_MIME_TYPE) f = TemporaryFile() - cbor2.dump(data, f) + cbor2.dump(data, f) # type: ignore def read_data(offset: int, length: int) -> bytes: f.seek(offset) diff --git a/src/allmydata/web/common.py b/src/allmydata/web/common.py index 2a3a9ea1c..982be491a 100644 --- a/src/allmydata/web/common.py +++ b/src/allmydata/web/common.py @@ -6,7 +6,7 @@ from __future__ import annotations from six import ensure_str from importlib.resources import files as resource_files, as_file from contextlib import ExitStack - +import weakref from typing import Optional, Union, TypeVar, overload from typing_extensions import Literal @@ -857,26 +857,19 @@ def get_keypair(request: IRequest) -> tuple[PublicKey, PrivateKey] | None: return pubkey, privkey -class StaticFiles: +def add_static_children(root: IResource): """ - Serve static files includes as resources. + Add static files from C{allmydata.web} to the given resource. Package resources may be on the filesystem, or they may be in a zip or something, so we need to do a bit more work to serve them as static files. """ - - def __init__(self): - self._temporary_file_manager = ExitStack() - - @classmethod - def add_static_children(cls, root: IResource): - """Add static files from C{allmydata.web} to the given resource.""" - self = cls() - static_dir = resource_files("allmydata.web") / "static" - for child in static_dir.iterdir(): - child_path = child.name.encode("utf-8") - root.putChild(child_path, static.File( - self._temporary_file_manager.enter_context(as_file(child)) - )) - root.__static_files_cleanup = self + temporary_file_manager = ExitStack() + static_dir = resource_files("allmydata.web") / "static" + for child in static_dir.iterdir(): + child_path = child.name.encode("utf-8") + root.putChild(child_path, static.File( + temporary_file_manager.enter_context(as_file(child)) + )) + weakref.finalize(root, temporary_file_manager.close) diff --git a/src/allmydata/web/introweb.py b/src/allmydata/web/introweb.py index 880ff66e5..7cb74a1c1 100644 --- a/src/allmydata/web/introweb.py +++ b/src/allmydata/web/introweb.py @@ -10,7 +10,7 @@ from allmydata.web.common import ( render_time, MultiFormatResource, SlotsSequenceElement, - StaticFiles + add_static_children, ) @@ -28,7 +28,7 @@ class IntroducerRoot(MultiFormatResource): self.introducer_service = introducer_node.getServiceNamed("introducer") # necessary as a root Resource self.putChild(b"", self) - StaticFiles.add_static_children(self) + add_static_children(self) def _create_element(self): """ diff --git a/src/allmydata/web/root.py b/src/allmydata/web/root.py index 5ebac3f0a..090f706f5 100644 --- a/src/allmydata/web/root.py +++ b/src/allmydata/web/root.py @@ -42,7 +42,7 @@ from allmydata.web.common import ( render_time_delta, render_time, render_time_attr, - StaticFiles, + add_static_children, ) from allmydata.web.private import ( create_private_tree, @@ -242,7 +242,7 @@ class Root(MultiFormatResource): self.putChild(b"statistics", status.Statistics(client.stats_provider)) self.putChild(b"report_incident", IncidentReporter()) - StaticFiles.add_static_children(self) + add_static_children(self) @exception_to_child def getChild(self, path, request):