changeset 14:7f426a77e192

mv dispatcher to wsgi
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 28 Feb 2012 14:45:50 -0800
parents fc44434040e5
children cfc141c154a8
files simpypi/dispatcher.py simpypi/handlers.py simpypi/wsgi.py
diffstat 3 files changed, 84 insertions(+), 174 deletions(-) [+]
line wrap: on
line diff
--- a/simpypi/dispatcher.py	Tue Feb 28 14:40:51 2012 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,84 +0,0 @@
-"""
-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 Dispatcher(object):
-
-    ### class level variables
-    defaults = {'auto_reload': 'False',
-                'template_dirs': '',
-                }
-
-    def __init__(self, directory, index=None):
-
-        self.directory = directory
-        assert os.path.exists(directory)
-
-        # set instance parameters from kw and defaults
-# XXX unneeded for now
-#        for key in self.defaults:
-#            setattr(self, key, kw.get(key, self.defaults[key]))
-#        self.auto_reload = self.auto_reload.lower() == 'true'
-
-        # request handlers
-        self.handlers = dict([(method, getattr(self, method))
-                              for method in ('GET', 'POST')])
-# XXX unneeded for now
-#        self.handlers = [ Index ]
-
-        # 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()
-
-        # template directories
-#        self.template_dirs = self.template_dirs.split()
-#        self.template_dirs = [os.path.join(here, 'templates')]
-
-    def __call__(self, environ, start_response):
-
-        # get a request object
-        request = Request(environ)
-
-        # get the path
- #       path = request.path_info.strip('/').split('/')
- #       if path == ['']:
- #           path = []
- #       request.environ['path'] = path
-
-        # match the request to a handler
-        handler = self.handlers.get(request.method)
-        if handler:
-            res = handler(request)
-        else:
-            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()
-        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))
--- a/simpypi/handlers.py	Tue Feb 28 14:40:51 2012 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,90 +0,0 @@
-"""
-request handlers:
-these are instantiated for every request, then called
-"""
-
-import os
-from pkg_resources import resource_filename
-from urlparse import urlparse
-from webob import Response, exc
-from tempita import HTMLTemplate
-
-class HandlerMatchException(Exception):
-    """the handler doesn't match the request"""
-
-class Handler(object):
-
-    methods = set(['GET']) # methods to listen to
-    handler_path = [] # path elements to match
-
-    @classmethod
-    def match(cls, app, request):
-
-        # check the method
-        if request.method not in cls.methods:
-            return None
-
-        # check the path
-        if request.environ['path'][:len(cls.handler_path)] != cls.handler_path:
-            return None
-
-        try:
-            return cls(app, request)
-        except HandlerMatchException:
-            return None
-
-    def __init__(self, app, request):
-        self.app = app
-        self.request = request
-        self.application_path = urlparse(request.application_url)[2]
-
-    def link(self, path=(), permanant=False):
-        if isinstance(path, basestring):
-            path = [ path ]
-        path = [ i.strip('/') for i in path ]
-        if permanant:
-            application_url = [ self.request.application_url ]
-        else:
-            application_url = [ self.application_path ]
-        path = application_url + path
-        return '/'.join(path)
-
-    def redirect(self, location):
-        raise exc.HTTPSeeOther(location=location)
-
-class TempitaHandler(Handler):
-
-    template_dirs = [ resource_filename(__name__, 'templates') ]
-
-    def __init__(self, app, request):
-        Handler.__init__(self, app, request)
-        self.data = { 'request': request,
-                      'link': self.link }
-
-    def __call__(self):
-        return getattr(self, self.request.method.title())()
-
-    def find_template(self, template):
-        for d in self.template_dirs:
-            path = os.path.join(d, template)
-            if os.path.exists(path):
-                return HTMLTemplate.from_filename(path)
-
-    def Get(self):
-        # needs to have self.template set
-        template = self.find_template(self.template)
-        return Response(content_type='text/html',
-                        body=template.substitute(**self.data))
-
-class Index(TempitaHandler):
-    template = 'index.html'
-    methods=set(['GET', 'POST'])
-
-    def Post(self):
-        """handle posting a package"""
-
-        # get the package
-        package = self.request.POST.get('package')
-
-        # redirect to the main page
-        self.redirect(self.link(self.handler_path))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simpypi/wsgi.py	Tue Feb 28 14:45:50 2012 -0800
@@ -0,0 +1,84 @@
+"""
+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 Dispatcher(object):
+
+    ### class level variables
+    defaults = {'auto_reload': 'False',
+                'template_dirs': '',
+                }
+
+    def __init__(self, directory, index=None):
+
+        self.directory = directory
+        assert os.path.exists(directory)
+
+        # set instance parameters from kw and defaults
+# XXX unneeded for now
+#        for key in self.defaults:
+#            setattr(self, key, kw.get(key, self.defaults[key]))
+#        self.auto_reload = self.auto_reload.lower() == 'true'
+
+        # request handlers
+        self.handlers = dict([(method, getattr(self, method))
+                              for method in ('GET', 'POST')])
+# XXX unneeded for now
+#        self.handlers = [ Index ]
+
+        # 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()
+
+        # template directories
+#        self.template_dirs = self.template_dirs.split()
+#        self.template_dirs = [os.path.join(here, 'templates')]
+
+    def __call__(self, environ, start_response):
+
+        # get a request object
+        request = Request(environ)
+
+        # get the path
+ #       path = request.path_info.strip('/').split('/')
+ #       if path == ['']:
+ #           path = []
+ #       request.environ['path'] = path
+
+        # match the request to a handler
+        handler = self.handlers.get(request.method)
+        if handler:
+            res = handler(request)
+        else:
+            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()
+        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))