| 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 import sys |
| 7 import os |
| 8 |
| 9 # Add the original server location to sys.path so we are able to import |
| 10 # modules from there. |
| 11 SERVER_PATH = 'chrome/common/extensions/docs/server2/' |
| 12 if os.path.abspath(SERVER_PATH) not in sys.path: |
| 13 sys.path.append(os.path.abspath(SERVER_PATH)) |
| 14 |
| 6 import logging | 15 import logging |
| 7 import urlfetch | 16 import urlfetch |
| 8 | 17 |
| 9 from google.appengine.ext import webapp | 18 from google.appengine.ext import webapp |
| 10 from google.appengine.ext.webapp.util import run_wsgi_app | 19 from google.appengine.ext.webapp.util import run_wsgi_app |
| 20 |
| 11 from branch_utility import BranchUtility | 21 from branch_utility import BranchUtility |
| 12 from resource_fetcher import SubversionFetcher | 22 from local_fetcher import LocalFetcher |
| 23 from subversion_fetcher import SubversionFetcher |
| 24 from template_utility import TemplateUtility |
| 13 | 25 |
| 14 EXTENSIONS_PATH = 'src/chrome/common/extensions/docs/' | 26 EXTENSIONS_PATH = 'chrome/common/extensions/' |
| 15 | 27 DOCS_PATH = 'docs/' |
| 28 STATIC_PATH = DOCS_PATH + 'static/' |
| 29 PUBLIC_TEMPLATE_PATH = DOCS_PATH + 'template2/public/' |
| 30 PRIVATE_TEMPLATE_PATH = DOCS_PATH + 'template2/private/' |
| 16 | 31 |
| 17 class MainPage(webapp.RequestHandler): | 32 class MainPage(webapp.RequestHandler): |
| 33 def _FetchResource(self, fetcher, branch, path): |
| 34 result = fetcher.FetchResource(branch, path) |
| 35 for key in result.headers: |
| 36 self.response.headers[key] = result.headers[key] |
| 37 return result.content |
| 38 |
| 18 def get(self): | 39 def get(self): |
| 19 path = self.request.path.replace('/chrome/extensions/', '') | 40 path = self.request.path |
| 20 | 41 |
| 42 # Hack for getting local files. Still researching how to use a command line |
| 43 # argument to toggle this. |
| 44 if path.startswith('/local'): |
| 45 logging.info(path) |
| 46 path = path.replace('/local', '') |
| 47 fetcher = LocalFetcher(EXTENSIONS_PATH) |
| 48 else: |
| 49 fetcher = SubversionFetcher(EXTENSIONS_PATH, urlfetch) |
| 50 branch_util = BranchUtility(urlfetch) |
| 51 template_util = TemplateUtility() |
| 52 |
| 53 path = path.replace('/chrome/extensions/', '') |
| 21 parts = path.split('/') | 54 parts = path.split('/') |
| 22 if len(parts) > 1: | 55 if len(parts) > 1: |
| 23 filename = parts[1] | 56 filename = parts[1] |
| 24 else: | 57 else: |
| 25 filename = parts[0] | 58 filename = parts[0] |
| 26 | 59 |
| 27 if len(path) > 0 and path[0] == '/': | 60 if len(path) > 0 and path[0] == '/': |
| 28 path = path.strip('/') | 61 path = path.strip('/') |
| 29 | 62 |
| 30 fetcher = SubversionFetcher(urlfetch) | 63 channel_name = branch_util.GetChannelNameFromPath(path) |
| 31 b_util = BranchUtility(urlfetch) | 64 branch = branch_util.GetBranchNumberForChannelName(channel_name) |
| 32 channel_name = b_util.GetChannelNameFromPath(path) | |
| 33 branch = b_util.GetBranchNumberForChannelName(channel_name) | |
| 34 | 65 |
| 35 logging.info(channel_name + ' ' + branch) | |
| 36 try: | 66 try: |
| 37 result = fetcher.FetchResource(branch, EXTENSIONS_PATH + filename) | 67 content = self._FetchResource(fetcher, branch, |
| 38 content = result.content | 68 PUBLIC_TEMPLATE_PATH + filename) |
| 39 for key in result.headers: | 69 content = template_util.RenderTemplate(content, '{"test": "Hello"}') |
| 40 self.response.headers[key] = result.headers[key] | |
| 41 except: | 70 except: |
| 42 content = 'File not found.' | 71 logging.info('Template for %s not found.' % filename) |
| 72 |
| 73 try: |
| 74 content = self._FetchResource(fetcher, branch, STATIC_PATH + filename) |
| 75 except: |
| 76 content = 'File not found.' |
| 43 | 77 |
| 44 self.response.out.write(content) | 78 self.response.out.write(content) |
| 45 | 79 |
| 46 application = webapp.WSGIApplication([ | 80 application = webapp.WSGIApplication([ |
| 47 ('/.*', MainPage), | 81 ('/.*', MainPage), |
| 48 ], debug=False) | 82 ], debug=False) |
| 49 | 83 |
| 50 | |
| 51 def main(): | 84 def main(): |
| 52 run_wsgi_app(application) | 85 run_wsgi_app(application) |
| 53 | 86 |
| 54 | 87 |
| 55 if __name__ == '__main__': | 88 if __name__ == '__main__': |
| 56 main() | 89 main() |
| OLD | NEW |