OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 # This helps you preview the apps and extensions docs. | 6 # This helps you preview the apps and extensions docs. |
7 # | 7 # |
8 # ./preview.py --help | 8 # ./preview.py --help |
9 # | 9 # |
10 # There are two modes: server- and render- mode. The default is server, in which | 10 # There are two modes: server- and render- mode. The default is server, in which |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 | 50 |
51 def set_status(self, status): | 51 def set_status(self, status): |
52 self.status = status | 52 self.status = status |
53 | 53 |
54 class _Request(object): | 54 class _Request(object): |
55 def __init__(self, path): | 55 def __init__(self, path): |
56 self.headers = {} | 56 self.headers = {} |
57 self.path = path | 57 self.path = path |
58 self.url = 'http://localhost' + path | 58 self.url = 'http://localhost' + path |
59 | 59 |
60 def _Render(path, local_path): | 60 def _Render(path): |
61 response = _Response() | 61 response = _Response() |
62 Handler(_Request(urlparse.urlparse(path).path), | 62 Handler(_Request(urlparse.urlparse(path).path), response).get() |
63 response, | |
64 local_path=local_path).get() | |
65 content = response.out.getvalue() | 63 content = response.out.getvalue() |
66 if isinstance(content, unicode): | 64 if isinstance(content, unicode): |
67 content = content.encode('utf-8', 'replace') | 65 content = content.encode('utf-8', 'replace') |
68 return (content, response.status, response.headers) | 66 return (content, response.status, response.headers) |
69 | 67 |
70 def _GetLocalPath(): | 68 def _GetLocalPath(): |
71 if os.sep in sys.argv[0]: | 69 if os.sep in sys.argv[0]: |
72 return os.path.join(sys.argv[0].rsplit(os.sep, 1)[0], os.pardir, os.pardir) | 70 return os.path.join(sys.argv[0].rsplit(os.sep, 1)[0], os.pardir, os.pardir) |
73 return os.path.join(os.pardir, os.pardir) | 71 return os.path.join(os.pardir, os.pardir) |
74 | 72 |
75 class RequestHandler(BaseHTTPRequestHandler): | 73 class RequestHandler(BaseHTTPRequestHandler): |
76 """A HTTPRequestHandler that outputs the docs page generated by Handler. | 74 """A HTTPRequestHandler that outputs the docs page generated by Handler. |
77 """ | 75 """ |
78 def do_GET(self): | 76 def do_GET(self): |
79 content, status, headers = _Render(self.path, RequestHandler.local_path) | 77 content, status, headers = _Render(self.path) |
80 self.send_response(status) | 78 self.send_response(status) |
81 for k, v in headers.iteritems(): | 79 for k, v in headers.iteritems(): |
82 self.send_header(k, v) | 80 self.send_header(k, v) |
83 self.end_headers() | 81 self.end_headers() |
84 self.wfile.write(content) | 82 self.wfile.write(content) |
85 | 83 |
86 if __name__ == '__main__': | 84 if __name__ == '__main__': |
87 parser = optparse.OptionParser( | 85 parser = optparse.OptionParser( |
88 description='Runs a server to preview the extension documentation.', | 86 description='Runs a server to preview the extension documentation.', |
89 usage='usage: %prog [option]...') | 87 usage='usage: %prog [option]...') |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 print('') | 139 print('') |
142 print('The extension documentation can be found at:') | 140 print('The extension documentation can be found at:') |
143 print('') | 141 print('') |
144 print(' http://localhost:%s/extensions/' % opts.port) | 142 print(' http://localhost:%s/extensions/' % opts.port) |
145 print('') | 143 print('') |
146 print('The apps documentation can be found at:') | 144 print('The apps documentation can be found at:') |
147 print('') | 145 print('') |
148 print(' http://localhost:%s/apps/' % opts.port) | 146 print(' http://localhost:%s/apps/' % opts.port) |
149 print('') | 147 print('') |
150 | 148 |
151 RequestHandler.local_path = opts.directory | |
152 server = HTTPServer(('', int(opts.port)), RequestHandler) | 149 server = HTTPServer(('', int(opts.port)), RequestHandler) |
153 try: | 150 try: |
154 server.serve_forever() | 151 server.serve_forever() |
155 finally: | 152 finally: |
156 server.socket.close() | 153 server.socket.close() |
OLD | NEW |