Skip to content

Commit

Permalink
mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
lloesche committed Oct 4, 2023
1 parent d4be8d5 commit cfc3664
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
28 changes: 14 additions & 14 deletions fixca/ca.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ def __init__(
if self.mountpoint not in ("/", ""):
self.config[self.mountpoint] = config

@cherrypy.expose
@cherrypy.tools.allow(methods=["GET"])
@cherrypy.expose # type: ignore
@cherrypy.tools.allow(methods=["GET"]) # type: ignore
def health(self) -> str:
cherrypy.response.headers["Content-Type"] = "text/plain"
unhealthy = [f"- {name}" for name, fn in self.health_conditions.items() if not fn()]
Expand All @@ -197,8 +197,8 @@ def health(self) -> str:
cherrypy.response.headers["Content-Type"] = "text/plain"
return "not ok\r\n\r\n" + "\r\n".join(unhealthy) + "\r\n"

@cherrypy.expose
@cherrypy.tools.allow(methods=["GET"])
@cherrypy.expose # type: ignore
@cherrypy.tools.allow(methods=["GET"]) # type: ignore
def metrics(self) -> bytes:
cherrypy.response.headers["Content-Type"] = CONTENT_TYPE_LATEST
return generate_latest()
Expand All @@ -212,8 +212,8 @@ def __init__(self, ca: CertificateAuthority, psk: str) -> None:
self.config = {"/": {"tools.gzip.on": False}}
PSK = self.psk

@cherrypy.expose
@cherrypy.tools.allow(methods=["GET"])
@cherrypy.expose # type: ignore
@cherrypy.tools.allow(methods=["GET"]) # type: ignore
def cert(self) -> bytes:
assert self.psk is not None and self.ca.cert is not None
fingerprint = cert_fingerprint(self.ca.cert)
Expand All @@ -225,9 +225,9 @@ def cert(self) -> bytes:
)
return cert_to_bytes(self.ca.cert)

@cherrypy.expose
@cherrypy.tools.allow(methods=["POST"])
@cherrypy.tools.jwt_check()
@cherrypy.expose # type: ignore
@cherrypy.tools.allow(methods=["POST"]) # type: ignore
@cherrypy.tools.jwt_check() # type: ignore
def sign(self) -> bytes:
try:
csr = load_csr_from_bytes(cherrypy.request.body.read())
Expand All @@ -244,11 +244,11 @@ def sign(self) -> bytes:
cherrypy.response.headers["Content-Disposition"] = f'attachment; filename="{filename}"'
return cert_to_bytes(crt)

@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
@cherrypy.tools.allow(methods=["POST"])
@cherrypy.tools.jwt_check()
@cherrypy.expose # type: ignore
@cherrypy.tools.json_out() # type: ignore
@cherrypy.tools.json_in() # type: ignore
@cherrypy.tools.allow(methods=["POST"]) # type: ignore
@cherrypy.tools.jwt_check() # type: ignore
def generate(self) -> Dict[str, Any]:
try:
assert self.ca.cert is not None
Expand Down
17 changes: 8 additions & 9 deletions fixca/utils.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import time
from functools import wraps
from typing import Callable, Any, Tuple, Dict, Union, TypeVar, Type
from typing import Callable, Any, Tuple, Dict, Union


def str_to_bool(s: Union[str, bool]) -> bool:
return str(s).lower() in ("true", "1", "yes")


RT = TypeVar("RT")


def memoize(
ttl: int = 60, cleanup_interval: int = 600, time_fn: Callable[[], float] = time.time
) -> Callable[[Callable[..., RT]], Callable[..., RT]]:
ttl: int = 60,
cleanup_interval: int = 600,
time_fn: Callable[[], float] = time.time,
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
last_cleanup: float = 0.0
cache: Dict[Tuple[Callable[..., RT], Tuple[Any, ...], frozenset[Tuple[str, Any]]], Tuple[RT, float]] = {}
cache: Dict[Tuple[Callable[..., Any], Tuple[Any, ...], frozenset[Tuple[str, Any]]], Tuple[Any, float]] = {}

def decorating_function(user_function: Callable[..., RT]) -> Callable[..., RT]:
def decorating_function(user_function: Callable[..., Any]) -> Callable[..., Any]:
@wraps(user_function)
def wrapper(*args: Any, **kwargs: Any) -> RT:
def wrapper(*args: Any, **kwargs: Any) -> Any:
now = time_fn()
key = (user_function, args, frozenset(kwargs.items()))
if key in cache:
Expand Down

0 comments on commit cfc3664

Please sign in to comment.