BitWorking

Python Routes

Routes is a Python re-implementation of the Rails routes system that allows for a two-way mapping between URLs and a dictionary. That is, you can pass it a URL and it will make sure it conforms to your specifications and it will then pull out the relavent bits as specified. Just as importantly it allows you to pass info back in and it will generate the appropriate URL.


>>> from routes import Mapper
>>> m = Mapper()
>>> m.prefix = 'http://bitworking.org/some.cgi'
>>> m.connect(':(collection)/:(id)')
>>> m.create_regs([])

>>> m.match("http://bitworking.org/some.cgi/trash")
{'action': 'index', 'controller': 'content', 'id': None, 
  'collection': 'trash'}
>>> m.match("http://bitworking.org/some.cgi/trash/QWERT")
{'action': 'index', 'controller': 'content', 'id': 'QWERT', 
  'collection': 'trash'}
>>> m.match("http://bitworking.org/some.cgi/livestore/QWERT/junk")
None

>>> m.generate(collection='livestore', id="Thisisatest")
http://bitworking.org/some.cgi/livestore/Thisisatest

Seems like a pretty nice library with lots of unit tests. The one downside is the .egg distribution, which has never worked for me. It was simpler to just pull the latest out of the subversion repository and copy it into site-packages.

Routes, dispatch and Kid together would make a nice un-framework.

2006-01-30