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: if accept.best == CBOR_MIME_TYPE:
request.setHeader("Content-Type", CBOR_MIME_TYPE) request.setHeader("Content-Type", CBOR_MIME_TYPE)
f = TemporaryFile() f = TemporaryFile()
cbor2.dump(data, f) cbor2.dump(data, f) # type: ignore
def read_data(offset: int, length: int) -> bytes: def read_data(offset: int, length: int) -> bytes:
f.seek(offset) f.seek(offset)

View File

@ -6,7 +6,7 @@ from __future__ import annotations
from six import ensure_str from six import ensure_str
from importlib.resources import files as resource_files, as_file from importlib.resources import files as resource_files, as_file
from contextlib import ExitStack from contextlib import ExitStack
import weakref
from typing import Optional, Union, TypeVar, overload from typing import Optional, Union, TypeVar, overload
from typing_extensions import Literal from typing_extensions import Literal
@ -857,26 +857,19 @@ def get_keypair(request: IRequest) -> tuple[PublicKey, PrivateKey] | None:
return pubkey, privkey 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 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 or something, so we need to do a bit more work to serve them as
static files. static files.
""" """
temporary_file_manager = ExitStack()
def __init__(self): static_dir = resource_files("allmydata.web") / "static"
self._temporary_file_manager = ExitStack() for child in static_dir.iterdir():
child_path = child.name.encode("utf-8")
@classmethod root.putChild(child_path, static.File(
def add_static_children(cls, root: IResource): temporary_file_manager.enter_context(as_file(child))
"""Add static files from C{allmydata.web} to the given resource.""" ))
self = cls() weakref.finalize(root, temporary_file_manager.close)
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

View File

@ -10,7 +10,7 @@ from allmydata.web.common import (
render_time, render_time,
MultiFormatResource, MultiFormatResource,
SlotsSequenceElement, SlotsSequenceElement,
StaticFiles add_static_children,
) )
@ -28,7 +28,7 @@ class IntroducerRoot(MultiFormatResource):
self.introducer_service = introducer_node.getServiceNamed("introducer") self.introducer_service = introducer_node.getServiceNamed("introducer")
# necessary as a root Resource # necessary as a root Resource
self.putChild(b"", self) self.putChild(b"", self)
StaticFiles.add_static_children(self) add_static_children(self)
def _create_element(self): def _create_element(self):
""" """

View File

@ -42,7 +42,7 @@ from allmydata.web.common import (
render_time_delta, render_time_delta,
render_time, render_time,
render_time_attr, render_time_attr,
StaticFiles, add_static_children,
) )
from allmydata.web.private import ( from allmydata.web.private import (
create_private_tree, create_private_tree,
@ -242,7 +242,7 @@ class Root(MultiFormatResource):
self.putChild(b"statistics", status.Statistics(client.stats_provider)) self.putChild(b"statistics", status.Statistics(client.stats_provider))
self.putChild(b"report_incident", IncidentReporter()) self.putChild(b"report_incident", IncidentReporter())
StaticFiles.add_static_children(self) add_static_children(self)
@exception_to_child @exception_to_child
def getChild(self, path, request): def getChild(self, path, request):