summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2007-04-20 22:58:41 +0200
committerHelmut Grohne <helmut@subdivi.de>2007-04-20 22:58:41 +0200
commitdab23d4b5bd904329aa43e4056197e10f7794ca7 (patch)
tree9e128e60129acf4775b22560da0425f23b446d13
parentd724bf10dbd7d625c8ad7fef8d8ad5dbde542772 (diff)
downloadwsgitools-dab23d4b5bd904329aa43e4056197e10f7794ca7.tar.gz
added wsgitools.adapters for future wsgi versions
-rw-r--r--wsgitools/adapters.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/wsgitools/adapters.py b/wsgitools/adapters.py
new file mode 100644
index 0000000..9f2ab1c
--- /dev/null
+++ b/wsgitools/adapters.py
@@ -0,0 +1,49 @@
+__all__ = []
+
+from filters import CloseableIterator, CloseableList
+
+__all__.append("WSGI2to1Adapter")
+class WSGI2to1Adapter:
+ """Adapts an application with an interface that might somewhen be known as
+ WSGI 2.0 to the WSGI 1.0 interface."""
+ def __init__(self, app):
+ """app is an application with an interface that might somewhen be known
+ as WSGI 2.0."""
+ self.app = app
+ def __call__(self, environ, start_response):
+ """WSGI 1.0 interface"""
+ status, headers, iterable = self.app(environ)
+ start_response(status, headers)
+ return iterable
+
+__all__.append("WSGI1to2Adapter")
+class WSGI1to2Adapter:
+ """Adapts a WSGI 1.0 application to something that might somewhen be the
+ WSGI 2.0 interface."""
+ def __init__(self, app):
+ """app is a WSGI 1.0 application"""
+ self.app = app
+ def __call__(self, environ):
+ """some interface that might somewhen be known as WSGI 2.0"""
+ results = [None, None, []]
+ def start_response(status, headers, exc_info=None):
+ results[0] = status
+ results[1] = headers
+ def write_callable(data):
+ results[2].append(data)
+ return write_callable
+ iterable = self.app(environ, start_response)
+ if isinstance(iterable, list):
+ # retaining .close attribute this way
+ iterable[:0] = results[2]
+ return results[0], results[1], results[2]
+ close_function = getattr(iterable, "close", None)
+ iterable = iter(iterable)
+ try:
+ first = iterable.next()
+ except StopIteration:
+ return (results[0], results[1],
+ CloseableList(close_function, results[2]))
+ results[2].append(first)
+ return (results[0], results[1],
+ CloseableIterator(close_function, results[2], iterable))