# HG changeset patch # User anatoly techtonik # Date 1405420423 -10800 # Node ID 6e313bdf6b690e419b15100dc067cc8a102703bd # Parent 3b632a25b1b3dd70524eeecdf013467e2624f65e routing: Add new roundup.web namespace with router component This branch is to untangle hardcoded Roundup URL scheme, make it more readable and customizable with extensions. Right now it doesn't seem possible to write extension that renders static HTML page at /about without modifying Roundup DB, and this web component should not depend on DB schema. diff -r 3b632a25b1b3 -r 6e313bdf6b69 roundup/web/NOTES.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/roundup/web/NOTES.md Tue Jul 15 13:33:43 2014 +0300 @@ -0,0 +1,48 @@ + +Engineering notes for Roundup components for web. + + +### Logical structure of web components + +An attempt to see what components are present in +web request processing. + + +-----------+ + | | + | Router | + └─----------+ (pure logic) + ---------------------------------------------- + +-----------+ + | | + | Login | + └-----------+ (logic + templates + messages) + ---------------------------------------------- + +-----------+ + ¦ ¦ + ¦ User DB ¦ + └-----------+ (messages) + + +Every component consists of messages (data), logic +(code) and representation (templates). Message +definition (data) also takes into account actions +that make component work. Templates are mostly +needed for human readability. + + +### Router + +Status for Roundup URL map check: + + [ ] urlmap component + + /_file/(.*) + StaticFileHandler + [ ] /_file + [ ] /_file/ + [ ] /_file/name + /@@file/(.*) + StaticFileHandler + [ ] /@@file + [ ] /@@file/ + [ ] /@@file/name diff -r 3b632a25b1b3 -r 6e313bdf6b69 roundup/web/router.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/roundup/web/router.py Tue Jul 15 13:33:43 2014 +0300 @@ -0,0 +1,32 @@ +#!/usr/bin/env python +""" +The purpose of router is to make Roundup URL scheme configurable +and allow extensions add their own handlers and URLs to tracker. + +Public domain work by: + anatoly techtonik +""" + +import re + + +class Router(object): + + def __init__(self, urlmap=[]): + """ + `urlmap` is a list (pattern, handler, pattern, ...) + """ + self.urlmap = urlmap + + def get_handler(self, urlpath): + """ + `urlpath` is a part of url /that/looks?like=this + + returns tuple (handler, arguments) or (None, ()) + """ + for i in range(0, len(self.urlmap), 2): + pattern, handler = self.urlmap[i], self.urlmap[i+1] + match = re.match(pattern, urlpath) + if match: + return handler, match.groups() + return (None, ())