diff piecrust/serving/server.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/server.py	Thu Mar 03 08:19:28 2016 -0800
+++ b/piecrust/serving/server.py	Thu Mar 03 08:22:41 2016 -0800
@@ -22,8 +22,8 @@
 
 
 class WsgiServer(object):
-    def __init__(self, root_dir, **kwargs):
-        self.server = Server(root_dir, **kwargs)
+    def __init__(self, appfactory, **kwargs):
+        self.server = Server(appfactory, **kwargs)
 
     def __call__(self, environ, start_response):
         return self.server._run_request(environ, start_response)
@@ -70,21 +70,20 @@
 
 
 class Server(object):
-    def __init__(self, root_dir,
-                 debug=False, theme_site=False,
-                 sub_cache_dir=None, enable_debug_info=True,
-                 root_url='/', static_preview=True):
-        self.root_dir = root_dir
-        self.debug = debug
-        self.theme_site = theme_site
-        self.sub_cache_dir = sub_cache_dir
+    def __init__(self, appfactory,
+                 enable_debug_info=True,
+                 root_url='/',
+                 static_preview=True):
+        self.appfactory = appfactory
         self.enable_debug_info = enable_debug_info
         self.root_url = root_url
         self.static_preview = static_preview
         self._page_record = ServeRecord()
-        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(
+                appfactory.root_dir,
+                CACHE_DIR,
+                (appfactory.cache_key or 'default'),
+                'server')
 
     def _run_request(self, environ, start_response):
         try:
@@ -111,9 +110,7 @@
             return response
 
         # Create the app for this request.
-        app = get_app_for_server(self.root_dir, debug=self.debug,
-                                 theme_site=self.theme_site,
-                                 sub_cache_dir=self.sub_cache_dir,
+        app = get_app_for_server(self.appfactory,
                                  root_url=self.root_url)
         if (app.config.get('site/enable_debug_info') and
                 self.enable_debug_info and