[z3-checkins] r55575 - z3/deliverance/sandboxes/paul/dvplone/lib

paul at codespeak.net paul at codespeak.net
Wed Jun 4 20:42:02 CEST 2008


Author: paul
Date: Wed Jun  4 20:41:59 2008
New Revision: 55575

Modified:
   z3/deliverance/sandboxes/paul/dvplone/lib/dvngfilter.py
   z3/deliverance/sandboxes/paul/dvplone/lib/dvplone.py
   z3/deliverance/sandboxes/paul/dvplone/lib/dvploneapp.py
Log:
Detect which themeset id to use and choose that processor.  Keep processors around per request (for better or worse)

Modified: z3/deliverance/sandboxes/paul/dvplone/lib/dvngfilter.py
==============================================================================
--- z3/deliverance/sandboxes/paul/dvplone/lib/dvngfilter.py	(original)
+++ z3/deliverance/sandboxes/paul/dvplone/lib/dvngfilter.py	Wed Jun  4 20:41:59 2008
@@ -6,21 +6,58 @@
 from lxml import etree
 from wsgifilter import Filter
 
+TSHEADER = "X-DV-Themeset"
+
 class DVNGFilter(Filter):
 
-    def __init__(self, app, etcdir):
+    default_themeid = "plone"
+
+    def __init__(self, app, varthemesets):
         self.app = app
-        themesetfn = os.path.join(etcdir, "themeset1.xml")
-        self.dvng = DVNG(themesetfn)
 
-        # Make an XSLT processor from the compiled theme
-        self.processor = etree.XSLT(self.dvng.xslt)
+        # The directory for var/themesets holds the cached versions of
+        # compiled themesets.  Passed in from the .ini file.
+        self.varthemesets = varthemesets
+
+        # Cache the processors for themesets we have already loaded.
+        # Obviously we need a way to tell these things to update
+        self.cached_themesets = {}
+
+
+    def loadThemeset(self, themesetid):
+        """Load themesetid if not loaded, then return DVNG processor"""
+
+        # XXX This might be a terrible idea.  Need to make sure 
+        # that lxml/libxml2 caching of XSLT processors doesn't 
+        # vomit.  If so, we just reload the processor on each request.
+        processor = self.cached_themesets.get(themesetid, None)
+        if processor is None:
+            # Need to load, store, and return the themeset
+            fn = "%s/themeset.xml" % themesetid
+            fullfn = os.path.join(self.varthemesets, fn)
+            themesetfn = os.path.join(fullfn)
+            dvng = DVNG(themesetfn)
+            processor = etree.XSLT(dvng.xslt)
+            self.cached_themesets[themesetid] = processor
+
+        return processor
+
 
     def filter(self, environ, headers, data):
+        """Turn incoming HTML4 data into etree, apply XSLT"""
+        
+        # First find out which themeset to use
+        themesetid = environ.get(TSHEADER, None)
+        if themesetid is None:
+            themesetid = self.default_themeid
+
+        # Get the processor from cache, or force a load
+        processor = self.loadThemeset(themesetid)
 
-        # Turn incoming HTML4 data into etree, apply XSLT
+        # Apply the processor to the HTML and return the themed
+        # result.
         htmldoc = lxml.html.document_fromstring(data)
-        result = self.processor(htmldoc)
+        result = processor(htmldoc)
         response = str(result)
         return response
 

Modified: z3/deliverance/sandboxes/paul/dvplone/lib/dvplone.py
==============================================================================
--- z3/deliverance/sandboxes/paul/dvplone/lib/dvplone.py	(original)
+++ z3/deliverance/sandboxes/paul/dvplone/lib/dvplone.py	Wed Jun  4 20:41:59 2008
@@ -4,11 +4,10 @@
 from dvploneapp import DVPlone
 
 
-def make_dvng(app, global_conf, **kw):
+def make_dvng(app, global_conf, varthemesets, **kw):
     """Choose which Deliverance theme should be used"""
 
-    etcdir = global_conf['here']
-    return DVNGFilter(app, etcdir)
+    return DVNGFilter(app, varthemesets)
 
 def make_dvplone(global_config, document_root, **local_conf):
 

Modified: z3/deliverance/sandboxes/paul/dvplone/lib/dvploneapp.py
==============================================================================
--- z3/deliverance/sandboxes/paul/dvplone/lib/dvploneapp.py	(original)
+++ z3/deliverance/sandboxes/paul/dvplone/lib/dvploneapp.py	Wed Jun  4 20:41:59 2008
@@ -1,7 +1,11 @@
 """WSGI App that pretends to be Plone + Deliverance"""
 
 from paste.urlparser import StaticURLParser
+from dvngfilter import TSHEADER
 
 class DVPlone(StaticURLParser):
 
-    pass
+    def __call__(self, environ, start_response):
+        print "IN DVPlone"
+        environ[TSHEADER] = "nuplone"
+        return super(DVPlone, self).__call__(environ, start_response)


More information about the z3-checkins mailing list