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