| 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 from handler import Handler | |
| 6 from fake_fetchers import ConfigureFakeFetchers | |
| 7 import os | 5 import os |
| 8 from StringIO import StringIO | |
| 9 import urlparse | 6 import urlparse |
| 10 | 7 |
| 11 class _Request(object): | 8 from appengine_wrappers import FakeRequest, FakeResponse |
| 12 def __init__(self, path, headers): | 9 from handler import Handler |
| 13 self.path = path | 10 from render_servlet import RenderServlet |
| 14 self.url = 'http://localhost/%s' % path | 11 from fake_fetchers import ConfigureFakeFetchers |
| 15 self.headers = headers | |
| 16 | |
| 17 class _Response(object): | |
| 18 def __init__(self): | |
| 19 self.status = 200 | |
| 20 self.out = StringIO() | |
| 21 self.headers = {} | |
| 22 | |
| 23 def set_status(self, status): | |
| 24 self.status = status | |
| 25 | 12 |
| 26 class LocalRenderer(object): | 13 class LocalRenderer(object): |
| 27 '''Renders pages fetched from the local file system. | 14 '''Renders pages fetched from the local file system. |
| 28 ''' | 15 ''' |
| 29 def __init__(self, base_dir): | 16 def __init__(self, base_dir): |
| 30 self._base_dir = base_dir.replace(os.sep, '/').rstrip('/') | 17 self._base_dir = base_dir.replace(os.sep, '/').rstrip('/') |
| 31 | 18 |
| 32 def Render(self, path, headers={}, always_online=False): | 19 def Render(self, path, headers={}): |
| 33 '''Renders |path|, returning a tuple of (status, contents, headers). | 20 '''Renders |path|, returning a tuple of (status, contents, headers). |
| 34 ''' | 21 ''' |
| 35 # TODO(kalman): do this via a LocalFileSystem not this fake AppEngine stuff. | 22 # TODO(kalman): do this via a LocalFileSystem not this fake AppEngine stuff. |
| 36 ConfigureFakeFetchers(os.path.join(self._base_dir, 'docs')) | 23 ConfigureFakeFetchers(os.path.join(self._base_dir, 'docs')) |
| 37 handler_was_always_online = Handler.ALWAYS_ONLINE | 24 response = FakeResponse() |
| 38 Handler.ALWAYS_ONLINE = always_online | 25 url_path = urlparse.urlparse(path.replace(os.sep, '/')).path |
| 39 try: | 26 Handler(FakeRequest(url_path, headers), response).get() |
| 40 response = _Response() | 27 content = response.out.getvalue() |
| 41 url_path = urlparse.urlparse(path.replace(os.sep, '/')).path | 28 if isinstance(content, unicode): |
| 42 Handler(_Request(url_path, headers), response).get() | 29 content = content.encode('utf-8', 'replace') |
| 43 content = response.out.getvalue() | 30 return (content, response.status, response.headers) |
| 44 if isinstance(content, unicode): | |
| 45 content = content.encode('utf-8', 'replace') | |
| 46 return (content, response.status, response.headers) | |
| 47 finally: | |
| 48 Handler.ALWAYS_ONLINE = handler_was_always_online | |
| OLD | NEW |