Python – cherrypy: accessing the URI/route parameters inside a tool hook


I have a tool hook setup for 'before_finalize' like so:

def before_finalize():
    d = cherrypy.response.body
    location = '%s/views' %['/']['application_path']
    cherrypy.response.body = Template(file='%s/index.tmpl' % location).respond()

What I need to do is find out inside that hook what route (I'm using RoutesDispatcher) got us to that hook, or what the URI is, so I can appropriately find my template based on that. How can I find this information?

Best Solution

cherrypy.url will get you the complete URI, but I suspect that's not quite what you're looking for...why do you need it? If you're trying to form your 'location' variable based on the URI, you probably want path_info instead of the complete URI:

location = '%s/views' %['/']['application_path']
if request.path_info.endswith('/'):
    fname = '%s%sindex.html' % (location, request.path_info)
    fname = '%s%s.html' % (location, request.path_info)