Mercurial > p > roundup > code
view roundup/web/router.py @ 4906:b860ede03056 routing
routing: Add example URL map
| author | anatoly techtonik <techtonik@gmail.com> |
|---|---|
| date | Tue, 15 Jul 2014 13:42:57 +0300 |
| parents | 6e313bdf6b69 |
| children | c37069a99cec |
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 # --- Example URL mapping class NamedObject(object): """Object that outputs given name when printed""" def __init__(self, name): self.name = name def __repr__(self): return self.name ExampleHandler = NamedObject('ExampleHandler') ExampleFileHandler = NamedObject('ExampleFileHandler') EXAMPLE_URLMAP = ( '/static/(.*)', ExampleFileHandler, '/', ExampleHandler ) # --- Regexp based router 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, ())
