changeset 48:1b9573832f33

clean up the dispatcher code
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 05 Jan 2011 18:26:07 -0800
parents e8d5782f6678
children 363f6625d036
files makeitso/dispatcher.py
diffstat 1 files changed, 19 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/makeitso/dispatcher.py	Wed Jan 05 18:18:26 2011 -0800
+++ b/makeitso/dispatcher.py	Wed Jan 05 18:26:07 2011 -0800
@@ -1,29 +1,20 @@
 """
-request dispatcher:
-data persisting across requests should go here
+request dispatcher for MakeItSo!
 """
 
+import sys
 from handlers import Index
 from webob import Request, Response, exc
 
 class Dispatcher(object):
 
     ### class level variables
-    defaults = { 'app': None, }
 
     def __init__(self, **kw):
 
-        # set instance parameters from kw and defaults
-        for key in self.defaults:
-            setattr(self, key, kw.get(key, self.defaults[key]))
-
         # request handlers
         self.handlers = [ Index ]
 
-        # endpoint app if used as middleware
-        if self.app:
-            assert hasattr(self.app, '__call__')
-
     def __call__(self, environ, start_response):
 
         # get a request object
@@ -41,10 +32,25 @@
             if handler is not None:
                 break
         else:
-            if self.app:
-                return self.app(environ, start_response)
             handler = exc.HTTPNotFound
 
         # get response
         res = handler()
         return res(environ, start_response)
+
+if __name__ == '__main__':
+    from optparse import OptionParser
+    from wsgiref import simple_server
+
+    # parse command line options
+    parser = OptionParser()
+    parser.add_option('-p', '--port', dest='port', default=8080, type='int',
+                      help='port to serve on')
+    options, args = parser.parse_args()
+
+    # create an app
+    app = Dispatcher()
+
+    # serve it!
+    server = simple_server.make_server(host=host, port=options.port, app=app)
+    server.serve_forever()