This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
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
4 changed files
with
73 additions
and
39 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 |
---|---|---|
@@ -1,14 +1,36 @@ | ||
import re | ||
|
||
from flect.constants import DYNAMIC_ROUTE_PREFIX | ||
from flect.constants import GROUP_ROUTE_PREFIX, LAYOUT_ROUTE_SUFFIX | ||
|
||
|
||
def path_priority(path: str) -> tuple: | ||
path = re.sub(rf"{DYNAMIC_ROUTE_PREFIX}[^/]+/", "", path) | ||
parts = path.split("/") | ||
""" | ||
Calculate the priority of a route path based on several weighted factors. | ||
catch_all_weight = float("inf") if "{path:path}" in path else 0 | ||
Parameters: | ||
- path (str): The route path for which to calculate the priority. | ||
Returns: | ||
- tuple: A tuple representing the path's priority. Lower values indicate higher priority. The factors are: | ||
1. catch_all_weight: Infinity if the path includes a catch-all pattern, indicating it should be matched last. Otherwise, 0. | ||
2. depth_weight: The depth of the path, with deeper paths considered more specific and thus lower priority. | ||
3. segment_weight: The last valid segment of the path, used for sorting when depths are equal. | ||
4. layout_weight: 1 if the path is for a layout route, 0 otherwise. Layout routes typically have lower priority. | ||
5. dynamic_weight: 1 if the path is for a dynamic route, 0 otherwise. Dynamic routes typically have lower priority. | ||
""" | ||
|
||
# Normalize path for processing | ||
normalized_path = re.sub(rf"{GROUP_ROUTE_PREFIX}[^/]+/", "", path) | ||
if normalized_path.endswith(LAYOUT_ROUTE_SUFFIX): | ||
normalized_path = normalized_path[: -len(LAYOUT_ROUTE_SUFFIX)] | ||
|
||
parts = normalized_path.strip("/").split("/") | ||
|
||
# Calculate weights | ||
catch_all_weight = float("inf") if "{path:path}" in normalized_path else 0 | ||
depth_weight = len(parts) | ||
dynamic_weight = int(path.count("{")) | ||
segment_weight = parts[-2] if len(parts) >= 2 else "" | ||
return catch_all_weight, depth_weight, segment_weight, dynamic_weight | ||
segment_weight = parts[-1] if parts else "" | ||
layout_weight = 1 if path.endswith(LAYOUT_ROUTE_SUFFIX) else 0 | ||
dynamic_weight = 1 if path.endswith("}/") else 0 | ||
|
||
return catch_all_weight, depth_weight, segment_weight, layout_weight, dynamic_weight |
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