Mercurial > piecrust2
annotate piecrust/serving.py @ 219:d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
The server can now run an endpoint that streams pipeline status, including
errors or "fixed" statuses.
As a result, I had to investigate using event-loop based server alternatives,
before I figured out the correct flag to set in Werkzeug. Support for Gunicorn
is therefore now possible, although disabled by default. I will come in handy
though when proper support for CMS-mode is enabled.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 02 Feb 2015 08:34:44 -0800 |
parents | 09e350db7f8f |
children | 7decf00eee47 |
rev | line source |
---|---|
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
1 import io |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
2 import os |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import re |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
4 import json |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import gzip |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 import time |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
7 import queue |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 import os.path |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 import hashlib |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 import logging |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
11 import threading |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
12 from werkzeug.exceptions import ( |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
13 NotFound, MethodNotAllowed, InternalServerError, HTTPException) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 from werkzeug.wrappers import Request, Response |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
15 from werkzeug.wsgi import ClosingIterator, wrap_file |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 from jinja2 import FileSystemLoader, Environment |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 from piecrust.app import PieCrust |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
18 from piecrust.data.filters import ( |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
19 PaginationFilter, HasFilterClause, IsFilterClause) |
111
208c652551a3
Quick fix for making the server correctly update referenced pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
110
diff
changeset
|
20 from piecrust.environment import StandardEnvironment |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 from piecrust.processing.base import ProcessorPipeline |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 from piecrust.rendering import PageRenderingContext, render_page |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
23 from piecrust.sources.base import PageFactory, MODE_PARSING |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 logger = logging.getLogger(__name__) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 |
111
208c652551a3
Quick fix for making the server correctly update referenced pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
110
diff
changeset
|
29 class ServingEnvironment(StandardEnvironment): |
113
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
30 pass |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
31 |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
32 |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
33 class ServeRecord(object): |
111
208c652551a3
Quick fix for making the server correctly update referenced pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
110
diff
changeset
|
34 def __init__(self): |
113
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
35 self.entries = {} |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
36 |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
37 def addEntry(self, entry): |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
38 key = self._makeKey(entry.uri, entry.sub_num) |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
39 self.entries[key] = entry |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
40 |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
41 def getEntry(self, uri, sub_num): |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
42 key = self._makeKey(uri, sub_num) |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
43 return self.entries.get(key) |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
44 |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
45 def _makeKey(self, uri, sub_num): |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
46 return "%s:%s" % (uri, sub_num) |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
47 |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
48 |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
49 class ServeRecordPageEntry(object): |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
50 def __init__(self, uri, sub_num): |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
51 self.uri = uri |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
52 self.sub_num = sub_num |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
53 self.used_source_names = set() |
111
208c652551a3
Quick fix for making the server correctly update referenced pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
110
diff
changeset
|
54 |
208c652551a3
Quick fix for making the server correctly update referenced pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
110
diff
changeset
|
55 |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
56 class WsgiServerWrapper(object): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
57 def __init__(self, server): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
58 self.server = server |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
59 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
60 def __call__(self, environ, start_response): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
61 return self.server._run_request(environ, start_response) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
62 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
63 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
64 class Server(object): |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
65 def __init__(self, root_dir, |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
66 debug=False, use_reloader=False, static_preview=True): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
67 self.root_dir = root_dir |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 self.debug = debug |
155
70b86e904b85
Properly use, or not, the debugging when using the chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
141
diff
changeset
|
69 self.use_reloader = use_reloader |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 self.static_preview = static_preview |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 self._out_dir = None |
113
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
72 self._page_record = None |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
73 self._proc_loop = None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 self._mimetype_map = load_mimetype_map() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
76 def getWsgiApp(self): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
77 # Bake all the assets so we know what we have, and so we can serve |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 # them to the client. We need a temp app for this. |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 app = PieCrust(root_dir=self.root_dir, debug=self.debug) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
80 self._out_dir = os.path.join(app.cache_dir, 'server') |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
81 self._page_record = ServeRecord() |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
82 |
214
09e350db7f8f
serve: Don't have 2 processing loops running when using `--use-reloader`.
Ludovic Chabant <ludovic@chabant.com>
parents:
209
diff
changeset
|
83 if (not self.use_reloader or |
09e350db7f8f
serve: Don't have 2 processing loops running when using `--use-reloader`.
Ludovic Chabant <ludovic@chabant.com>
parents:
209
diff
changeset
|
84 os.environ.get('WERKZEUG_RUN_MAIN') == 'true'): |
09e350db7f8f
serve: Don't have 2 processing loops running when using `--use-reloader`.
Ludovic Chabant <ludovic@chabant.com>
parents:
209
diff
changeset
|
85 # We don't want to run the processing loop here if this isn't |
09e350db7f8f
serve: Don't have 2 processing loops running when using `--use-reloader`.
Ludovic Chabant <ludovic@chabant.com>
parents:
209
diff
changeset
|
86 # the actual process that does the serving. In most cases it is, |
09e350db7f8f
serve: Don't have 2 processing loops running when using `--use-reloader`.
Ludovic Chabant <ludovic@chabant.com>
parents:
209
diff
changeset
|
87 # but if we're using Werkzeug's reloader, then it won't be the |
09e350db7f8f
serve: Don't have 2 processing loops running when using `--use-reloader`.
Ludovic Chabant <ludovic@chabant.com>
parents:
209
diff
changeset
|
88 # first time we get there... it will only be the correct process |
09e350db7f8f
serve: Don't have 2 processing loops running when using `--use-reloader`.
Ludovic Chabant <ludovic@chabant.com>
parents:
209
diff
changeset
|
89 # the second time, when the reloading process is spawned, with the |
09e350db7f8f
serve: Don't have 2 processing loops running when using `--use-reloader`.
Ludovic Chabant <ludovic@chabant.com>
parents:
209
diff
changeset
|
90 # `WERKZEUG_RUN_MAIN` variable set. |
09e350db7f8f
serve: Don't have 2 processing loops running when using `--use-reloader`.
Ludovic Chabant <ludovic@chabant.com>
parents:
209
diff
changeset
|
91 pipeline = ProcessorPipeline(app, self._out_dir) |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
92 self._proc_loop = ProcessingLoop(pipeline) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
93 self._proc_loop.start() |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
95 # Run the WSGI app. |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
96 wsgi_wrapper = WsgiServerWrapper(self) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
97 return wsgi_wrapper |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
98 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 def _run_request(self, environ, start_response): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 try: |
200
76e459d48c43
serve: Correctly pass on the HTTP status code when an error occurs.
Ludovic Chabant <ludovic@chabant.com>
parents:
190
diff
changeset
|
101 return self._try_run_request(environ, start_response) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 except Exception as ex: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 if self.debug: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 raise |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 return self._handle_error(ex, environ, start_response) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 |
200
76e459d48c43
serve: Correctly pass on the HTTP status code when an error occurs.
Ludovic Chabant <ludovic@chabant.com>
parents:
190
diff
changeset
|
107 def _try_run_request(self, environ, start_response): |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 request = Request(environ) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 # We don't support anything else than GET requests since we're |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 # previewing something that will be static later. |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 if self.static_preview and request.method != 'GET': |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
113 logger.error("Only GET requests are allowed, got %s" % |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
114 request.method) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
115 raise MethodNotAllowed() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
117 # Handle special requests right away. |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
118 response = self._try_special_request(environ, request) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
119 if response is not None: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
120 return response(environ, start_response) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
121 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
122 # Also handle requests to a pipeline-built asset right away. |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
123 response = self._try_serve_asset(environ, request) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
124 if response is not None: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
125 return response(environ, start_response) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
126 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 # Create the app for this request. |
141
666ee97e77a9
Switch the PieCrust server to debug mode with `?!debug` in the URL.
Ludovic Chabant <ludovic@chabant.com>
parents:
136
diff
changeset
|
128 rq_debug = ('!debug' in request.args) |
666ee97e77a9
Switch the PieCrust server to debug mode with `?!debug` in the URL.
Ludovic Chabant <ludovic@chabant.com>
parents:
136
diff
changeset
|
129 app = PieCrust(root_dir=self.root_dir, debug=(self.debug or rq_debug)) |
53
73956224eb67
Setup the server better.
Ludovic Chabant <ludovic@chabant.com>
parents:
39
diff
changeset
|
130 app.config.set('site/root', '/') |
73956224eb67
Setup the server better.
Ludovic Chabant <ludovic@chabant.com>
parents:
39
diff
changeset
|
131 app.config.set('site/pretty_urls', True) |
73956224eb67
Setup the server better.
Ludovic Chabant <ludovic@chabant.com>
parents:
39
diff
changeset
|
132 app.config.set('server/is_serving', True) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
134 # We'll serve page assets directly from where they are. |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 app.env.base_asset_url_format = '/_asset/%path%' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
136 |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
137 # Let's see if it can be a page asset. |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 response = self._try_serve_page_asset(app, environ, request) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 if response is not None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 return response(environ, start_response) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
141 |
133
9e4c2e68a129
Optimize server for files that already exist.
Ludovic Chabant <ludovic@chabant.com>
parents:
130
diff
changeset
|
142 # Nope. Let's see if it's an actual page. |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
143 try: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 response = self._try_serve_page(app, environ, request) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
145 return response(environ, start_response) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
146 except (RouteNotFoundError, SourceNotFoundError) as ex: |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
147 raise NotFound(str(ex)) |
133
9e4c2e68a129
Optimize server for files that already exist.
Ludovic Chabant <ludovic@chabant.com>
parents:
130
diff
changeset
|
148 except HTTPException: |
9e4c2e68a129
Optimize server for files that already exist.
Ludovic Chabant <ludovic@chabant.com>
parents:
130
diff
changeset
|
149 raise |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
150 except Exception as ex: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
151 if app.debug: |
128
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
152 logger.exception(ex) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
153 raise |
128
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
154 msg = str(ex) |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
155 logger.error(msg) |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
156 raise InternalServerError(msg) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
157 |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
158 def _try_special_request(self, environ, request): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
159 static_mount = '/__piecrust_static/' |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
160 if request.path.startswith(static_mount): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
161 rel_req_path = request.path[len(static_mount):] |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
162 mount = os.path.join( |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
163 os.path.dirname(__file__), |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
164 'resources', 'server') |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
165 full_path = os.path.join(mount, rel_req_path) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
166 try: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
167 response = self._make_wrapped_file_response( |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
168 environ, full_path) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
169 return response |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
170 except OSError: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
171 pass |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
172 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
173 debug_mount = '/__piecrust_debug/' |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
174 if request.path.startswith(debug_mount): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
175 rel_req_path = request.path[len(debug_mount):] |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
176 if rel_req_path == 'pipeline_status': |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
177 provider = PipelineStatusServerSideEventProducer( |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
178 self._proc_loop.status_queue) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
179 it = ClosingIterator(provider.run(), [provider.close]) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
180 response = Response(it) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
181 response.headers['Cache-Control'] = 'no-cache' |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
182 if 'text/event-stream' in request.accept_mimetypes: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
183 response.mimetype = 'text/event-stream' |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
184 response.direct_passthrough = True |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
185 response.implicit_sequence_conversion = False |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
186 return response |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
187 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
188 return None |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
189 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
190 def _try_serve_asset(self, environ, request): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
191 rel_req_path = request.path.lstrip('/').replace('/', os.sep) |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
192 full_path = os.path.join(self._out_dir, rel_req_path) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
193 try: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
194 response = self._make_wrapped_file_response( |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
195 environ, full_path) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
196 return response |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
197 except OSError: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
198 pass |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
199 return None |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
200 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
201 def _try_serve_page_asset(self, app, environ, request): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
202 if not request.path.startswith('/_asset/'): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
203 return None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
204 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
205 full_path = os.path.join(app.root_dir, request.path[len('/_asset/'):]) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
206 if not os.path.isfile(full_path): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
207 return None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
208 |
190
430ee5b80962
serve: Make the server find assets generated by external tools.
Ludovic Chabant <ludovic@chabant.com>
parents:
176
diff
changeset
|
209 return self._make_wrapped_file_response(environ, full_path) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
210 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
211 def _try_serve_page(self, app, environ, request): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
212 # Try to find what matches the requested URL. |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
213 req_path = request.path |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
214 page_num = 1 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
215 pgn_suffix_re = app.config.get('__cache/pagination_suffix_re') |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
216 pgn_suffix_m = re.search(pgn_suffix_re, request.path) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
217 if pgn_suffix_m: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
218 req_path = request.path[:pgn_suffix_m.start()] |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
219 page_num = int(pgn_suffix_m.group('num')) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
220 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
221 routes = find_routes(app.routes, req_path) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
222 if len(routes) == 0: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
223 raise RouteNotFoundError("Can't find route for: %s" % req_path) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
224 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
225 taxonomy = None |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
226 for route, route_metadata in routes: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
227 source = app.getSource(route.source_name) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
228 if route.taxonomy is None: |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
229 rel_path, fac_metadata = source.findPagePath( |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
230 route_metadata, MODE_PARSING) |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
231 if rel_path is not None: |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
232 break |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
233 else: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
234 taxonomy = app.getTaxonomy(route.taxonomy) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
235 term_value = route_metadata.get(taxonomy.term_name) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
236 if term_value is not None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
237 tax_page_ref = taxonomy.getPageRef(source.name) |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
238 rel_path = tax_page_ref.rel_path |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
239 source = tax_page_ref.source |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
240 fac_metadata = {taxonomy.term_name: term_value} |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
241 break |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
242 else: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
243 raise SourceNotFoundError("Can't find path for: %s " |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
244 "(looked in: %s)" % |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
245 (req_path, [r.source_name for r, _ in routes])) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
246 |
113
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
247 # Build the page. |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
248 fac = PageFactory(source, rel_path, fac_metadata) |
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
249 page = fac.buildPage() |
174
e9a3d405e18f
serve: Always force render the page being previewed.
Ludovic Chabant <ludovic@chabant.com>
parents:
173
diff
changeset
|
250 # We force the rendering of the page because it could not have |
e9a3d405e18f
serve: Always force render the page being previewed.
Ludovic Chabant <ludovic@chabant.com>
parents:
173
diff
changeset
|
251 # changed, but include pages that did change. |
e9a3d405e18f
serve: Always force render the page being previewed.
Ludovic Chabant <ludovic@chabant.com>
parents:
173
diff
changeset
|
252 render_ctx = PageRenderingContext(page, req_path, page_num, |
e9a3d405e18f
serve: Always force render the page being previewed.
Ludovic Chabant <ludovic@chabant.com>
parents:
173
diff
changeset
|
253 force_render=True) |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
254 if taxonomy is not None: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
255 flt = PaginationFilter() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
256 if taxonomy.is_multiple: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
257 flt.addClause(HasFilterClause(taxonomy.name, term_value)) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
258 else: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
259 flt.addClause(IsFilterClause(taxonomy.name, term_value)) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
260 render_ctx.pagination_filter = flt |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
261 render_ctx.custom_data = { |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
262 taxonomy.term_name: term_value} |
113
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
263 |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
264 # See if this page is known to use sources. If that's the case, |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
265 # just don't use cached rendered segments for that page (but still |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
266 # use them for pages that are included in it). |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
267 entry = self._page_record.getEntry(req_path, page_num) |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
268 if (taxonomy is not None or entry is None or |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
269 entry.used_source_names): |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
270 cache_key = '%s:%s' % (req_path, page_num) |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
271 app.env.rendered_segments_repository.invalidate(cache_key) |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
272 |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
273 # Render the page. |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
274 rendered_page = render_page(render_ctx) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
275 rp_content = rendered_page.content |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
276 |
128
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
277 if taxonomy is not None: |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
278 paginator = rendered_page.data.get('pagination') |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
279 if (paginator and paginator.is_loaded and |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
280 len(paginator.items) == 0): |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
281 message = ("This URL matched a route for taxonomy '%s' but " |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
282 "no pages have been found to have it. This page " |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
283 "won't be generated by a bake." % taxonomy.name) |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
284 raise NotFound(message) |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
285 |
113
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
286 if entry is None: |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
287 entry = ServeRecordPageEntry(req_path, page_num) |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
288 self._page_record.addEntry(entry) |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
289 entry.used_source_names = set(render_ctx.used_source_names) |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
290 |
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
291 # Profiling. |
7
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
292 if app.debug: |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
293 now_time = time.clock() |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
294 timing_info = ('%8.1f ms' % |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
295 ((now_time - app.env.start_time) * 1000.0)) |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
296 rp_content = rp_content.replace('__PIECRUST_TIMING_INFORMATION__', |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
297 timing_info) |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
298 |
113
de257cc40ce1
Re-enable proper caching of rendered segments in server.
Ludovic Chabant <ludovic@chabant.com>
parents:
111
diff
changeset
|
299 # Build the response. |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
300 response = Response() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
301 |
7
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
302 etag = hashlib.md5(rp_content.encode('utf8')).hexdigest() |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
303 if not app.debug and etag in request.if_none_match: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
304 response.status_code = 304 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
305 return response |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
306 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
307 response.set_etag(etag) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
308 response.content_md5 = etag |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
309 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
310 cache_control = response.cache_control |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
311 if app.debug: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
312 cache_control.no_cache = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
313 cache_control.must_revalidate = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
314 else: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
315 cache_time = (page.config.get('cache_time') or |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
316 app.config.get('site/cache_time')) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
317 if cache_time: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
318 cache_control.public = True |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
319 cache_control.max_age = cache_time |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
320 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
321 content_type = page.config.get('content_type') |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
322 if content_type and '/' not in content_type: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
323 mimetype = content_type_map.get(content_type, content_type) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
324 else: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
325 mimetype = content_type |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
326 if mimetype: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
327 response.mimetype = mimetype |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
328 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
329 if ('gzip' in request.accept_encodings and |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
330 app.config.get('site/enable_gzip')): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
331 try: |
7
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
332 with io.BytesIO() as gzip_buffer: |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
333 with gzip.open(gzip_buffer, mode='wt', |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
334 encoding='utf8') as gzip_file: |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
335 gzip_file.write(rp_content) |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
336 rp_content = gzip_buffer.getvalue() |
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
337 response.content_encoding = 'gzip' |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
338 except Exception: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
339 logger.exception("Error compressing response, " |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
340 "falling back to uncompressed.") |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
341 response.set_data(rp_content) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
342 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
343 return response |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
344 |
190
430ee5b80962
serve: Make the server find assets generated by external tools.
Ludovic Chabant <ludovic@chabant.com>
parents:
176
diff
changeset
|
345 def _make_wrapped_file_response(self, environ, path): |
430ee5b80962
serve: Make the server find assets generated by external tools.
Ludovic Chabant <ludovic@chabant.com>
parents:
176
diff
changeset
|
346 logger.debug("Serving %s" % path) |
430ee5b80962
serve: Make the server find assets generated by external tools.
Ludovic Chabant <ludovic@chabant.com>
parents:
176
diff
changeset
|
347 wrapper = wrap_file(environ, open(path, 'rb')) |
430ee5b80962
serve: Make the server find assets generated by external tools.
Ludovic Chabant <ludovic@chabant.com>
parents:
176
diff
changeset
|
348 response = Response(wrapper) |
430ee5b80962
serve: Make the server find assets generated by external tools.
Ludovic Chabant <ludovic@chabant.com>
parents:
176
diff
changeset
|
349 _, ext = os.path.splitext(path) |
430ee5b80962
serve: Make the server find assets generated by external tools.
Ludovic Chabant <ludovic@chabant.com>
parents:
176
diff
changeset
|
350 response.mimetype = self._mimetype_map.get( |
430ee5b80962
serve: Make the server find assets generated by external tools.
Ludovic Chabant <ludovic@chabant.com>
parents:
176
diff
changeset
|
351 ext.lstrip('.'), 'text/plain') |
430ee5b80962
serve: Make the server find assets generated by external tools.
Ludovic Chabant <ludovic@chabant.com>
parents:
176
diff
changeset
|
352 return response |
430ee5b80962
serve: Make the server find assets generated by external tools.
Ludovic Chabant <ludovic@chabant.com>
parents:
176
diff
changeset
|
353 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
354 def _handle_error(self, exception, environ, start_response): |
200
76e459d48c43
serve: Correctly pass on the HTTP status code when an error occurs.
Ludovic Chabant <ludovic@chabant.com>
parents:
190
diff
changeset
|
355 code = 500 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
356 path = 'error' |
128
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
357 description = str(exception) |
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
358 if isinstance(exception, HTTPException): |
200
76e459d48c43
serve: Correctly pass on the HTTP status code when an error occurs.
Ludovic Chabant <ludovic@chabant.com>
parents:
190
diff
changeset
|
359 code = exception.code |
128
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
360 description = exception.description |
200
76e459d48c43
serve: Correctly pass on the HTTP status code when an error occurs.
Ludovic Chabant <ludovic@chabant.com>
parents:
190
diff
changeset
|
361 if isinstance(exception, NotFound): |
76e459d48c43
serve: Correctly pass on the HTTP status code when an error occurs.
Ludovic Chabant <ludovic@chabant.com>
parents:
190
diff
changeset
|
362 path += '404' |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
363 env = Environment(loader=ErrorMessageLoader()) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
364 template = env.get_template(path) |
128
28444014ce7d
Fix error reporting and counting of lines.
Ludovic Chabant <ludovic@chabant.com>
parents:
113
diff
changeset
|
365 context = {'details': description} |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
366 response = Response(template.render(context), mimetype='text/html') |
200
76e459d48c43
serve: Correctly pass on the HTTP status code when an error occurs.
Ludovic Chabant <ludovic@chabant.com>
parents:
190
diff
changeset
|
367 response.status_code = code |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
368 return response(environ, start_response) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
369 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
370 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
371 class RouteNotFoundError(Exception): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
372 pass |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
373 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
374 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
375 class SourceNotFoundError(Exception): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
376 pass |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
377 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
378 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
379 content_type_map = { |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
380 'html': 'text/html', |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
381 'xml': 'text/xml', |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
382 'txt': 'text/plain', |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
383 'text': 'text/plain', |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
384 'css': 'text/css', |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
385 'xhtml': 'application/xhtml+xml', |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
386 'atom': 'application/atom+xml', # or 'text/xml'? |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
387 'rss': 'application/rss+xml', # or 'text/xml'? |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
388 'json': 'application/json'} |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
389 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
390 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
391 def find_routes(routes, uri): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
392 uri = uri.lstrip('/') |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
393 res = [] |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
394 for route in routes: |
176
d47d9493bb0a
routes: When matching URIs, return metadata directly instead of the match object.
Ludovic Chabant <ludovic@chabant.com>
parents:
174
diff
changeset
|
395 metadata = route.matchUri(uri) |
d47d9493bb0a
routes: When matching URIs, return metadata directly instead of the match object.
Ludovic Chabant <ludovic@chabant.com>
parents:
174
diff
changeset
|
396 if metadata: |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
397 res.append((route, metadata)) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
398 return res |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
399 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
400 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
401 class ErrorMessageLoader(FileSystemLoader): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
402 def __init__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
403 base_dir = os.path.join(os.path.dirname(__file__), 'resources', |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
404 'messages') |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
405 super(ErrorMessageLoader, self).__init__(base_dir) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
406 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
407 def get_source(self, env, template): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
408 template += '.html' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
409 return super(ErrorMessageLoader, self).get_source(env, template) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
410 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
411 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
412 def load_mimetype_map(): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
413 mimetype_map = {} |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
414 sep_re = re.compile(r'\s+') |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
415 path = os.path.join(os.path.dirname(__file__), 'mime.types') |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
416 with open(path, 'r') as f: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
417 for line in f: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
418 tokens = sep_re.split(line) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
419 if len(tokens) > 1: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
420 for t in tokens[1:]: |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
421 mimetype_map[t] = tokens[0] |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
422 return mimetype_map |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
423 |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
424 |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
425 class PipelineStatusServerSideEventProducer(object): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
426 def __init__(self, status_queue): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
427 self.status_queue = status_queue |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
428 self.interval = 2 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
429 self.timeout = 60*10 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
430 self._start_time = 0 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
431 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
432 def run(self): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
433 logger.debug("Starting pipeline status SSE.") |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
434 self._start_time = time.time() |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
435 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
436 outstr = 'event: ping\ndata: started\n\n' |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
437 yield bytes(outstr, 'utf8') |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
438 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
439 count = 0 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
440 while True: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
441 if time.time() > self.timeout + self._start_time: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
442 logger.debug("Closing pipeline status SSE, timeout reached.") |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
443 outstr = 'event: pipeline_timeout\ndata: bye\n\n' |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
444 yield bytes(outstr, 'utf8') |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
445 break |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
446 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
447 try: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
448 logger.debug("Polling pipeline status queue...") |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
449 count += 1 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
450 data = self.status_queue.get(True, self.interval) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
451 except queue.Empty: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
452 if count < 3: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
453 continue |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
454 data = {'type': 'ping', 'message': 'ping'} |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
455 count = 0 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
456 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
457 event_type = data['type'] |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
458 outstr = 'event: %s\ndata: %s\n\n' % ( |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
459 event_type, json.dumps(data)) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
460 logger.debug("Sending pipeline status SSE.") |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
461 yield bytes(outstr, 'utf8') |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
462 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
463 def close(self): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
464 logger.debug("Closing pipeline status SSE.") |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
465 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
466 |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
467 class ProcessingLoop(threading.Thread): |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
468 def __init__(self, pipeline): |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
469 super(ProcessingLoop, self).__init__( |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
470 name='pipeline-reloader', daemon=True) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
471 self.pipeline = pipeline |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
472 self.status_queue = queue.Queue() |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
473 self.interval = 1 |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
474 self._paths = set() |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
475 self._record = None |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
476 self._last_bake = 0 |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
477 |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
478 def run(self): |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
479 # Build the first list of known files and run the pipeline once. |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
480 app = self.pipeline.app |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
481 roots = [os.path.join(app.root_dir, r) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
482 for r in self.pipeline.mounts.keys()] |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
483 for root in roots: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
484 for dirpath, dirnames, filenames in os.walk(root): |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
485 self._paths |= set([os.path.join(dirpath, f) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
486 for f in filenames]) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
487 self._last_bake = time.time() |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
488 self._record = self.pipeline.run(save_record=False) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
489 |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
490 while True: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
491 for root in roots: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
492 # For each mount root we try to find the first new or |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
493 # modified file. If any, we just run the pipeline on |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
494 # that mount. |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
495 found_new_or_modified = False |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
496 for dirpath, dirnames, filenames in os.walk(root): |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
497 for filename in filenames: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
498 path = os.path.join(dirpath, filename) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
499 if path not in self._paths: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
500 logger.debug("Found new asset: %s" % path) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
501 self._paths.add(path) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
502 found_new_or_modified = True |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
503 break |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
504 if os.path.getmtime(path) > self._last_bake: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
505 logger.debug("Found modified asset: %s" % path) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
506 found_new_or_modified = True |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
507 break |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
508 |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
509 if found_new_or_modified: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
510 break |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
511 |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
512 if found_new_or_modified: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
513 self._runPipeline(root) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
514 |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
515 time.sleep(self.interval) |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
516 |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
517 def _runPipeline(self, root): |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
518 self._last_bake = time.time() |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
519 try: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
520 self._record = self.pipeline.run( |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
521 root, |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
522 previous_record=self._record, |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
523 save_record=False) |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
524 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
525 # Update the status queue. |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
526 # (we need to clear it because there may not be a consumer |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
527 # on the other side, if the user isn't running with the |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
528 # debug window active) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
529 while True: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
530 try: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
531 self.status_queue.get_nowait() |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
532 except queue.Empty: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
533 break |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
534 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
535 if self._record.success: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
536 item = { |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
537 'type': 'pipeline_success'} |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
538 self.status_queue.put_nowait(item) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
539 else: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
540 item = { |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
541 'type': 'pipeline_error', |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
542 'assets': []} |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
543 for entry in self._record.entries: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
544 if entry.errors: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
545 asset_item = { |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
546 'path': entry.rel_input, |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
547 'errors': list(entry.errors)} |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
548 item['assets'].append(asset_item) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
214
diff
changeset
|
549 self.status_queue.put_nowait(item) |
209
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
550 except: |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
551 pass |
7a5a7a7e8cee
serve: Run the asset pipeline asynchronously.
Ludovic Chabant <ludovic@chabant.com>
parents:
205
diff
changeset
|
552 |