duck.routes.route_registry¶
Class for registering routes/url_patterns/urls/paths
Module Contents¶
Classes¶
A registry for storing and retrieving routes (URL patterns) with associated handlers. |
Data¶
API¶
- class duck.routes.route_registry.BaseRouteRegistry[source]¶
A registry for storing and retrieving routes (URL patterns) with associated handlers.
This registry supports wildcard patterns (*), automatically replacing angle bracket placeholders like
with wildcards. It ensures uniqueness of routes both by name and by URL pattern. Initialization
- extract_kwargs_from_url(url: str, registered_url: str) dict[source]¶
Extracts dynamic parameters from a URL based on a registered URL pattern.
Example:
Given:
url:
/articles/what-is-money/1/registered_url:
/articles/<name>/<article-number>/
This function identifies and extracts dynamic values based on placeholders in the registered URL pattern.
How it works:
<name>captureswhat-is-money<article-number>captures1
The result is a dictionary:
{ "name": "what-is-money", "article-number": "1" }Edge Case:
If the registered pattern ends with a dynamic placeholder (
/<path>), the remaining part of the URL is assigned to that variable.Example:
url:
/files/user/docs/readme.txtregistered_url:
/files/<path>
Produces:
{"path": "user/docs/readme.txt"}- Parameters:
url – The actual URL to process.
registered_url – The template pattern containing placeholders.
- Returns:
A dictionary of extracted parameters and their corresponding values.
- Return type:
dict
- Raises:
RouteError – If the provided URL does not match the registered pattern.
- fetch_route_info_by_name(name: str) Dict[source]¶
Fetches the handler, allowed methods, URL pattern for a route, etc by its name
Note: this does not generate any handler kwargs because a real URL is needed not a Name only
- Parameters:
name – The name of the route to retrieve.
- Returns:
A dictionary containing the name, handler function, handler keyword arguments, a list of allowed methods, and the URL pattern.
- Return type:
Dict
- Raises:
RouteNotError – If no route with the given name is found.
- fetch_route_info_by_url(url_path: str) Dict[source]¶
Fetches the handler and allowed methods for a given URL path.
This generates handler kwargs rather than method fetch_route_info_by_name
- Parameters:
url_path – The URL path to match.
- Returns:
A dictionary containing the route details.
- Return type:
Dict
- Raises:
RouteNotFoundError – If no matching route is found.
RouteError – If URL in bad format
- regex_register(re_url: str, handler: Callable, name: Optional[str] = None, methods: Optional[List[str]] = None, **kw)[source]¶
Registers a Regular expression route
- Parameters:
re_url – Regular expression route (e.g /some/path/.*)
handler – The view or handler for the route.
name – The name for the route. (optional)
methods – The supported methods for the route. Defaults to None to support all methods.
- register(url_path: str, handler: Callable, name: Optional[str] = None, methods: Optional[List[str]] = None)[source]¶
Registers a Regular expression route
- Parameters:
url_path – Regular expression route (e.g /some/path/.*)
handler – The view or handler for the route.
name – The name for the route. (optional)
methods – The supported methods for the route. Defaults to None to support all methods.
- Raises:
RouteError – If a route with the same name or conflicting URL pattern already exists or a Bad URL path
AssertionError – If handler argument is not a Callable
- url_map¶
‘defaultdict(…)’
Mapping of URLs to route details
- duck.routes.route_registry.RouteRegistry¶
‘Lazy(…)’