[Pypi-checkins] r1020 - trunk/pypi

richard python-checkins at python.org
Wed Mar 14 05:49:42 CET 2012


Author: richard
Date: Wed Mar 14 05:49:42 2012
New Revision: 1020

Added:
   trunk/pypi/wsgi_app.py
Modified:
   trunk/pypi/pypi.wsgi
Log:
refactor to allow multiple wsgi application configs more easily

Modified: trunk/pypi/pypi.wsgi
==============================================================================
--- trunk/pypi/pypi.wsgi	(original)
+++ trunk/pypi/pypi.wsgi	Wed Mar 14 05:49:42 2012
@@ -1,64 +1,12 @@
 #!/usr/bin/python
-import sys,os
+import sys, os
 prefix = os.path.dirname(__file__)
 sys.path.insert(0, prefix)
-import cStringIO, webui, store, config
 
-store.keep_conn = True
-
-CONFIG_FILE = os.path.join(prefix, 'config.ini')
-
-class Request:
-
-    def __init__(self, environ, start_response):
-        self.start_response = start_response
-        try:
-            length = int(environ['CONTENT_LENGTH'])
-        except ValueError:
-            length = 0
-        self.rfile = cStringIO.StringIO(environ['wsgi.input'].read(length))
-        self.wfile = cStringIO.StringIO()
-        self.config = config.Config(CONFIG_FILE )
-
-    def send_response(self, code, message='no details available'):
-        self.status = '%s %s' % (code, message)
-        self.headers = []
-
-    def send_header(self, keyword, value):
-        self.headers.append((keyword, value))
-
-    def set_content_type(self, content_type):
-        self.send_header('Content-Type', content_type)
-
-    def end_headers(self):
-        self.start_response(self.status, self.headers)
-
-def debug(environ, start_response):
-    if environ['PATH_INFO'].startswith("/auth") and \
-           "HTTP_AUTHORIZATION" not in environ:
-        start_response("401 login",
-                       [('WWW-Authenticate', 'Basic realm="foo"')])
-        return
-    start_response("200 ok", [('Content-type', 'text/plain')])
-    environ = environ.items()
-    environ.sort()
-    for k,v in environ:
-        yield "%s=%s\n" % (k, v)
-    return
-
-
-def application(environ, start_response):
-    if "HTTP_AUTHORIZATION" in environ:
-        environ["HTTP_CGI_AUTHORIZATION"] = environ["HTTP_AUTHORIZATION"]
-    r = Request(environ, start_response)
-    webui.WebUI(r, environ).run()
-    return [r.wfile.getvalue()]
-#application=debug
+import wsgi_app
+config_path = os.path.join(prefix, 'config.ini')
+application = wsgi_app.Application(config_path, debug=False)
 
 if __name__ == '__main__':
-    # very simple wsgi server so we can play locally
-    from wsgiref.simple_server import make_server
-    httpd = make_server('', 8000, application)
-    print "Serving on port 8000..."
-    httpd.serve_forever()
+    application.test(8000)
 

Added: trunk/pypi/wsgi_app.py
==============================================================================
--- (empty file)
+++ trunk/pypi/wsgi_app.py	Wed Mar 14 05:49:42 2012
@@ -0,0 +1,65 @@
+#!/usr/bin/python
+import sys,os
+import cStringIO, webui, store, config
+from wsgiref.simple_server import make_server
+
+class Request:
+    def __init__(self, environ, start_response, config):
+        self.start_response = start_response
+        try:
+            length = int(environ['CONTENT_LENGTH'])
+        except ValueError:
+            length = 0
+        self.rfile = cStringIO.StringIO(environ['wsgi.input'].read(length))
+        self.wfile = cStringIO.StringIO()
+        self.config = config.Config(config)
+
+    def send_response(self, code, message='no details available'):
+        self.status = '%s %s' % (code, message)
+        self.headers = []
+
+    def send_header(self, keyword, value):
+        self.headers.append((keyword, value))
+
+    def set_content_type(self, content_type):
+        self.send_header('Content-Type', content_type)
+
+    def end_headers(self):
+        self.start_response(self.status, self.headers)
+
+class Application:
+    def __init__(self, config, debug=False):
+        self.config = config
+
+        if debug:
+            self.__call__ = self.debug
+        else:
+            self.__call__ = self.application
+            store.keep_conn = True
+
+    def application(self, environ, start_response):
+        if "HTTP_AUTHORIZATION" in environ:
+            environ["HTTP_CGI_AUTHORIZATION"] = environ["HTTP_AUTHORIZATION"]
+        r = Request(environ, start_response)
+        webui.WebUI(r, environ).run()
+        return [r.wfile.getvalue()]
+
+    def debug(self, environ, start_response):
+        if environ['PATH_INFO'].startswith("/auth") and \
+               "HTTP_AUTHORIZATION" not in environ:
+            start_response("401 login",
+                           [('WWW-Authenticate', 'Basic realm="foo"')])
+            return
+        start_response("200 ok", [('Content-type', 'text/plain')])
+        environ = environ.items()
+        environ.sort()
+        for k,v in environ:
+            yield "%s=%s\n" % (k, v)
+        return
+
+    def test(self, port):
+        # very simple wsgi server so we can play locally
+        httpd = make_server('', port, self)
+        print "Serving on port %d..." % port
+        httpd.serve_forever()
+


More information about the Pypi-checkins mailing list