Skip to content

Instantly share code, notes, and snippets.

@imxiaobo
Forked from nod/tornado_route_decorator.py
Created October 8, 2010 11:48
Show Gist options
  • Save imxiaobo/616666 to your computer and use it in GitHub Desktop.
Save imxiaobo/616666 to your computer and use it in GitHub Desktop.
class route(object):
"""
decorates RequestHandlers and builds up a list of routables handlers
Tech Notes (or "What the *@# is really happening here?")
--------------------------------------------------------
Everytime @route('...') is called, we instantiate a new route object which
saves off the passed in URI. Then, since it's a decorator, the function is
passed to the route.__call__ method as an argument. We save a reference to
that handler with our uri in our class level routes list then return that
class to be instantiated as normal.
Later, we can call the classmethod route.get_routes to return that list of
tuples which can be handed directly to the tornado.web.Application
instantiation.
Example
-------
@route('/some/path')
class SomeRequestHandler(RequestHandler):
pass
my_routes = route.get_routes()
"""
_routes = []
def __init__(self, uri):
self._uri = uri
def __call__(self, _handler):
"""gets called when we class decorate"""
self._routes.append((self._uri, _handler))
return _handler
@classmethod
def get_routes(self):
return self._routes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment