from os import path, chdir
import sys

import views

reserved_locations = {
    'browse' : views.BrowseView,
    'feed.rss' : views.RSSEntryView,
    }    

def handler(req):
    # Make sure the current dir is in PYTHONPATH
    curdir = path.dirname(path.realpath(__file__))
    chdir(curdir)
    sys.path = [curdir+'/'] + sys.path
    sys.path = [curdir+'/templates/'] + sys.path

    # Find a list of the traversal to the wanted location
    uri_bits = req.uri.split('/')
    pathinfo = uri_bits[(uri_bits.index('blog')+1):]

    page = pathinfo[0]
    extrapath = []
    if len(pathinfo)>1:
        extrapath = pathinfo[1:]

    view = None
    if page in reserved_locations:
        view = reserved_locations[page](req, extrapath)
    else:
        view = views.EntryView(req, extrapath, page)

    return view()
