From 403735470960c5674b6d680d12e25d05c26b65c8 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 14 Sep 2023 08:42:27 +0300 Subject: [PATCH] Allow for priorities on routes defs --- .github/workflows/python-publish.yml | 3 --- sanic_routing/group.py | 9 +++++++++ sanic_routing/route.py | 11 ++++++++--- sanic_routing/router.py | 8 ++++++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index 8987d99..90e73f6 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -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: diff --git a/sanic_routing/group.py b/sanic_routing/group.py index 4368847..a435d8f 100644 --- a/sanic_routing/group.py +++ b/sanic_routing/group.py @@ -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) @@ -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 diff --git a/sanic_routing/route.py b/sanic_routing/route.py index a5088d2..2f7f820 100644 --- a/sanic_routing/route.py +++ b/sanic_routing/route.py @@ -28,6 +28,7 @@ class Route: "parts", "path", "pattern", + "priority", "regex", "requirements", "router", @@ -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() @@ -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]', , '[A-z]') + ```text + parse_parameter_string('')` -> ('param_one', '[A-z]', , '[A-z]') + ``` :param parameter_string: String to parse :return: tuple containing diff --git a/sanic_routing/router.py b/sanic_routing/router.py index 0007d4f..64b52c8 100644 --- a/sanic_routing/router.py +++ b/sanic_routing/router.py @@ -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 @@ -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] @@ -223,6 +229,7 @@ def add( unquote=unquote, static=static, regex=regex, + priority=priority, ) group = self.group_class(route) @@ -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