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(โฆ)โ