# HG changeset patch # User Jeff Hammel # Date 1330467408 28800 # Node ID 3e8597489ea32d62d19b0d171e8c0a50e08977ed # Parent 9879140a2026ed4ee74cef91a0e3b4137d68e748 start moving this to a single file diff -r 9879140a2026 -r 3e8597489ea3 simpypi/dispatcher.py --- a/simpypi/dispatcher.py Tue Feb 28 13:54:21 2012 -0800 +++ b/simpypi/dispatcher.py Tue Feb 28 14:16:48 2012 -0800 @@ -4,7 +4,7 @@ """ import os -from handlers import Index +#from handlers import Index from webob import Request, Response, exc here = os.path.dirname(os.path.abspath(__file__)) @@ -14,6 +14,7 @@ ### class level variables defaults = {'auto_reload': 'False', 'template_dirs': '', + 'index': os.path.join(here, 'templates', 'index.html') } def __init__(self, directory, **kw): @@ -28,11 +29,18 @@ # self.auto_reload = self.auto_reload.lower() == 'true' # request handlers - self.handlers = [ Index ] + self.handlers = dict([(method, getattr(self, method)) + for i in ('GET', 'POST')]) +# XXX unneeded for now +# self.handlers = [ Index ] + + # cache index HTML + assert os.path.exists(self.index) + self.index = file(self.index).read() # template directories # self.template_dirs = self.template_dirs.split() - self.template_dirs = [os.path.join(here, 'templates')] +# self.template_dirs = [os.path.join(here, 'templates')] def __call__(self, environ, start_response): @@ -40,19 +48,37 @@ request = Request(environ) # get the path - path = request.path_info.strip('/').split('/') - if path == ['']: - path = [] - request.environ['path'] = path + # path = request.path_info.strip('/').split('/') + # if path == ['']: + # path = [] + # request.environ['path'] = path # match the request to a handler - for h in self.handlers: - handler = h.match(self, request) - if handler is not None: - break + handler = self.handlers.get(request.method) + if handler: + res = handler(request) else: - handler = exc.HTTPNotFound + res = exc.HTTPNotFound() +# for h in self.handlers: +# handler = h.match(self, request) +# if handler is not None: +# break +# else: +# handler = exc.HTTPNotFound # get response - res = handler() +# res = handler() 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 = self.request.POST.get('package') + + # redirect to the main page + # TODO + # self.redirect(self.link(self.handler_path))