# Sanic Routing
## Background
Beginning in v21.3, Sanic makes use of this new AST-style router in two use cases:
1. Routing paths; and
2. Routing signals.
Therefore, this package comes with a `BaseRouter` that needs to be subclassed in order to be used for its specific needs.
Most Sanic users should never need to concern themselves with the details here.
## Basic Example
A simple implementation:
```python
import logging
from sanic_routing import BaseRouter
logging.basicConfig(level=logging.DEBUG)
class Router(BaseRouter):
def get(self, path, *args, **kwargs):
return self.resolve(path, *args, **kwargs)
router = Router()
router.add("/<foo>", lambda: ...)
router.finalize()
router.tree.display()
logging.info(router.find_route_src)
route, handler, params = router.get("/matchme", method="BASE", extra=None)
```
The above snippet uses `router.tree.display()` to show how the router has decided to arrange the routes into a tree. In this simple example:
```
<Node: level=0>
<Node: part=__dynamic__:str, level=1, groups=[<RouteGroup: path=<foo:str> len=1>], dynamic=True>
```
We can can see the code that the router has generated for us. It is available as a string at `router.find_route_src`.
```python
def find_route(path, method, router, basket, extra):
parts = tuple(path[1:].split(router.delimiter))
num = len(parts)
# node=1 // part=__dynamic__:str
if num == 1: # CHECK 1
try:
basket['__matches__'][0] = str(parts[0])
except ValueError:
pass
else:
# Return 1
return router.dynamic_routes[('<__dynamic__:str>',)][0], basket
raise NotFound
```
_FYI: If you are on Python 3.9, you can see a representation of the source after compilation at `router.find_route_src_compiled`_
## What's it doing?
Therefore, in general implementation requires you to:
1. Define a router with a `get` method;
2. Add one or more routes;
3. Finalize the router (`router.finalize()`); and
4. Call the router's `get` method.
_NOTE: You can call `router.finalize(False)` if you do not want to compile the source code into executable form. This is useful if you only intend to review the generated output._
Every time you call `router.add` you create one (1) new `Route` instance. Even if that one route is created with multiple methods, it generates a single instance. If you `add()` another `Route` that has a similar path structure (but, perhaps has differen methods) they will be grouped together into a `RouteGroup`. It is worth also noting that a `RouteGroup` is created the first time you call `add()`, but subsequent similar routes will reuse the existing grouping instance.
When you call `finalize()`, it is taking the defined route groups and arranging them into "nodes" in a hierarchical tree. A single node is a path segment. A `Node` instance can have one or more `RouteGroup` on it where the `Node` is the termination point for that path.
Perhaps an example is easier:
```python
router.add("/path/to/<foo>", lambda: ...)
router.add("/path/to/<foo:int>", lambda: ...)
router.add("/path/to/different/<foo>", lambda: ...)
router.add("/path/to/different/<foo>", lambda: ..., methods=["one", "two"])
```
The generated `RouteGroup` instances (3):
```
<RouteGroup: path=path/to/<foo:str> len=1>
<RouteGroup: path=path/to/<foo:int> len=1>
<RouteGroup: path=path/to/different/<foo:str> len=2>
```
The generated `Route` instances (4):
```
<Route: path=path/to/<foo:str>>
<Route: path=path/to/<foo:int>>
<Route: path=path/to/different/<foo:str>>
<Route: path=path/to/different/<foo:str>>
```
The Node Tree:
```
<Node: level=0>
<Node: part=path, level=1>
<Node: part=to, level=2>
<Node: part=different, level=3>
<Node: part=__dynamic__:str, level=4, groups=[<RouteGroup: path=path/to/different/<foo:str> len=2>], dynamic=True>
<Node: part=__dynamic__:int, level=3, groups=[<RouteGroup: path=path/to/<foo:int> len=1>], dynamic=True>
<Node: part=__dynamic__:str, level=3, groups=[<RouteGroup: path=path/to/<foo:str> len=1>], dynamic=True>
```
And, the generated source code:
```python
def find_route(path, method, router, basket, extra):
parts = tuple(path[1:].split(router.delimiter))
num = len(parts)
# node=1 // part=path
if num > 1: # CHECK 1
if parts[0] == "path": # CHECK 4
# node=1.1 // part=to
if num > 2: # CHECK 1
if parts[1] == "to": # CHECK 4
# node=1.1.1 // part=different
if num > 3: # CHECK 1
if parts[2] == "different": # CHECK 4
# node=1.1.1.1 // part=__dynamic__:str
if num == 4: # CHECK 1
try:
basket['__matches__'][3] = str(parts[3])
except ValueError:
pass
else:
if method in frozenset({'one', 'two'}):
route_idx = 0
elif method in frozenset({'BASE'}):
route_idx = 1
else:
raise NoMethod
# Return 1.1.1.1
return router.dynamic_routes[('path', 'to', 'different', '<__dynamic__:str>')][route_idx], basket
# node=1.1.2 // part=__dynamic__:int
if num >= 3: # CHECK 1
try:
basket['__matches__'][2] = int(parts[2])
except ValueError:
pass
else:
if num == 3: # CHECK 5
# Return 1.1.2
return router.dynamic_routes[('path', 'to', '<__dynamic__:int>')][0], basket
# node=1.1.3 // part=__dynamic__:str
if num >= 3: # CHECK 1
try:
basket['__matches__'][2] = str(parts[2])
except ValueError:
pass
else:
if num == 3: # CHECK 5
# Return 1.1.3
return router.dynamic_routes[('path', 'to', '<__dynamic__:str>')][0], basket
raise NotFound
```
## Special cases
The above example only shows routes that have a dynamic path segment in them (example: `<foo>`). But, there are other use cases that are covered differently:
1. *fully static paths* - These are paths with no parameters (example: `/user/login`). These are basically matched against a key/value store.
2. *regex paths* - If a route as a single regular expression match, then the whole route will be matched via regex. In general, this happens inline not too dissimilar than what we see in the above example.
3. *special regex paths* - The router comes with a special `path` type (example: `<foo:path>`) that can match on an expanded delimiter. This is also true for any regex that uses the path delimiter in it. These cannot be matched in the normal course since they are of unknown length.
Raw data
{
"_id": null,
"home_page": "https://github.com/sanic-org/sanic-routing/",
"name": "sanic-routing",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Adam Hopkins",
"author_email": "admhpkns@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/d1/5c/2a7edd14fbccca3719a8d680951d4b25f986752c781c61ccf156a6d1ebff/sanic-routing-23.12.0.tar.gz",
"platform": "any",
"description": "# Sanic Routing\n\n## Background\n\nBeginning in v21.3, Sanic makes use of this new AST-style router in two use cases:\n\n1. Routing paths; and\n2. Routing signals.\n\nTherefore, this package comes with a `BaseRouter` that needs to be subclassed in order to be used for its specific needs. \n\nMost Sanic users should never need to concern themselves with the details here.\n\n## Basic Example\n\nA simple implementation:\n\n```python\nimport logging\n\nfrom sanic_routing import BaseRouter\n\nlogging.basicConfig(level=logging.DEBUG)\n\n\nclass Router(BaseRouter):\n def get(self, path, *args, **kwargs):\n return self.resolve(path, *args, **kwargs)\n\n\nrouter = Router()\n\nrouter.add(\"/<foo>\", lambda: ...)\nrouter.finalize()\nrouter.tree.display()\nlogging.info(router.find_route_src)\n\nroute, handler, params = router.get(\"/matchme\", method=\"BASE\", extra=None)\n```\n\nThe above snippet uses `router.tree.display()` to show how the router has decided to arrange the routes into a tree. In this simple example:\n\n```\n<Node: level=0>\n <Node: part=__dynamic__:str, level=1, groups=[<RouteGroup: path=<foo:str> len=1>], dynamic=True>\n```\n\nWe can can see the code that the router has generated for us. It is available as a string at `router.find_route_src`.\n\n```python\ndef find_route(path, method, router, basket, extra):\n parts = tuple(path[1:].split(router.delimiter))\n num = len(parts)\n \n # node=1 // part=__dynamic__:str\n if num == 1: # CHECK 1\n try:\n basket['__matches__'][0] = str(parts[0])\n except ValueError:\n pass\n else:\n # Return 1\n return router.dynamic_routes[('<__dynamic__:str>',)][0], basket\n raise NotFound\n```\n\n_FYI: If you are on Python 3.9, you can see a representation of the source after compilation at `router.find_route_src_compiled`_\n\n## What's it doing?\n\nTherefore, in general implementation requires you to:\n\n1. Define a router with a `get` method;\n2. Add one or more routes;\n3. Finalize the router (`router.finalize()`); and\n4. Call the router's `get` method.\n\n_NOTE: You can call `router.finalize(False)` if you do not want to compile the source code into executable form. This is useful if you only intend to review the generated output._\n\nEvery time you call `router.add` you create one (1) new `Route` instance. Even if that one route is created with multiple methods, it generates a single instance. If you `add()` another `Route` that has a similar path structure (but, perhaps has differen methods) they will be grouped together into a `RouteGroup`. It is worth also noting that a `RouteGroup` is created the first time you call `add()`, but subsequent similar routes will reuse the existing grouping instance.\n\n\nWhen you call `finalize()`, it is taking the defined route groups and arranging them into \"nodes\" in a hierarchical tree. A single node is a path segment. A `Node` instance can have one or more `RouteGroup` on it where the `Node` is the termination point for that path.\n\nPerhaps an example is easier:\n\n```python\nrouter.add(\"/path/to/<foo>\", lambda: ...)\nrouter.add(\"/path/to/<foo:int>\", lambda: ...)\nrouter.add(\"/path/to/different/<foo>\", lambda: ...)\nrouter.add(\"/path/to/different/<foo>\", lambda: ..., methods=[\"one\", \"two\"])\n```\n\nThe generated `RouteGroup` instances (3):\n\n```\n<RouteGroup: path=path/to/<foo:str> len=1>\n<RouteGroup: path=path/to/<foo:int> len=1>\n<RouteGroup: path=path/to/different/<foo:str> len=2>\n```\n\nThe generated `Route` instances (4):\n\n```\n<Route: path=path/to/<foo:str>>\n<Route: path=path/to/<foo:int>>\n<Route: path=path/to/different/<foo:str>>\n<Route: path=path/to/different/<foo:str>>\n```\n\nThe Node Tree:\n\n```\n<Node: level=0>\n <Node: part=path, level=1>\n <Node: part=to, level=2>\n <Node: part=different, level=3>\n <Node: part=__dynamic__:str, level=4, groups=[<RouteGroup: path=path/to/different/<foo:str> len=2>], dynamic=True>\n <Node: part=__dynamic__:int, level=3, groups=[<RouteGroup: path=path/to/<foo:int> len=1>], dynamic=True>\n <Node: part=__dynamic__:str, level=3, groups=[<RouteGroup: path=path/to/<foo:str> len=1>], dynamic=True>\n```\n\nAnd, the generated source code:\n\n```python\ndef find_route(path, method, router, basket, extra):\n parts = tuple(path[1:].split(router.delimiter))\n num = len(parts)\n \n # node=1 // part=path\n if num > 1: # CHECK 1\n if parts[0] == \"path\": # CHECK 4\n \n # node=1.1 // part=to\n if num > 2: # CHECK 1\n if parts[1] == \"to\": # CHECK 4\n \n # node=1.1.1 // part=different\n if num > 3: # CHECK 1\n if parts[2] == \"different\": # CHECK 4\n \n # node=1.1.1.1 // part=__dynamic__:str\n if num == 4: # CHECK 1\n try:\n basket['__matches__'][3] = str(parts[3])\n except ValueError:\n pass\n else:\n if method in frozenset({'one', 'two'}):\n route_idx = 0\n elif method in frozenset({'BASE'}):\n route_idx = 1\n else:\n raise NoMethod\n # Return 1.1.1.1\n return router.dynamic_routes[('path', 'to', 'different', '<__dynamic__:str>')][route_idx], basket\n \n # node=1.1.2 // part=__dynamic__:int\n if num >= 3: # CHECK 1\n try:\n basket['__matches__'][2] = int(parts[2])\n except ValueError:\n pass\n else:\n if num == 3: # CHECK 5\n # Return 1.1.2\n return router.dynamic_routes[('path', 'to', '<__dynamic__:int>')][0], basket\n \n # node=1.1.3 // part=__dynamic__:str\n if num >= 3: # CHECK 1\n try:\n basket['__matches__'][2] = str(parts[2])\n except ValueError:\n pass\n else:\n if num == 3: # CHECK 5\n # Return 1.1.3\n return router.dynamic_routes[('path', 'to', '<__dynamic__:str>')][0], basket\n raise NotFound\n```\n\n## Special cases\n\nThe above example only shows routes that have a dynamic path segment in them (example: `<foo>`). But, there are other use cases that are covered differently:\n\n1. *fully static paths* - These are paths with no parameters (example: `/user/login`). These are basically matched against a key/value store.\n2. *regex paths* - If a route as a single regular expression match, then the whole route will be matched via regex. In general, this happens inline not too dissimilar than what we see in the above example.\n3. *special regex paths* - The router comes with a special `path` type (example: `<foo:path>`) that can match on an expanded delimiter. This is also true for any regex that uses the path delimiter in it. These cannot be matched in the normal course since they are of unknown length.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Core routing component for Sanic",
"version": "23.12.0",
"project_urls": {
"Homepage": "https://github.com/sanic-org/sanic-routing/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cfe33425c9a8773807ac2c01d6a56c8521733f09b627e5827e733c5cd36b9ac5",
"md5": "cbd8fc9ae85737849c674aa5814b3356",
"sha256": "1558a72afcb9046ed3134a5edae02fc1552cff08f0fff2e8d5de0877ea43ed73"
},
"downloads": -1,
"filename": "sanic_routing-23.12.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cbd8fc9ae85737849c674aa5814b3356",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 25522,
"upload_time": "2023-12-31T09:28:35",
"upload_time_iso_8601": "2023-12-31T09:28:35.233833Z",
"url": "https://files.pythonhosted.org/packages/cf/e3/3425c9a8773807ac2c01d6a56c8521733f09b627e5827e733c5cd36b9ac5/sanic_routing-23.12.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d15c2a7edd14fbccca3719a8d680951d4b25f986752c781c61ccf156a6d1ebff",
"md5": "e2ff8482fa88d053976af5b401ac32cc",
"sha256": "1dcadc62c443e48c852392dba03603f9862b6197fc4cba5bbefeb1ace0848b04"
},
"downloads": -1,
"filename": "sanic-routing-23.12.0.tar.gz",
"has_sig": false,
"md5_digest": "e2ff8482fa88d053976af5b401ac32cc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 29473,
"upload_time": "2023-12-31T09:28:36",
"upload_time_iso_8601": "2023-12-31T09:28:36.992186Z",
"url": "https://files.pythonhosted.org/packages/d1/5c/2a7edd14fbccca3719a8d680951d4b25f986752c781c61ccf156a6d1ebff/sanic-routing-23.12.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-31 09:28:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sanic-org",
"github_project": "sanic-routing",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "sanic-routing"
}