local_renderer.py revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
1# Copyright 2013 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import os 6import sys 7 8from render_servlet import RenderServlet 9from server_instance import ServerInstance 10from servlet import Request 11 12class _LocalRenderServletDelegate(object): 13 def CreateServerInstanceForChannel(self, channel): 14 return ServerInstance.ForLocal() 15 16class LocalRenderer(object): 17 '''Renders pages fetched from the local file system. 18 ''' 19 @staticmethod 20 def Render(path): 21 assert not '\\' in path 22 def render_path(path): 23 return RenderServlet(Request(path, 'http://localhost', {}), 24 _LocalRenderServletDelegate(), 25 default_channel='trunk').Get() 26 response = render_path(path) 27 while response.status in [301, 302]: 28 redirect = response.headers['Location'] 29 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect)) 30 response = render_path(redirect) 31 return response 32