🎨 run black and fix ruff errors

This commit is contained in:
Joe Kaufeld 2024-08-21 21:07:28 -04:00
parent c9b7458014
commit e10fd02e99
7 changed files with 28 additions and 28 deletions

View File

@ -1,3 +1,3 @@
from spiderweb.main import SpiderwebRouter # noqa: F401
from spiderweb.middleware import * # noqa: F401, F403
from spiderweb.constants import __version__
from spiderweb.constants import __version__ as __version__

View File

@ -3,7 +3,7 @@ import threading
import time
from logging import Logger
from threading import Thread
from typing import NoReturn, Callable, Any
from typing import NoReturn
from wsgiref.simple_server import WSGIServer, WSGIRequestHandler
from spiderweb.constants import __version__

View File

@ -21,7 +21,7 @@ from spiderweb.exceptions import (
)
from spiderweb.local_server import LocalServerMiddleware
from spiderweb.request import Request
from spiderweb.response import HttpResponse, TemplateResponse
from spiderweb.response import HttpResponse, TemplateResponse, JsonResponse
from spiderweb.routes import RoutesMiddleware
from spiderweb.secrets import FernetMiddleware
from spiderweb.utils import get_http_status_by_code
@ -87,15 +87,15 @@ class SpiderwebRouter(
f"Static files directory '{str(static_dir)}' does not exist."
)
raise ConfigError
self.add_route(r"/static/<str:filename>", send_file)
self.add_route(r"/static/<str:filename>", send_file) # noqa: F405
def fire_response(self, start_response, request: Request, resp: HttpResponse):
try:
status = get_http_status_by_code(resp.status_code)
cookies = []
if "Set-Cookie" in resp.headers:
cookies = resp.headers['Set-Cookie']
del resp.headers['Set-Cookie']
cookies = resp.headers["Set-Cookie"]
del resp.headers["Set-Cookie"]
headers = list(resp.headers.items())
for c in cookies:
headers.append(("Set-Cookie", c))
@ -157,7 +157,7 @@ class SpiderwebRouter(
def prepare_and_fire_response(self, start_response, request, resp) -> list[bytes]:
try:
if isinstance(resp, dict):
self.fire_response(request, JsonResponse(data=resp))
self.fire_response(start_response, request, JsonResponse(data=resp))
if isinstance(resp, TemplateResponse):
resp.set_template_loader(self.env)
@ -200,9 +200,11 @@ class SpiderwebRouter(
try:
if handler:
abort = self.process_request_middleware(request)
if abort:
return
abort_view = self.process_request_middleware(request)
if abort_view:
return self.prepare_and_fire_response(
start_response, request, abort_view
)
resp = handler(request, **additional_args)
if resp is None:
raise NoResponseError(f"View {handler} returned None.")

View File

@ -1,7 +1,7 @@
from typing import Callable, ClassVar
from .base import SpiderwebMiddleware
from .csrf import CSRFMiddleware
from .base import SpiderwebMiddleware as SpiderwebMiddleware
from .csrf import CSRFMiddleware as CSRFMiddleware
from ..exceptions import ConfigError, UnusedMiddleware
from ..request import Request
from ..response import HttpResponse
@ -33,8 +33,7 @@ class MiddlewareMiddleware:
continue
if resp:
self.process_response_middleware(request, resp)
self.fire_response(request, resp)
return True # abort further processing
return resp # abort further processing
def process_response_middleware(
self, request: Request, response: HttpResponse

View File

@ -42,14 +42,14 @@ class HttpResponse:
self,
name: str,
value: str,
domain: str=None,
domain: str = None,
expires: datetime.datetime = None,
http_only: bool=None,
max_age: int=None,
partitioned: bool=None,
path: str=None,
secure: bool=False,
same_site: str=None
http_only: bool = None,
max_age: int = None,
partitioned: bool = None,
path: str = None,
secure: bool = False,
same_site: str = None,
):
if not bool(re.match(REGEX_COOKIE_NAME, name)):
url = "https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes"
@ -94,7 +94,6 @@ class HttpResponse:
else:
self.headers["Set-Cookie"] = [cookie]
def render(self) -> str:
return str(self.body)

View File

@ -85,7 +85,7 @@ class RoutesMiddleware:
def get_error_route(self, code: int) -> Callable:
view = self._error_routes.get(code) or globals().get(f"http{code}")
if not view:
return http500
return http500 # noqa: F405
return view
def check_for_route_duplicates(self, path: str):

View File

@ -15,7 +15,7 @@ def import_by_string(name):
def is_safe_path(path: str) -> bool:
# this cannot possibly catch all issues
return not ".." in str(path)
return ".." not in str(path)
def get_http_status_by_code(code: int) -> Optional[str]: