Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: chrome/common/extensions/docs/server2/local_renderer.py

Issue 14273035: Docserver: refactor the Handler/ServerInstance relationship into a servlet (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: done Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 render_servlet import RenderServlet
12 def __init__(self, path, headers): 9 from fake_fetchers import ConfigureFakeFetchers
13 self.path = path 10 from servlet import Request
14 self.url = 'http://localhost/%s' % path
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 11
26 class LocalRenderer(object): 12 class LocalRenderer(object):
27 '''Renders pages fetched from the local file system. 13 '''Renders pages fetched from the local file system.
28 ''' 14 '''
29 def __init__(self, base_dir): 15 def __init__(self, base_dir):
30 self._base_dir = base_dir.replace(os.sep, '/').rstrip('/') 16 self._base_dir = base_dir.replace(os.sep, '/').rstrip('/')
31 17
32 def Render(self, path, headers={}, always_online=False): 18 def Render(self, path, headers=None, servlet=RenderServlet):
33 '''Renders |path|, returning a tuple of (status, contents, headers). 19 '''Renders |path|, returning a tuple of (status, contents, headers).
34 ''' 20 '''
21 headers = headers or {}
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 url_path = urlparse.urlparse(path.replace(os.sep, '/')).path
38 Handler.ALWAYS_ONLINE = always_online 25 return servlet(Request(url_path, headers)).Get()
39 try:
40 response = _Response()
41 url_path = urlparse.urlparse(path.replace(os.sep, '/')).path
42 Handler(_Request(url_path, headers), response).get()
43 content = response.out.getvalue()
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
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/integration_test.py ('k') | chrome/common/extensions/docs/server2/preview.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698