milla.dispatch.routing¶
URL router
Created: | Mar 13, 2011 |
---|---|
Author: | dustin |
Updated: | $Date$ |
Updater: | $Author$ |
-
class
milla.dispatch.routing.
Router
(trailing_slash=<class 'milla.dispatch.routing.REDIRECT'>)[source]¶ A dispatcher that maps arbitrary paths to controller callables
Typical usage:
router = Router() router.add_route('/foo/{bar}/{baz:\d+}', some_func) app = milla.Application(dispatcher=router)
In many cases, paths with trailing slashes need special handling. The
Router
has two ways of dealing with requests that should have a trailing slash but do not. The default is to send the client an HTTP 301 Moved Permanently response, and the other is to simply treat the request as if it had the necessary trailing slash. A third option is to disable special handling entirely and simply return HTTP 404 Not Found for requests with missing trailing slashes. To change the behavior, pass a different value to the constructor’strailing_slash
keyword.Redirect the client to the proper path (the default):
router = Router(trailing_slash=Router.REDIRECT) router.add_route('/my_collection/', some_func)
Pretend the request had a trailing slash, even if it didn’t:
router = Router(trailing_slash=Router.SILENT) router.add_route('/my_collection/', some_func)
Do nothing, let the client get a 404 error:
router = Router(trailing_slash=None) router.add_route('/my_collection/', some_func)
-
add_route
(template, controller, **vars)[source]¶ Add a route to the routing table
Parameters: - template – Route template string
- controller – Controller callable or string Python path
Route template strings are path segments, beginning with
/
. Paths can also contain variable segments, delimited with curly braces.Example:
/some/other/{variable}/{path}
By default, variable segments will match any character except a
/
. Alternate expressions can be passed by specifying them alongside the name, separated by a:
.Example:
/some/other/{alternate:[a-zA-Z]}
Variable path segments will be passed as keywords to the controller. In the first example above, assuming
controller
is the name of the callable passed, and the request path was/some/other/great/place
:controller(request, variable='great', path='place')
The
controller
argument itself can be any callable that accepts a WebOb request as its first argument, and any keywords that may be passed from variable segments. It can also be a string Python path to such a callable. For example:`some.module:function`
This string will resolve to the function
function
in the modulesome.module
.
-
resolve
(path_info)[source]¶ Find a controller for a given path
Parameters: path_info – Path for which to locate a controller Returns: A functools.partial
instance that sets the values collected from variable segments as keyword arguments to the callableThis method walks through the routing table created with calls to
add_route()
and finds the first whose template matches the given path. Variable segments are added as keywords to the controller function.
-
template_re
= <_sre.SRE_Pattern object>¶ Compiled regular expression for variable segments
-