def delegate_error_to_simplate(website, state, response, request=None, resource=None): if request is None: return # early parsing must've failed if response.code < 400: return code = str(response.code) possibles = [code + ".spt", "error.spt"] fspath = _first(website.ours_or_theirs(errpage) for errpage in possibles) if fspath is not None: request.original_resource = resource resource = resources.get(website.request_processor, fspath) state['dispatch_result'] = DispatchResult(DispatchStatus.okay, fspath, {}, 'Found.', {}, True) # Try to return an error that matches the type of the response the # client would have received if the error didn't occur wanted = getattr(state.get('output'), 'media_type', None) or '' # If we don't have a media type (e.g. when we're returning a 404), then # we fall back to the Accept header wanted += ',' + (state.get('accept_header') or '') # As a last resort we accept anything, with a preference for text/plain wanted += ',text/plain;q=0.2,*/*;q=0.1' state['accept_header'] = wanted.lstrip(',') render_response(state, resource, response, website.request_processor)
def _render(response, path, state, **extra): state.update(extra) if 'dispatch_result' not in state: state['dispatch_result'] = DispatchResult(DispatchStatus.okay, path, None, None, None) website = state['website'] resource = website.request_processor.resources.get(path) render_response(state, resource, response, website) raise response
def _render(response, path, state, **extra): state.update(extra) if 'dispatch_result' not in state: state['dispatch_result'] = DispatchResult(DispatchStatus.okay, path, {}, "Response.render()", {}, False) request_processor = state['request_processor'] output = aspen.resources.get(request_processor, path).render(state) fill_response_with_output(output, response, request_processor) raise response
def handle_dispatch_exception(website, exception): if isinstance(exception, Redirect): website.redirect(exception.message) elif isinstance(exception, UnindexedDirectory) and website.list_directories: autoindex_spt = website.ours_or_theirs('autoindex.html.spt') dispatch_result = DispatchResult(DispatchStatus.okay, autoindex_spt, {}, 'Directory autoindex.', {'autoindexdir': exception.message}, False) return {'dispatch_result': dispatch_result, 'exception': None} elif isinstance(exception, NotFound): raise Response(404)
def compile_assets(website): cleanup = [] for spt in find_files(website.www_root+'/assets/', '*.spt'): filepath = spt[:-4] # /path/to/www/assets/foo.css if not os.path.exists(filepath): cleanup.append(filepath) dispatch_result = DispatchResult(DispatchStatus.okay, spt, {}, "Found.", {}, True) state = dict(dispatch_result=dispatch_result, response=Response()) state['state'] = state content = resources.get(website.request_processor, spt).render(state).body if not isinstance(content, bytes): content = content.encode('utf8') tmpfd, tmpfpath = mkstemp(dir='.') os.write(tmpfd, content) os.close(tmpfd) os.rename(tmpfpath, filepath) if website.env.clean_assets: atexit.register(lambda: rm_f(*cleanup))