-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtypes.py
43 lines (31 loc) · 1.07 KB
/
types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Sequence, TypeVar
from idom.types import Key
from typing_extensions import Protocol, Self
@dataclass
class Route:
path: str
element: Any
routes: Sequence[Self]
def __init__(
self,
path: str,
element: Any | None,
*routes_: Self,
# we need kwarg in order to play nice with the expected dataclass interface
routes: Sequence[Self] = (),
) -> None:
self.path = path
self.element = element
self.routes = (*routes_, *routes)
R = TypeVar("R", bound=Route, contravariant=True)
class RouteCompiler(Protocol[R]):
def __call__(self, route: R) -> RoutePattern:
"""Compile a route into a pattern that can be matched against a path"""
class RoutePattern(Protocol):
@property
def key(self) -> Key:
"""Uniquely identified this pattern"""
def match(self, path: str) -> dict[str, Any] | None:
"""Returns otherwise a dict of path parameters if the path matches, else None"""