Skip to main content

WebDispatch

https://travis-ci.org/aodag/WebDispatch.svg?branch=master https://coveralls.io/repos/aodag/WebDispatch/badge.png?branch=master Wheel Status

WebDispatch is dispatcher collection for wsgi application. That has no dependency to exsiting frameworks, but works fine with WebOb.

Dispatch and Generate URL

dispatch with url patterns.

example helo application:

>>> from webob.dec import wsgify
>>> @wsgify
... def greeting(request):
...     return "Hello, %s" % request.urlvars['name']

create and configure URL Dispatcher:

>>> from webdispatch import URLDispatcher
>>> dispatcher = URLDispatcher()
>>> dispatcher.add_url('top', '/hello/{name}', greeting)

invoke dispatcher as WSGI Application:

>>> from webob import Request
>>> req = Request.blank('/hello/webdispatch')
>>> res = req.get_response(dispatcher)
>>> res.body
b'Hello, webdispatch'

Wildcard

You can use wildcard after last slash. Pattern with wildcard consumes paths before the wildcard, (and make that new script_name,) and remained paths becomes new path_info.

>>> @wsgify
... def with_pathinfo(request):
...     return "Hello, %s" % request.path_info
>>> dispatcher.add_url('top', '/with_pathinfo/*', with_pathinfo)
>>> req = Request.blank('/with_pathinfo/this/is/pathinfo')
>>> res = req.get_response(dispatcher)
>>> res.body
b'Hello, this/is/pathinfo'

Type Converter

You can specify converter with varname below “:”.

>>> @wsgify
... def add(request):
...     result = request.urlvars['v1'] + request.urlvars['v2']
...     return "result, %d" % result
>>> dispatcher.add_url('add', '/add/{v1:int}/{v2:int}', add)
>>> req = Request.blank('/add/1/2')
>>> res = req.get_response(dispatcher)
>>> res.body
b'result, 3'

default converters are defined as bellow:

DEFAULT_CONVERTERS = {
    'int': int,
    'date': lambda s: datetime.strptime(s, '%Y-%m-%d'),
    'date_ym': lambda s: datetime.strptime(s, '%Y-%m'),
}

Action Dispatch

ActionDispatcher invokes object method with action name from urlvars.

action handler class:

>>> class MyHandler(object):
...     @wsgify
...     def greeting(self, request):
...         return "Hello"

create and configure ActionDispatcher:

>>> from webdispatch import ActionDispatcher
>>> actiondispatcher = ActionDispatcher()
>>> actiondispatcher.register_actionhandler(MyHandler)

add action url with urlvars named action:

>>> dispatcher.add_url('action_dispatch', '/actions/{action}', actiondispatcher)

invoke wsgi appclication.:

>>> req = Request.blank('/actions/greeting')
>>> res = req.get_response(dispatcher)
>>> res.body
b'Hello'

Method Dispatch

dispatch by HTTP METHOD restfully.

use register_app decorator:

>>> from webdispatch import MethodDispatcher
>>> restapp = MethodDispatcher()
>>> @restapp.register('get')
... @wsgify
... def get_hello(request):
...    return "Get Hello"
>>> @restapp.register('post')
... @wsgify
... def post_hello(request):
...    return "Post Hello"

or use registe_app method:

>>> from webdispatch import MethodDispatcher
>>> restapp = MethodDispatcher()
>>> restapp.register_app('get', get_hello)
>>> restapp.register_app('post', post_hello)

Each applications are registered with HTTP Method name.

invoke WSGI application:

>>> req = Request.blank('/')
>>> res = req.get_response(restapp)
>>> res.body
b'Get Hello'

extra_environ

DispatchBase accepts extra_environ argument. Dispatcher adds that argument to wsgi environ by request.

Changes

1.3

  • added decrator API

1.2

  • added extra_environ argument to constructer

  • drop python2.6 tests

  • drop dependency for python2.6

  • added type converter for url vars

1.1

  • added extra_environ method to DispatchBase class

  • added support for Python 3.4

1.0.1

  • include char of “-” to urlmatch words #9

1.0

  • no changes

1.0b4

  • fix response body to bytes

1.0b3

  • fix some bugs

1.0b2

  • fix setup bug

Release files for WebDispatch 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for WebDispatch 1.3
File Size Uploaded
WebDispatch-1.3.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for WebDispatch 1.3
File Interpreter ABI Platform
WebDispatch-1.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 28.2 kB

Release files / WebDispatch-1.3.tar.gz

Download URL WebDispatch-1.3.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f5a0971007412208fd5a673343302f97e7ed0fed32eb8440c2cc5b4b279a8c39
BLAKE2b-256 checksum
How to use checksums
b98a9d38473ac330d5242ee967d72157e01ceda79b291eaec41daa2c8bf8512a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / WebDispatch-1.3-py2.py3-none-any.whl

Download URL WebDispatch-1.3-py2.py3-none-any.whl
Size 17.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
9e49063871935d2ebfd2e025b5071ae8a6147c77057b40db1dfc81becab8a08d
BLAKE2b-256 checksum
How to use checksums
9c2d2ee683115383c3ba3e6e063a43b0953a1bb12766e535926f0cfbcb38a3fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page