Skip to main content

decoroute

Decorator style and pattern-matching based url routing library. No framework! Very compact. Core logic less 100 lines of code. WSGI compliant. No additional API required.

Complete example

import decoroute

app = decoroute.App(prefix='')

# pure WSGI
@app.expose('/wsgi/<id:\d+>')
@decoroute.wsgi('wsgiorg.routing_args')
def wsgi_app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return environ['wsgiorg.routing_args']

# pass variables into **kw
def render_response(status = '200 OK', content_type = 'text/plain', add_headers = [], **context):
    return status, [('Content-Type', content_type)] + add_headers, context

@app.expose('/node', id = '1')
@app.expose('/node/<id:\d+>')
def node(env, id):
    return render_response(id = id)

@app.expose('/url_for')
def url_for(env):
    return render_response(url = app.url_for(node, id = 666))

@app.expose('/404')
def not_found(env):
    raise decoroute.NotFound()

@app.not_found
def not_found_handler(env):
    return render_response(status = '404 NF', **env)

@app.render
def render(env, (status, headers, context)):
    context['site_uri'] = '%s://%s' % (env['wsgi.url_scheme'], env['HTTP_HOST'])
    return status, headers, ['%s=%s\n' %(k, context[k]) for k in context.keys()] # fake templating

from wsgiref.simple_server import make_server

make_server('', 6666, app).serve_forever()

Tips

If you place your views (Django term. in ror - controller) in another modules, organize code like werkzeug.

# utils.py

import decoroute
app = decoroute.App()
# view.py and anoter

from utils import *

@app.render
def render(env, ...)
    # returns of your endpoints pass to render handler
    # in render use your prefered template engine and return triple
    # status, generator of response headers, generator of response body
    # ( generator / iterator / list - any )
    # if you use default render endpoints must be return triple

@app.not_found
    # no route enpoint
    # it also pass to render handler

@app.expose('/node', id = '1')
@app.expose('/node/<id:\d+>')
def node(env, id):
    # endpoint passed some variables
    # env is a wsgi environ
    # id = '1' default value of parameter
    #   all parameters must be <type 'str'>
    # <id:\d+> parameter pattern;
    #   \d+ specify regexp constraint of parameter (required)
# manage.py - main module

from utils import app
import view # and all other

if __name__ == '__main__':
    # serve app here

Metadata

Release files for decoroute 0.8.1

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

Source distribution (sdist)

Source distribution for decoroute 0.8.1
File Size Uploaded
decoroute-0.8.1.tar.gz 5.9 kB Details

Release files / decoroute-0.8.1.tar.gz

Download URL decoroute-0.8.1.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
54f3f9aef95ce17ad97dc745e6d8445402490bb0906d117a22e26260026bd649
BLAKE2b-256 checksum
How to use checksums
9b15a2efe5cc43a6d1f74e1e320acaee1d1dc7e7e24a1b0292d366171fc0579b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.8.1 This release

1 release file

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