diff piecrust/serving/middlewares.py @ 666:81d9c3a3a0b5

internal: Get rid of the whole "sub cache" business. * Compute cache keys up front, so the cache directory is only chosen once. * Buffer up config variants to apply before loading the config. Makes it possible to cache variant-resulting configs, too. * Make a factory class to reuse the logic that creates the `PieCrust` object correctly for multi-process workers and such. * Add a test.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 03 Mar 2016 08:22:41 -0800
parents 3ceeca7bb71c
children c2ea75e37540
line wrap: on
line diff
--- a/piecrust/serving/middlewares.py	Thu Mar 03 08:19:28 2016 -0800
+++ b/piecrust/serving/middlewares.py	Thu Mar 03 08:22:41 2016 -0800
@@ -40,18 +40,14 @@
 class PieCrustDebugMiddleware(object):
     """ WSGI middleware that handles debugging of PieCrust stuff.
     """
-    def __init__(self, app, root_dir, debug=False, theme_site=False,
-                 sub_cache_dir=None, run_sse_check=None):
+    def __init__(self, app, appfactory,
+                 run_sse_check=None):
         self.app = app
-        self.root_dir = root_dir
-        self.debug = debug
-        self.theme_site = theme_site
-        self.sub_cache_dir = sub_cache_dir
+        self.appfactory = appfactory
         self.run_sse_check = run_sse_check
         self._proc_loop = None
-        self._out_dir = os.path.join(root_dir, CACHE_DIR, 'server')
-        if sub_cache_dir:
-            self._out_dir = os.path.join(sub_cache_dir, 'server')
+        self._out_dir = os.path.join(
+                root_dir, CACHE_DIR, appfactory.cache_key, 'server')
         self._handlers = {
                 'debug_info': self._getDebugInfo,
                 'werkzeug_shutdown': self._shutdownWerkzeug,
@@ -63,10 +59,7 @@
             # to start the pipeline loop in the inner process most of the
             # time so we let the implementation tell us if this is OK.
             from piecrust.serving.procloop import ProcessingLoop
-            self._proc_loop = ProcessingLoop(root_dir, self._out_dir,
-                                             theme_site=theme_site,
-                                             sub_cache_dir=sub_cache_dir,
-                                             debug=debug)
+            self._proc_loop = ProcessingLoop(self.appfactory, self._out_dir)
             self._proc_loop.start()
 
     def __call__(self, environ, start_response):
@@ -82,8 +75,7 @@
         return self.app(environ, start_response)
 
     def _getDebugInfo(self, request, start_response):
-        app = get_app_for_server(self.root_dir, debug=self.debug,
-                                 sub_cache_dir=self.sub_cache_dir)
+        app = get_app_for_server(self.appfactory)
         if not app.config.get('site/enable_debug_info'):
             return Forbidden()