🎨 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.main import SpiderwebRouter # noqa: F401
from spiderweb.middleware import * # noqa: F401, F403 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 import time
from logging import Logger from logging import Logger
from threading import Thread from threading import Thread
from typing import NoReturn, Callable, Any from typing import NoReturn
from wsgiref.simple_server import WSGIServer, WSGIRequestHandler from wsgiref.simple_server import WSGIServer, WSGIRequestHandler
from spiderweb.constants import __version__ from spiderweb.constants import __version__

View File

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

View File

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

View File

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

View File

@ -85,7 +85,7 @@ class RoutesMiddleware:
def get_error_route(self, code: int) -> Callable: def get_error_route(self, code: int) -> Callable:
view = self._error_routes.get(code) or globals().get(f"http{code}") view = self._error_routes.get(code) or globals().get(f"http{code}")
if not view: if not view:
return http500 return http500 # noqa: F405
return view return view
def check_for_route_duplicates(self, path: str): 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: def is_safe_path(path: str) -> bool:
# this cannot possibly catch all issues # 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]: def get_http_status_by_code(code: int) -> Optional[str]: