local_renderer.py revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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 sys
6
7from render_servlet import RenderServlet
8from server_instance import ServerInstance
9from servlet import Request
10
11class _LocalRenderServletDelegate(object):
12  def CreateServerInstanceForChannel(self, channel):
13    return ServerInstance.ForLocal()
14
15class LocalRenderer(object):
16  '''Renders pages fetched from the local file system.
17  '''
18  @staticmethod
19  def Render(path):
20    def render_path(path):
21      return RenderServlet(Request(path, 'http://localhost', {}),
22                           _LocalRenderServletDelegate(),
23                           default_channel='trunk').Get()
24    response = render_path(path)
25    while response.status in [301, 302]:
26      redirect = response.headers['Location']
27      sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect))
28      response = render_path(redirect)
29    return response
30