| Development Status :: 4 - Beta |
| Intended Audience :: Developers |
| License :: OSI Approved :: GNU General Public License (GPL) |
| Operating System :: OS Independent |
| Programming Language :: Python |
| Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware |
The WSGI Dispatcher middleware does three things:
environ['wsgiorg.routing_args'] and invokes a WSGI application from a list
of predefined directories if there is a match or a returns an HTTP 404.
environ['PATH_INFO'] for one or more predefined URLs and serves static content
from corresponding directories.
environ['PATH_INFO'] for one or more predefined URLs and invokes WSGI
applications (which may well be other dispatchers).
The dispatcher is intented to work behind WSGI URL parsers like
Routes which parse request URLs and place named or positional
arguments in environ['wsgiorg.routing_args'].
WSGIDispatcher-0.3.tar.gz (Development Status :: 4 - Beta)
WSGIDispatcher-0.2.tar.gz (Development Status :: 4 - Beta)
WSGIDispatcher-0.1.tar.gz (Development Status :: 3 - Alpha)
The WSGI Dispatcher constructor accepts the following arguments:
controller_dirs A list of directories containing WSGI applications (or controllers
in an MVC context).
match_named (default: True) environ['wsgiorg.routing_args']
is a two-tuple of (positional_args, named_args).
If match_named is set to True, the dispatcher uses named_args
to dispatch, if set to False it uses positional_args.
with_action (default: False) If set to True the dispatcher will
try to locate the WSGI application and invoke one of its methods (also referred to as "action").
For example, using Routes, one may specify
m.connect('home', '', controller='blog', action='index'), in which case the dispatcher will
try to instantiate the "blog" class (the name may differ according to transformation rules which will be
discussed later) and call its "index" method like a WSGI application, passing it environ and
start_response.
not_found_app (default: None) Pass a WSGI application to be called when no match
is found. By default the dispatcher returns an HTTP 404.
Furthermore, the functionality of the dispatcher can be customised setting the following WSGI Dispatcher properties:
fname_pattern (default: None) Use the
fname_pattern to find WSGI application/controllers in controller_dirs. For example,
for d.fname_pattern = "Taste%(filename)sPotatoes" the dispatcher will match:
"Taste<controller name in environ[wsgiorg.routingargs']>Potatoes.py"; assuming the controller
name was "index", the dispatcher would load the filename: "TasteindexPotatoes.py".
fname_transform_func (default: None) Use the
fname_transform_func to transform the controller filename before trying
to find WSGI application/controllers in controller_dirs. For example, for
d.fname_transform_func = lambda x: x[0].upper() + x[1:], for a controller filename "index", and
the previous fname_pattern value, the dispatcher would load the filename: "TasteIndexPotatoes.py".
classname_pattern (default: None) Use the
classname_pattern to find the classname of the WSGI
application/controller. For example, for d.classname_pattern =
"Tasty%(classname)sPotato" the dispather will match:
"Tasty<controller name in
environ[wsgiorg.routingargs']>Potato"; assuming the
controller name was "index", the dispatcher will use the class "TastyindexPotato."
classname_transform_func (default: None) Same
functionality as fname_transform_func but for class names.
action_pattern (default: None) Same
functionality as classname_pattern; use
%(action)s for the action name placeholder.
action_transform_func (default: None) Same
functionality as fname_transform_func and
classname_transform_func but for action names.
Furthermore, the dispatcher uses Paste's URLMap to serve static content and/or to call other WSGI applications based on specific URLs.
add_static_dir(url, dir_path) Serves static resources from directory dir_path when
url is matched in environ['PATH_INFO'].
add_app_dispatcher(url, dir_path) Invokes a WSGI application from dir_path when
url is matched in environ['PATH_INFO'].
For more information on the syntax of URLs (url) and the matching behaviour
please refer to URLMap documentation.
from paste.pony import PonyMiddleware
from routes.middleware import Mapper, RoutesMiddleware
# Mapper needed for Routes.
from config.routing import mapper
from wsgi_dispatcher import WSGIDispatcher
# Define the routes using Routes here.
mapper = Mapper()
mapper.connect('/webapp', controller="webapp")
mapper.create_regs('webapp')
dispatcher = WSGIDispatcher(controller_dirs = ["controllers"])
dispatcher.classname_transform_func=lambda x: x[0].upper() + x[1:]
dispatcher.add_static_dir('/test/', 'public/static')
app = RoutesMiddleware(dispatcher, mapper)