changeset 10:3e8597489ea3

start moving this to a single file
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 28 Feb 2012 14:16:48 -0800
parents 9879140a2026
children 2bfcba075db0
files simpypi/dispatcher.py
diffstat 1 files changed, 39 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- 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))