comparison 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
comparison
equal deleted inserted replaced
665:5dc13c816045 666:81d9c3a3a0b5
38 38
39 39
40 class PieCrustDebugMiddleware(object): 40 class PieCrustDebugMiddleware(object):
41 """ WSGI middleware that handles debugging of PieCrust stuff. 41 """ WSGI middleware that handles debugging of PieCrust stuff.
42 """ 42 """
43 def __init__(self, app, root_dir, debug=False, theme_site=False, 43 def __init__(self, app, appfactory,
44 sub_cache_dir=None, run_sse_check=None): 44 run_sse_check=None):
45 self.app = app 45 self.app = app
46 self.root_dir = root_dir 46 self.appfactory = appfactory
47 self.debug = debug
48 self.theme_site = theme_site
49 self.sub_cache_dir = sub_cache_dir
50 self.run_sse_check = run_sse_check 47 self.run_sse_check = run_sse_check
51 self._proc_loop = None 48 self._proc_loop = None
52 self._out_dir = os.path.join(root_dir, CACHE_DIR, 'server') 49 self._out_dir = os.path.join(
53 if sub_cache_dir: 50 root_dir, CACHE_DIR, appfactory.cache_key, 'server')
54 self._out_dir = os.path.join(sub_cache_dir, 'server')
55 self._handlers = { 51 self._handlers = {
56 'debug_info': self._getDebugInfo, 52 'debug_info': self._getDebugInfo,
57 'werkzeug_shutdown': self._shutdownWerkzeug, 53 'werkzeug_shutdown': self._shutdownWerkzeug,
58 'pipeline_status': self._startSSEProvider} 54 'pipeline_status': self._startSSEProvider}
59 55
61 # When using a server with code reloading, some implementations 57 # When using a server with code reloading, some implementations
62 # use process forking and we end up going here twice. We only want 58 # use process forking and we end up going here twice. We only want
63 # to start the pipeline loop in the inner process most of the 59 # to start the pipeline loop in the inner process most of the
64 # time so we let the implementation tell us if this is OK. 60 # time so we let the implementation tell us if this is OK.
65 from piecrust.serving.procloop import ProcessingLoop 61 from piecrust.serving.procloop import ProcessingLoop
66 self._proc_loop = ProcessingLoop(root_dir, self._out_dir, 62 self._proc_loop = ProcessingLoop(self.appfactory, self._out_dir)
67 theme_site=theme_site,
68 sub_cache_dir=sub_cache_dir,
69 debug=debug)
70 self._proc_loop.start() 63 self._proc_loop.start()
71 64
72 def __call__(self, environ, start_response): 65 def __call__(self, environ, start_response):
73 debug_mount = '/__piecrust_debug/' 66 debug_mount = '/__piecrust_debug/'
74 67
80 return handler(request, start_response) 73 return handler(request, start_response)
81 74
82 return self.app(environ, start_response) 75 return self.app(environ, start_response)
83 76
84 def _getDebugInfo(self, request, start_response): 77 def _getDebugInfo(self, request, start_response):
85 app = get_app_for_server(self.root_dir, debug=self.debug, 78 app = get_app_for_server(self.appfactory)
86 sub_cache_dir=self.sub_cache_dir)
87 if not app.config.get('site/enable_debug_info'): 79 if not app.config.get('site/enable_debug_info'):
88 return Forbidden() 80 return Forbidden()
89 81
90 found = False 82 found = False
91 page_path = request.args.get('page') 83 page_path = request.args.get('page')