[z3-checkins] r30825 - in z3/deliverance/branches/packaged: deliverance deliverance/etc deliverance/tests deliverance/tests/test-data deliverance/tests/test-data/etc etc

ianb at codespeak.net ianb at codespeak.net
Mon Jul 31 22:09:23 CEST 2006


Author: ianb
Date: Mon Jul 31 22:09:14 2006
New Revision: 30825

Added:
   z3/deliverance/branches/packaged/deliverance/etc/
      - copied from r30821, z3/deliverance/branches/packaged/etc/
   z3/deliverance/branches/packaged/deliverance/tests/test-data/
   z3/deliverance/branches/packaged/deliverance/tests/test-data/etc/
   z3/deliverance/branches/packaged/deliverance/tests/test-data/index.html   (contents, props changed)
   z3/deliverance/branches/packaged/deliverance/tests/test_wsgifilter.py   (contents, props changed)
   z3/deliverance/branches/packaged/deliverance/wsgifilter.py   (contents, props changed)
Removed:
   z3/deliverance/branches/packaged/etc/
Modified:
   z3/deliverance/branches/packaged/deliverance/main.py
   z3/deliverance/branches/packaged/deliverance/mpfilter.py
Log:
Some more rearranging, initial test (still committing mostly rearrangement)

Modified: z3/deliverance/branches/packaged/deliverance/main.py
==============================================================================
--- z3/deliverance/branches/packaged/deliverance/main.py	(original)
+++ z3/deliverance/branches/packaged/deliverance/main.py	Mon Jul 31 22:09:14 2006
@@ -1,4 +1,3 @@
-
 import os
 from lxml import etree
 from time import time
@@ -14,11 +13,12 @@
 
 class AppMap:
 
-    def __init__(self):
+    def __init__(self, layout_dir):
         
         # Open the appmap file, make a tree, and process XIncludes
         self.module_dir = os.path.dirname(os.path.abspath(__file__))
-        layoutsfn = os.path.join(self.module_dir, "etc/appmap.xml")
+        self.layout_dir = os.path.join(self.module_dir, layout_dir)
+        layoutsfn = os.path.join(self.layout_dir, "appmap.xml")
         self.tree = etree.ElementTree(file=layoutsfn)
         self.tree.xinclude()
 

Modified: z3/deliverance/branches/packaged/deliverance/mpfilter.py
==============================================================================
--- z3/deliverance/branches/packaged/deliverance/mpfilter.py	(original)
+++ z3/deliverance/branches/packaged/deliverance/mpfilter.py	Mon Jul 31 22:09:14 2006
@@ -12,7 +12,7 @@
 from cStringIO import StringIO
 
 from mod_python import apache
-from deliverance import AppMap
+from deliverance.main import AppMap
 appmap = AppMap() # Theme is generated once at module import time
 
 def outputfilter(filter):

Added: z3/deliverance/branches/packaged/deliverance/tests/test-data/index.html
==============================================================================
--- (empty file)
+++ z3/deliverance/branches/packaged/deliverance/tests/test-data/index.html	Mon Jul 31 22:09:14 2006
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
+<title>Test file</title>
+</head>
+
+<body>
+<h1>Test file</h1>
+
+This is a test file!
+
+</body> </html>

Added: z3/deliverance/branches/packaged/deliverance/tests/test_wsgifilter.py
==============================================================================
--- (empty file)
+++ z3/deliverance/branches/packaged/deliverance/tests/test_wsgifilter.py	Mon Jul 31 22:09:14 2006
@@ -0,0 +1,21 @@
+import os
+from deliverance.main import AppMap
+from deliverance.wsgifilter import DeliveranceMiddleware
+from paste.urlparser import StaticURLParser
+from paste.fixture import TestApp
+
+here = os.path.dirname(__file__)
+test_data = os.path.join(here, 'test-data')
+
+static_app = StaticURLParser(test_data)
+
+wrapped_app = DeliveranceMiddleware(
+    static_app, AppMap(os.path.join(test_data, 'etc')))
+
+app = TestApp(wrapped_app)
+
+def test_filter():
+    root = app.get('/')
+    print res
+    assert 0
+    

Added: z3/deliverance/branches/packaged/deliverance/wsgifilter.py
==============================================================================
--- (empty file)
+++ z3/deliverance/branches/packaged/deliverance/wsgifilter.py	Mon Jul 31 22:09:14 2006
@@ -0,0 +1,57 @@
+"""
+Deliverance theming as WSGI middleware
+
+Deliverance applies a theme to content.
+"""
+
+import time
+from cStringIO import StringIO
+
+from paste.wsgilib import intercept_output
+from paste.response import header_value, replace_header
+from deliverance.main import AppMap
+appmap = AppMap() # Theme is generated once at module import time
+
+class DeliveranceMiddleware(object):
+
+    def __init__(self, app, appmap):
+        self.app = app
+        self.appmap = appmap
+
+    def __call__(self, environ, start_response):
+        qs = environ.get('QUERY_STRING', '')
+        notheme = 'notheme' in qs
+        if notheme:
+            return self.app(environ, start_response)
+        status, headers, body = intercept_output(
+            environ, self.app,
+            self.should_intercept,
+            start_response)
+        if status is None:
+            # should_intercept returned False
+            return body
+        body = self.filter_body(body)
+        replace_header(headers, 'content-length', len(body))
+        start_response(status, headers)
+        return [body]
+
+    def should_intercept(self, status, headers):
+        type = header_value(headers, 'content-type')
+        return type.startswith('text/html')
+
+    def filter_body(self, body):
+        return appmap.publish(body)
+
+def handler(req):
+    """Basic filter applying to all mime types it is registered for"""
+
+    # Get the path, strip off leading slash, and convert to a 
+    # dotted notation for xml:id compatibility
+    path_info = req.path_info[1:]
+    dotted_path = path_info.replace("/", ".")
+
+    response = appmap.publish(dotted_path)
+    req.content_type = "text/html"
+    req.write(response)
+
+    return apache.OK


More information about the z3-checkins mailing list