Skip to content

Commit

Permalink
Allow for priorities on routes defs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Sep 14, 2023
1 parent 8893c4a commit 4037354
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: Upload Python Package

on:
Expand Down
9 changes: 9 additions & 0 deletions sanic_routing/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ def finalize(self):
}
)

def prioritize_routes(self) -> None:
"""
Sorts the routes in the group by priority
"""
self._routes = tuple(
sorted(self._routes, key=lambda route: route.priority, reverse=True)
)

def reset(self):
self.methods_index = dict(self.methods_index)

Expand Down Expand Up @@ -163,6 +171,7 @@ def handler2(...):
)
else:
_routes.append(other_route)
_routes.sort(key=lambda route: route.priority, reverse=True)
self._routes = tuple(_routes)

@property
Expand Down
11 changes: 8 additions & 3 deletions sanic_routing/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Route:
"parts",
"path",
"pattern",
"priority",
"regex",
"requirements",
"router",
Expand Down Expand Up @@ -80,12 +81,15 @@ def __init__(
static: bool = False,
regex: bool = False,
overloaded: bool = False,
*,
priority: int = 0,
):
self.router = router
self.name = name
self.handler = handler # type: ignore
self.methods = frozenset(methods)
self.requirements = Requirements(requirements or {})
self.priority = priority

self.ctx = SimpleNamespace()
self.extra = SimpleNamespace()
Expand Down Expand Up @@ -326,10 +330,11 @@ def parse_parameter_string(self, parameter_string: str):
"""Parse a parameter string into its constituent name, type, and
pattern
For example::
For example:
parse_parameter_string('<param_one:[A-z]>')` ->
('param_one', '[A-z]', <class 'str'>, '[A-z]')
```text
parse_parameter_string('<param_one:[A-z]>')` -> ('param_one', '[A-z]', <class 'str'>, '[A-z]')
```
:param parameter_string: String to parse
:return: tuple containing
Expand Down
8 changes: 8 additions & 0 deletions sanic_routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def add(
unquote: bool = False, # noqa
overwrite: bool = False,
append: bool = False,
*,
priority: int = 0,
) -> Route:
# Can add a route with overwrite, or append, not both.
# - overwrite: if matching path exists, replace it
Expand All @@ -166,6 +168,10 @@ def add(
"Cannot add a route with both overwrite and append equal "
"to True"
)
if priority and not append:
raise FinalizationError(
"Cannot add a route with priority if append is False"
)
if not methods:
methods = [self.DEFAULT_METHOD]

Expand Down Expand Up @@ -223,6 +229,7 @@ def add(
unquote=unquote,
static=static,
regex=regex,
priority=priority,
)
group = self.group_class(route)

Expand Down Expand Up @@ -327,6 +334,7 @@ def finalize(self, do_compile: bool = True, do_optimize: bool = False):
group.finalize()
for route in group.routes:
route.finalize()
group.prioritize_routes()

# Evaluates all of the paths and arranges them into a hierarchichal
# tree of nodes
Expand Down

0 comments on commit 4037354

Please sign in to comment.