A RegEx based router
rerouter routes string commands to annotated functions.
How to define the 'grammar':
"Grammar" is the pattern that tells rerouter which handler a string command should be routed to.
For example, github slack client supports slash commands like:
subscribe user/repo +label:"teams/designers" +label:"urgent"
subscribe user/repo commits:myBranch author:"hotfix"
close [issue link]
open [pull request link]
Suppose we want to build a router that routes various command to different handlers, we can do:
router = RegExRouter()
@router.route("subscribe <user_repo> [<option(+label|commits|author)>:<value>]+")
def handle_subscribe(rns, *args, **kwargs):
"""Handle commands like:
subscribe user/repo +label:"teams/designers" +label:"urgent"
subscribe user/repo commits:myBranch author:"hotfix"
"""
pass
@router.route("(close|open) [link:<link_url>]+")
def handle_open_close(rns, *args, **kwargs):
"""Handle commands like:
close [issue link]
open [pull request link]
"""
pass
More examples:
@router.route("settings (set|get|delete) project:<jira_project>")
def f_settings(rns, *args, **kwargs):
"""Matches:
settings set project:TEST-PROJ
settings get project:TEST-PROJ
settings delete project:TEST-PROJ
"""
pass
@router.routex(
("(subscribe)", ""),
("(?P<feature>reviews|pushes|checks)", ""),
(
"(?P<filter_name>[+-]path|[+-]fork|[+-]branch|[+-]reviewer):(?P<filter_value>[^:]+)", # noqa
"+",
),
)
def f_subscribe(rns):
"""Matches:
subscribe reviews +path:foo/bar/* -fork:main/release +path:infra/tools/*
subscribe pushes path:foo/bar/* fork:main/release path:infra/tools/*
"""
pass
@router.route("a+ b")
@router.route("a* c* b")
def f_abc(rns, *args, **kwargs):
"""Matches:
aa
ab
aab
aaab
acb
aacb
cb
ccb
"""
pass
How to start routing:
This is done by calling the RegExRouter::route_to method, example:
router = RegExRouter()
@router.route("hello <user>")
def handle_subscribe(rns, *args, **kwargs):
return rns.named("user")
res = router.route_to("hello world") # res == 'world'
Behind the scene, rerouter translates the routing syntax into a list of regex patterns, aka:
| grammar: | (close|open) [link:<link_url>]+ |
| re patterns: | 1. (close|open); 2. (link):(^(?P<{link_url}>[^:]+)$) |
In the callback function, the rns is a RegExRouteMatch object which has the following properties:
conclusion: bool: whether the grammar match the command. (for annotation use cases, this is always true)matches: a list of RegEx match objects, for example, command:close https://example.comwill be routed tohandle_open_close(...)and thematcheswill be- <re.Match object, match='close'>
- <re.Match object, match='http://example.com''>
grammar: the grammar which the command matches, in our example, its value is(close|open) [link:<link_url>]+
Metadata
Release files for rerouter 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| rerouter-0.0.2.tar.gz | 13.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| rerouter-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.0 kB
Release files / rerouter-0.0.2.tar.gz
| Download URL | rerouter-0.0.2.tar.gz |
|---|---|
| Size | 13.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ae867eee6f7d8f7ec6e2d3fed8979f8492988936d5585ba673a32f7b6587c8e2
|
|
BLAKE2b-256 checksum How to use checksums |
dd617e88839a0bd9cd9418d0a994e0fbd2d55ca50f93d88121abb89f7ad5e590
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / rerouter-0.0.2-py3-none-any.whl
| Download URL | rerouter-0.0.2-py3-none-any.whl |
|---|---|
| Size | 10.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
95903bf2af7630340a5362b478b41886a9abdd112a7f4fe116091c49179a13f9
|
|
BLAKE2b-256 checksum How to use checksums |
9f4b19d4bcbc138e0add515728666dc3199e5256dd194598692a9893c38c66b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|