OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | 5 import sys |
6 import urlparse | |
7 | 6 |
8 from render_servlet import RenderServlet | 7 from render_servlet import RenderServlet |
9 from fake_fetchers import ConfigureFakeFetchers | 8 from server_instance import ServerInstance |
10 from servlet import Request | 9 from servlet import Request |
11 | 10 |
| 11 class _LocalRenderServletDelegate(object): |
| 12 def CreateServerInstanceForChannel(self, channel): |
| 13 return ServerInstance.ForLocal() |
| 14 |
12 class LocalRenderer(object): | 15 class LocalRenderer(object): |
13 '''Renders pages fetched from the local file system. | 16 '''Renders pages fetched from the local file system. |
14 ''' | 17 ''' |
15 def __init__(self, base_dir): | 18 @staticmethod |
16 self._base_dir = base_dir.replace(os.sep, '/').rstrip('/') | 19 def Render(path): |
17 | 20 def render_path(path): |
18 def Render(self, path, headers=None, servlet=RenderServlet): | 21 return RenderServlet(Request(path, 'http://localhost', {}), |
19 '''Renders |path|, returning a tuple of (status, contents, headers). | 22 _LocalRenderServletDelegate(), |
20 ''' | 23 default_channel='trunk').Get() |
21 headers = headers or {} | 24 response = render_path(path) |
22 # TODO(kalman): do this via a LocalFileSystem not this fake AppEngine stuff. | 25 while response.status in [301, 302]: |
23 ConfigureFakeFetchers(os.path.join(self._base_dir, 'docs')) | 26 redirect = response.headers['Location'] |
24 url_path = urlparse.urlparse(path.replace(os.sep, '/')).path | 27 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect)) |
25 return servlet(Request(url_path, headers)).Get() | 28 response = render_path(redirect) |
| 29 return response |
OLD | NEW |