Skip to content

Commit 5ac15e1

Browse files
committed
Tighen up the typing
1 parent d5c9e52 commit 5ac15e1

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies = [
3232
"pkginfo>=1.12",
3333
"readme-renderer[md]",
3434
"simple-repository~=0.9",
35+
"typing-extensions",
3536
"uvicorn",
3637
"authlib",
3738
"starlette[full]",
@@ -64,6 +65,11 @@ ensure_newline_before_comments = true
6465
line_length = 88
6566
force_sort_within_sections = true
6667

68+
# [tool.mypy]
69+
# check_untyped_defs = true
70+
# disallow_untyped_defs = true
71+
# disallow_untyped_calls = true
72+
6773
[[tool.mypy.overrides]]
6874
module = [
6975
"diskcache",

simple_repository_browser/controller.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# from __future__ import annotations
2+
13
import asyncio
24
import dataclasses
35
from enum import Enum
@@ -10,7 +12,7 @@
1012
from packaging.version import InvalidVersion, Version
1113

1214
from . import errors, model, view
13-
from .static_files import HashedStaticFileHandler
15+
from .static_files import HashedStaticFileHandler, StaticFilesManifest
1416

1517

1618
@dataclasses.dataclass(frozen=True)
@@ -81,7 +83,7 @@ def __init__(self, model: model.Model, view: view.View) -> None:
8183
self.model = model
8284
self.view = view
8385

84-
def create_router(self, static_files_manifest) -> fastapi.APIRouter:
86+
def create_router(self, static_files_manifest: StaticFilesManifest) -> fastapi.APIRouter:
8587
router = self.router.build_fastapi_router(self)
8688
router.mount("/static", HashedStaticFileHandler(manifest=static_files_manifest), name="static")
8789
return router

simple_repository_browser/static_files.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import argparse
24
from hashlib import sha256
35
import json
@@ -9,14 +11,20 @@
911

1012
from starlette.responses import Response
1113
from starlette.staticfiles import StaticFiles
14+
from starlette.types import Scope
1215
from typing_extensions import override
1316

17+
#: A StaticFilesManifest maps the relative path to the static files root
18+
#: (i.e. the input value of a template requiring a static file) to the relative
19+
#: path that should be rendered in a template, and the full path of the file on
20+
#: disk.
21+
StaticFilesManifest: typing.TypeAlias = dict[str, tuple[str, pathlib.Path]]
22+
1423

15-
def compile_static_files(*, destination: pathlib.Path, sources: typing.Sequence[pathlib.Path]):
24+
def compile_static_files(*, destination: pathlib.Path, manifest: StaticFilesManifest) -> None:
1625
"""Compile a static directory from one or more source directories."""
1726
# This function is designed to write the static files, could be useful for serving static
1827
# files via apache/nginx/etc.
19-
manifest = generate_manifest(sources)
2028
file_map: dict[str, dict[str, str]] = {'file-map': {}}
2129

2230
for input_filename, (hashed_relpath, source_path) in manifest.items():
@@ -29,7 +37,7 @@ def compile_static_files(*, destination: pathlib.Path, sources: typing.Sequence[
2937
(destination / '.gitignore').write_text('*')
3038

3139

32-
def generate_manifest(sources: typing.Sequence[pathlib.Path]) -> dict[str, tuple[str, pathlib.Path]]:
40+
def generate_manifest(sources: typing.Sequence[pathlib.Path]) -> StaticFilesManifest:
3341
"""
3442
Generate a manifest which maps template_rel_path to a (hashed_relpath, full_path) tuple.
3543
"""
@@ -54,7 +62,7 @@ def generate_manifest(sources: typing.Sequence[pathlib.Path]) -> dict[str, tuple
5462

5563

5664
class HashedStaticFileHandler(StaticFiles):
57-
def __init__(self, *, manifest, **kwargs):
65+
def __init__(self, *, manifest: StaticFilesManifest, **kwargs) -> None:
5866
super().__init__(**kwargs)
5967
self.manifest = manifest
6068
self._inverted_manifest = {src: path for src, path in manifest.values()}
@@ -64,10 +72,10 @@ def lookup_path(self, path: str) -> tuple[str, os.stat_result | None]:
6472
actual_path = self._inverted_manifest.get(path)
6573
if actual_path is None:
6674
return super().lookup_path(path)
67-
return actual_path, os.stat(actual_path)
75+
return str(actual_path), os.stat(actual_path)
6876

6977
@override
70-
async def get_response(self, path: str, scope) -> Response:
78+
async def get_response(self, path: str, scope: Scope) -> Response:
7179
response = await super().get_response(path, scope)
7280
if response.status_code in [200, 304]:
7381
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
@@ -95,7 +103,8 @@ def main(argv: typing.Sequence[str]) -> None:
95103

96104
def handle_compile(args: argparse.Namespace):
97105
print(f'Writing static files to {args.destination}')
98-
compile_static_files(destination=args.destination, sources=args.source)
106+
manifest = generate_manifest(args.source)
107+
compile_static_files(destination=args.destination, manifest=manifest)
99108

100109

101110
if __name__ == '__main__':

simple_repository_browser/view.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
from starlette.datastructures import URL
88

99
from . import model
10+
from .static_files import StaticFilesManifest
1011

1112

1213
class View:
13-
def __init__(self, templates_paths: typing.Sequence[Path], browser_version: str, static_files_manifest: dict[str, tuple[str, Path]]):
14+
def __init__(self, templates_paths: typing.Sequence[Path], browser_version: str, static_files_manifest: StaticFilesManifest):
1415
self.templates_paths = templates_paths
1516
self.version = browser_version
1617
self.static_files_manifest = static_files_manifest

0 commit comments

Comments
 (0)