Fix mypy complaints, simplifying code while we're at it

This commit is contained in:
Itamar Turner-Trauring 2023-11-17 11:18:28 -05:00
parent ccc35bf7cd
commit e6630b59f7
4 changed files with 16 additions and 23 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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):
"""

View File

@ -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):