-
-
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.
- Loading branch information
Showing
10 changed files
with
195 additions
and
201 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
# the version is statically loaded by setup.py | ||
__version__ = "0.0.1" | ||
|
||
from idom_router.types import Route, RouteCompiler, RouteResolver | ||
from . import simple | ||
from .core import create_router, link, route, router_component, use_params, use_query | ||
from .types import Route, RouteCompiler, RouteResolver | ||
|
||
from .core import link, create_router, router_component, use_params, use_query | ||
|
||
__all__ = [ | ||
__all__ = ( | ||
"create_router", | ||
"link", | ||
"route", | ||
"route", | ||
"Route", | ||
"RouteCompiler", | ||
"router_component", | ||
"RouteResolver", | ||
"use_location", | ||
"simple", | ||
"use_params", | ||
"use_query", | ||
] | ||
) |
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
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,86 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
import uuid | ||
from typing import Any, Callable | ||
|
||
from typing_extensions import TypedDict | ||
|
||
from idom_router.core import create_router | ||
from idom_router.types import Route | ||
|
||
__all__ = ["router"] | ||
|
||
ConversionFunc = Callable[[str], Any] | ||
ConverterMapping = dict[str, ConversionFunc] | ||
|
||
PARAM_REGEX = re.compile(r"{(?P<name>\w+)(?P<type>:\w+)?}") | ||
|
||
|
||
class SimpleResolver: | ||
def __init__(self, route: Route) -> None: | ||
self.element = route.element | ||
self.pattern, self.converters = parse_path(route.path) | ||
self.key = self.pattern.pattern | ||
|
||
def resolve(self, path: str) -> tuple[Any, dict[str, Any]] | None: | ||
print(path) | ||
print(self.key) | ||
match = self.pattern.match(path) | ||
if match: | ||
return ( | ||
self.element, | ||
{k: self.converters[k](v) for k, v in match.groupdict().items()}, | ||
) | ||
return None | ||
|
||
|
||
def parse_path(path: str) -> tuple[re.Pattern[str], ConverterMapping]: | ||
pattern = "^" | ||
last_match_end = 0 | ||
converters: ConverterMapping = {} | ||
for match in PARAM_REGEX.finditer(path): | ||
param_name = match.group("name") | ||
param_type = (match.group("type") or "str").lstrip(":") | ||
try: | ||
param_conv = CONVERSION_TYPES[param_type] | ||
except KeyError: | ||
raise ValueError(f"Unknown conversion type {param_type!r} in {path!r}") | ||
pattern += re.escape(path[last_match_end : match.start()]) | ||
pattern += f"(?P<{param_name}>{param_conv['regex']})" | ||
converters[param_name] = param_conv["func"] | ||
last_match_end = match.end() | ||
pattern += re.escape(path[last_match_end:]) + "$" | ||
return re.compile(pattern), converters | ||
|
||
|
||
class ConversionInfo(TypedDict): | ||
regex: str | ||
func: ConversionFunc | ||
|
||
|
||
CONVERSION_TYPES: dict[str, ConversionInfo] = { | ||
"str": { | ||
"regex": r"[^/]+", | ||
"func": str, | ||
}, | ||
"int": { | ||
"regex": r"\d+", | ||
"func": int, | ||
}, | ||
"float": { | ||
"regex": r"\d+(\.\d+)?", | ||
"func": float, | ||
}, | ||
"uuid": { | ||
"regex": r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", | ||
"func": uuid.UUID, | ||
}, | ||
"path": { | ||
"regex": r".+", | ||
"func": str, | ||
}, | ||
} | ||
|
||
|
||
router = create_router(SimpleResolver) |
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,3 +1,2 @@ | ||
idom >=1 | ||
typing_extensions | ||
starlette |
Oops, something went wrong.