Skip to content

Commit

Permalink
Fix/ppf (#76)
Browse files Browse the repository at this point in the history
* Remove unused middleware
* Optimise dclass
  • Loading branch information
tarsil authored Jun 30, 2024
1 parent 20cb2dc commit 0f65ad4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
8 changes: 0 additions & 8 deletions lilya/_internal/_path_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import math
import re
import uuid
from dataclasses import dataclass
from datetime import datetime
from typing import Any, ClassVar, Generic, TypeVar

Expand All @@ -15,7 +14,6 @@
path_regex = re.compile(r"{([^:]+):([^}]+)}")


@dataclass(frozen=True, eq=True)
class Transformer(Generic[T]):
"""
Base for all path transformers
Expand All @@ -32,7 +30,6 @@ def normalise(self, value: T) -> str: # pragma: no cover
raise NotImplementedError() # pragma: no cover


@dataclass(frozen=True, eq=True)
class StringTransformer(Transformer[str]):
regex = "[^/]+"

Expand All @@ -46,7 +43,6 @@ def normalise(self, value: str) -> str:
return value


@dataclass(frozen=True, eq=True)
class PathTransformer(Transformer[str]):
regex = ".*"

Expand All @@ -57,7 +53,6 @@ def normalise(self, value: str) -> str:
return str(value)


@dataclass(frozen=True, eq=True)
class IntegerTransformer(Transformer[int]):
regex = "[0-9]+"

Expand All @@ -70,7 +65,6 @@ def normalise(self, value: int) -> str:
return str(value)


@dataclass(frozen=True, eq=True)
class FloatTransformer(Transformer[float]):
regex = r"[0-9]+(\.[0-9]+)?"

Expand All @@ -85,7 +79,6 @@ def normalise(self, value: float) -> str:
return (f"{value:0.20f}").rstrip("0").rstrip(".")


@dataclass(frozen=True, eq=True)
class UUIDTransformer(Transformer[uuid.UUID]):
regex = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"

Expand All @@ -96,7 +89,6 @@ def normalise(self, value: uuid.UUID) -> str:
return str(value)


@dataclass(frozen=True, eq=True)
class DatetimeTransformer(Transformer[datetime]):
regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]+)?"

Expand Down
2 changes: 0 additions & 2 deletions lilya/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from lilya.conf.global_settings import Settings
from lilya.datastructures import State, URLPath
from lilya.middleware.app_settings import ApplicationSettingsMiddleware
from lilya.middleware.asyncexit import AsyncExitStackMiddleware
from lilya.middleware.base import DefineMiddleware
from lilya.middleware.exceptions import ExceptionMiddleware
from lilya.middleware.server_error import ServerErrorMiddleware
Expand Down Expand Up @@ -492,7 +491,6 @@ def build_middleware_stack(self) -> ASGIApp:
*self.custom_middleware,
DefineMiddleware(ApplicationSettingsMiddleware),
DefineMiddleware(ExceptionMiddleware, handlers=exception_handlers, debug=self.debug),
DefineMiddleware(AsyncExitStackMiddleware),
]

app = self.router
Expand Down
44 changes: 28 additions & 16 deletions lilya/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from abc import ABC
from copy import copy
from dataclasses import asdict, dataclass
from functools import lru_cache
from http.cookies import SimpleCookie
from typing import (
Expand Down Expand Up @@ -591,15 +590,15 @@ def make_absolute_url(self, base_url: str | URL) -> URL:
return URL(scheme=scheme, netloc=netloc, path=path)


@dataclass
class Secret:
"""
Holds the information about a string secret.
This will make sure no secrets are leaked
in stack traces.
"""

value: str = None
def __init__(self, value: str = None) -> None:
self.value = value

def __str__(self) -> str:
return self.value
Expand Down Expand Up @@ -698,27 +697,40 @@ def __repr__(self) -> str:
)


@dataclass
class Cookie:
key: str
value: str | None = None
max_age: int | None = None
expires: int | None = None
path: str = "/"
domain: str | None = None
secure: bool | None = None
httponly: bool | None = None
samesite: Literal["lax", "strict", "none"] = "lax"
description: str | None = None

def __init__(
self,
key: str,
value: str | None = None,
max_age: int | None = None,
expires: int | None = None,
path: str = "/",
domain: str | None = None,
secure: bool | None = None,
httponly: bool | None = None,
samesite: Literal["lax", "strict", "none"] = "lax",
description: str | None = None,
) -> None:
self.key = key
self.value = value
self.max_age = max_age
self.expires = expires
self.path = path
self.domain = domain
self.secure = secure
self.httponly = httponly
self.samesite = samesite
self.description = description

def to_header(self, **kwargs: Any) -> str:
simple_cookie: SimpleCookie = SimpleCookie()
simple_cookie[self.key] = self.value or ""
if self.max_age:
simple_cookie[self.key]["max-age"] = self.max_age
cookie_dict = asdict(self)
cookie_dict = dict(self.__dict__)
for key in ["expires", "path", "domain", "secure", "httponly", "samesite"]:
if cookie_dict[key] is not None:
if self.__dict__[key] is not None:
simple_cookie[self.key][key] = cookie_dict[key]
return simple_cookie.output(**kwargs).strip()

Expand Down

0 comments on commit 0f65ad4

Please sign in to comment.