comparison piecrust/serving.py @ 7:343d08ef5668

More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 16 Aug 2014 15:07:22 -0700
parents 474c9882decf
children 617191dec18e
comparison
equal deleted inserted replaced
6:f5ca5c5bed85 7:343d08ef5668
21 21
22 22
23 logger = logging.getLogger(__name__) 23 logger = logging.getLogger(__name__)
24 24
25 25
26
27
28 class Server(object): 26 class Server(object):
29 def __init__(self, root_dir, host='localhost', port='8080', 27 def __init__(self, root_dir, host='localhost', port='8080',
30 debug=False, static_preview=True): 28 debug=False, static_preview=True):
31 self.root_dir = root_dir 29 self.root_dir = root_dir
32 self.host = host 30 self.host = host
120 skip_patterns=self._skip_patterns, 118 skip_patterns=self._skip_patterns,
121 force_patterns=self._force_patterns) 119 force_patterns=self._force_patterns)
122 pipeline.run(asset_in_path) 120 pipeline.run(asset_in_path)
123 121
124 logger.debug("Serving %s" % asset_out_path) 122 logger.debug("Serving %s" % asset_out_path)
125 wrapper = wrap_file(environ, open(asset_out_path)) 123 wrapper = wrap_file(environ, open(asset_out_path, 'rb'))
126 response = Response(wrapper) 124 response = Response(wrapper)
127 _, ext = os.path.splitext(rel_req_path) 125 _, ext = os.path.splitext(rel_req_path)
128 response.mimetype = self._mimetype_map.get( 126 response.mimetype = self._mimetype_map.get(
129 ext.lstrip('.'), 'text/plain') 127 ext.lstrip('.'), 'text/plain')
130 return response 128 return response
136 full_path = os.path.join(app.root_dir, request.path[len('/_asset/'):]) 134 full_path = os.path.join(app.root_dir, request.path[len('/_asset/'):])
137 if not os.path.isfile(full_path): 135 if not os.path.isfile(full_path):
138 return None 136 return None
139 137
140 logger.debug("Serving %s" % full_path) 138 logger.debug("Serving %s" % full_path)
141 wrapper = wrap_file(environ, open(full_path)) 139 wrapper = wrap_file(environ, open(full_path, 'rb'))
142 response = Response(wrapper) 140 response = Response(wrapper)
143 _, ext = os.path.splitext(full_path) 141 _, ext = os.path.splitext(full_path)
144 response.mimetype = self._mimetype_map.get( 142 response.mimetype = self._mimetype_map.get(
145 ext.lstrip('.'), 'text/plain') 143 ext.lstrip('.'), 'text/plain')
146 return response 144 return response
195 render_ctx.custom_data = { 193 render_ctx.custom_data = {
196 taxonomy.term_name: term_value} 194 taxonomy.term_name: term_value}
197 rendered_page = render_page(render_ctx) 195 rendered_page = render_page(render_ctx)
198 rp_content = rendered_page.content 196 rp_content = rendered_page.content
199 197
198 if app.debug:
199 now_time = time.clock()
200 timing_info = ('%8.1f ms' %
201 ((now_time - app.env.start_time) * 1000.0))
202 rp_content = rp_content.replace('__PIECRUST_TIMING_INFORMATION__',
203 timing_info)
204
200 # Start response. 205 # Start response.
201 response = Response() 206 response = Response()
202 207
203 etag = hashlib.md5(rp_content).hexdigest() 208 etag = hashlib.md5(rp_content.encode('utf8')).hexdigest()
204 if not app.debug and etag in request.if_none_match: 209 if not app.debug and etag in request.if_none_match:
205 response.status_code = 304 210 response.status_code = 304
206 return response 211 return response
207 212
208 response.set_etag(etag) 213 response.set_etag(etag)
225 else: 230 else:
226 mimetype = content_type 231 mimetype = content_type
227 if mimetype: 232 if mimetype:
228 response.mimetype = mimetype 233 response.mimetype = mimetype
229 234
230 if app.debug:
231 now_time = time.clock()
232 timing_info = ('%8.1f ms' %
233 ((now_time - app.env.start_time) * 1000.0))
234 rp_content = rp_content.replace('__PIECRUST_TIMING_INFORMATION__',
235 timing_info)
236
237 if ('gzip' in request.accept_encodings and 235 if ('gzip' in request.accept_encodings and
238 app.config.get('site/enable_gzip')): 236 app.config.get('site/enable_gzip')):
239 try: 237 try:
240 gzip_buffer = io.StringIO() 238 with io.BytesIO() as gzip_buffer:
241 gzip_file = gzip.GzipFile( 239 with gzip.open(gzip_buffer, mode='wt',
242 mode='wb', 240 encoding='utf8') as gzip_file:
243 compresslevel=9, 241 gzip_file.write(rp_content)
244 fileobj=gzip_buffer) 242 rp_content = gzip_buffer.getvalue()
245 gzip_file.write(rp_content) 243 response.content_encoding = 'gzip'
246 gzip_file.close()
247 rp_content = gzip_buffer.getvalue()
248 response.content_encoding = 'gzip'
249 except Exception: 244 except Exception:
250 logger.exception("Error compressing response, " 245 logger.exception("Error compressing response, "
251 "falling back to uncompressed.") 246 "falling back to uncompressed.")
252 rp_content = rendered_page.content
253 response.set_data(rp_content) 247 response.set_data(rp_content)
254 248
255 return response 249 return response
256 250
257 def _handle_error(self, exception, environ, start_response): 251 def _handle_error(self, exception, environ, start_response):