view simpypi/wsgi.py @ 37:1bdece293671

* fix calling signature in test * no need for a self here
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 01 Mar 2012 09:30:41 -0800
parents a02d08627d9c
children ee29001674af
line wrap: on
line source

"""
request dispatcher:
data persisting across requests should go here
"""

import os
#from handlers import Index
from webob import Request, Response, exc

here = os.path.dirname(os.path.abspath(__file__))

class SimPyPI(object):

    def __init__(self, directory, index=None):

        self.directory = directory
        assert os.path.exists(directory)

        # request handlers
        self.handlers = dict([(method, getattr(self, method))
                              for method in ('GET', 'POST')])
        # TODO: HEAD, OPTIONS, maybe more

        # cache index HTML
        self.index = index or os.path.join(here, 'templates', 'index.html')
        assert os.path.exists(self.index)
        self.index = file(self.index).read()

    def __call__(self, environ, start_response):

        # get a request object
        request = Request(environ)

        # match the request to a handler
        handler = self.handlers.get(request.method)
        if handler:
            res = handler(request)
        else:
            res = exc.HTTPNotFound()

        return res(environ, start_response)

    def GET(self, request):
        return Response(body=self.index, content_type='text/html')

    def POST(self, request):
        """handle posting a package"""

        # get the package
        package = request.POST.get('package')

        # sanity check:
        # - does the field exist?
        # - is it a file?
        # TODO

        # put the package in the right place

        # redirect to the main page
        return exc.HTTPSeeOther(add_slash=True)