view roundup/web/router.py @ 4905:6e313bdf6b69 routing

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.
author anatoly techtonik <techtonik@gmail.com>
date Tue, 15 Jul 2014 13:33:43 +0300
parents
children b860ede03056
line wrap: on
line source

#!/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 <techtonik@gmail.com>
"""

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, ())

Roundup Issue Tracker: http://roundup-tracker.org/