view uploader/dispatcher.py @ 0:827f7577f940

initial commit of file upload widget
author k0s <k0scist@gmail.com>
date Sat, 21 Nov 2009 15:29:03 -0500
parents
children a02c4fcd7001
line wrap: on
line source

"""
request dispatcher
"""

import os
from handlers import Get, Post
from webob import Request, exc

class Dispatcher(object):

    ### class level variables
    defaults = { 'directory': None}

    def __init__(self, **kw):
        for key in self.defaults:
            setattr(self, key, kw.get(key, self.defaults[key]))
        self.handlers = [ Get, Post ]
        assert os.path.exists(self.directory)

    ### methods dealing with HTTP
    def __call__(self, environ, start_response):
        request = Request(environ)
        for h in self.handlers:
            if h.match(request):
                handler = h(self, request)
                break
        else:
            handler = exc.HTTPNotFound
        res = handler()
        return res(environ, start_response)