OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 # This is a preview server for the apps and extensions docs. Navigate to a docs |
| 7 # page, and the page will be rendered how it will be on the production server. |
| 8 # |
| 9 # For example: http://localhost:8000/tabs.html will render the docs page for |
| 10 # the tabs API. |
| 11 # |
| 12 # Run with: './preview.py' |
| 13 |
| 14 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
| 15 import optparse |
| 16 import os |
| 17 from StringIO import StringIO |
| 18 import sys |
| 19 import urlparse |
| 20 |
| 21 from handler import Handler |
| 22 |
| 23 class Response(object): |
| 24 def __init__(self): |
| 25 self.status = 200 |
| 26 self.out = StringIO() |
| 27 self.headers = {} |
| 28 |
| 29 def set_status(self, status): |
| 30 self.status = status |
| 31 |
| 32 class Request(object): |
| 33 def __init__(self, path): |
| 34 self.headers = {} |
| 35 self.path = path |
| 36 |
| 37 def _GetLocalPath(): |
| 38 return os.path.join(sys.argv[0].rsplit('/', 1)[0], os.pardir, os.pardir) |
| 39 |
| 40 class RequestHandler(BaseHTTPRequestHandler): |
| 41 """A HTTPRequestHandler that outputs the docs page generated by Handler. |
| 42 """ |
| 43 def do_GET(self): |
| 44 parsed_url = urlparse.urlparse(self.path) |
| 45 request = Request(parsed_url.path) |
| 46 response = Response() |
| 47 Handler(request, response, local_path=_GetLocalPath()).get() |
| 48 content = response.out.getvalue() |
| 49 if isinstance(content, str): |
| 50 self.wfile.write(content) |
| 51 else: |
| 52 self.wfile.write(content.encode('utf-8', 'replace')) |
| 53 |
| 54 if __name__ == '__main__': |
| 55 parser = optparse.OptionParser( |
| 56 description='Runs a server to preview the extension documentation.', |
| 57 usage='usage: %prog [option]...') |
| 58 parser.add_option('-p', '--port', default=8000, |
| 59 help='port to run the server on') |
| 60 |
| 61 (opts, argv) = parser.parse_args() |
| 62 |
| 63 try: |
| 64 print('Starting previewserver on port %d' % opts.port) |
| 65 print('The extension documentation can be found at:') |
| 66 print('') |
| 67 print(' http://localhost:%d' % opts.port) |
| 68 print('') |
| 69 |
| 70 server = HTTPServer(('', int(opts.port)), RequestHandler) |
| 71 server.serve_forever() |
| 72 except KeyboardInterrupt: |
| 73 pass |
| 74 finally: |
| 75 server.socket.close() |
OLD | NEW |