-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows compilers to work in a wider variety of ways.
- Loading branch information
Showing
7 changed files
with
186 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
from typing import Any | ||
|
||
from starlette.convertors import Convertor | ||
from starlette.routing import compile_path as _compile_starlette_path | ||
|
||
from idom_router.types import Route | ||
|
||
|
||
def compile_starlette_route(route: Route) -> StarletteRoutePattern: | ||
pattern, _, converters = _compile_starlette_path(route.path) | ||
return StarletteRoutePattern(pattern, converters) | ||
|
||
|
||
class StarletteRoutePattern: | ||
def __init__( | ||
self, | ||
pattern: re.Pattern[str], | ||
converters: dict[str, Convertor], | ||
) -> None: | ||
self.pattern = pattern | ||
self.key = pattern.pattern | ||
self.converters = converters | ||
|
||
def match(self, path: str) -> dict[str, Any] | None: | ||
match = self.pattern.match(path) | ||
if match: | ||
return { | ||
k: self.converters[k].convert(v) if k in self.converters else v | ||
for k, v in match.groupdict().items() | ||
} | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
from dataclasses import dataclass | ||
from typing import Callable, Any, Protocol, Sequence | ||
from typing import Any, Sequence, TypeVar | ||
|
||
from idom.types import Key | ||
from typing_extensions import Self, Protocol | ||
|
||
|
||
@dataclass | ||
class Route: | ||
path: str | ||
element: Any | ||
routes: Sequence[Route] | ||
|
||
def __init__(self, path: str, element: Any | None, *routes: Route) -> None: | ||
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 | ||
self.routes = (*routes_, *routes) | ||
|
||
|
||
class RouteCompiler(Protocol): | ||
def __call__(self, route: str) -> RoutePattern: | ||
... | ||
R = TypeVar("R", bound=Route, contravariant=True) | ||
|
||
|
||
@dataclass | ||
class RoutePattern: | ||
pattern: re.Pattern[str] | ||
converters: dict[str, Callable[[Any], Any]] | ||
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""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
idom >=0.40.2,<0.41 | ||
idom >=1 | ||
typing_extensions | ||
starlette |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.