urlmap

Map urls to Python objects


License
MIT
Install
pip install urlmap==0.1

Documentation

# urlmap

Yet another library to map URLs to Python objects and get parameters.

This is designed to be as lightweight as possible. Please fork, but merge requests that add new "features" are likely to be rejected.

## Usage

```python
import webob, webob.exc
import urlmap

@urlmap.map("/")
def index(request):
	return webob.Response(body="index")

@urlmap.map("/test")
def test(request):
	return webob.Response(body="test")

@urlmap.map("/^[1-9]+$")
def number(request, n):
	return webob.Response(body="I like the number %d..." % int(n))

@urlmap.map("/0")
def zero(request):
	return webob.Response(body="I hate zero.")


def serve_request(environ, start_response):
	request = webob.Request(environ)

	try:
		(controller, parameters) = urlmap.retrieve(request.path_info)
		response = controller(request, *parameters)
	except urlmap.RouteNotFoundException:
		response = webob.exc.HTTPNotFound()

	return response(environ, start_response)

if __name__ == "__main__":
	import wsgiref.simple_server

	httpd = wsgiref.simple_server.make_server("localhost", 8080, serve_request)

	try:
		httpd.serve_forever()
	except KeyboardInterrupt:
		print "^C"
```