| 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_fetcher import TemplateFetcher |
| 25 from template_utility import TemplateUtility |
| 13 | 26 |
| 14 EXTENSIONS_PATH = 'src/chrome/common/extensions/docs/' | 27 EXTENSIONS_PATH = 'chrome/common/extensions/' |
| 15 | 28 DOCS_PATH = 'docs/' |
| 29 STATIC_PATH = DOCS_PATH + 'static/' |
| 30 PUBLIC_TEMPLATE_PATH = DOCS_PATH + 'template2/public/' |
| 31 PRIVATE_TEMPLATE_PATH = DOCS_PATH + 'template2/private/' |
| 16 | 32 |
| 17 class MainPage(webapp.RequestHandler): | 33 class MainPage(webapp.RequestHandler): |
| 34 def _FetchResource(self, fetcher, branch, path): |
| 35 result = fetcher.FetchResource(branch, path) |
| 36 for key in result.headers: |
| 37 self.response.headers[key] = result.headers[key] |
| 38 return result.content |
| 39 |
| 18 def get(self): | 40 def get(self): |
| 19 path = self.request.path.replace('/chrome/extensions/', '') | 41 path = self.request.path |
| 20 | 42 path = path.replace('/chrome/extensions/', '') |
| 21 parts = path.split('/') | 43 parts = path.split('/') |
| 22 if len(parts) > 1: | 44 filename = parts[-1] |
| 23 filename = parts[1] | |
| 24 else: | |
| 25 filename = parts[0] | |
| 26 | 45 |
| 27 if len(path) > 0 and path[0] == '/': | 46 if len(path) > 0 and path[0] == '/': |
| 28 path = path.strip('/') | 47 path = path.strip('/') |
| 29 | 48 |
| 30 fetcher = SubversionFetcher(urlfetch) | 49 branch_util = BranchUtility(urlfetch) |
| 31 b_util = BranchUtility(urlfetch) | 50 channel_name = branch_util.GetChannelNameFromPath(path) |
| 32 channel_name = b_util.GetChannelNameFromPath(path) | 51 branch = branch_util.GetBranchNumberForChannelName(channel_name) |
| 33 branch = b_util.GetBranchNumberForChannelName(channel_name) | |
| 34 | 52 |
| 35 logging.info(channel_name + ' ' + branch) | 53 if channel_name == 'local': |
| 36 try: | 54 fetcher = LocalFetcher(EXTENSIONS_PATH) |
| 37 result = fetcher.FetchResource(branch, EXTENSIONS_PATH + filename) | 55 else: |
| 38 content = result.content | 56 fetcher = SubversionFetcher(EXTENSIONS_PATH, urlfetch) |
| 39 for key in result.headers: | 57 template_fetcher = TemplateFetcher(branch, fetcher) |
| 40 self.response.headers[key] = result.headers[key] | 58 template_util = TemplateUtility() |
| 41 except: | 59 |
| 42 content = 'File not found.' | 60 template = template_fetcher[PUBLIC_TEMPLATE_PATH + filename] |
| 61 if template: |
| 62 content = template_util.RenderTemplate(template, '{"test": "Hello"}') |
| 63 else: |
| 64 logging.info('Template for %s not found.' % filename) |
| 65 |
| 66 try: |
| 67 content = self._FetchResource(fetcher, branch, STATIC_PATH + filename) |
| 68 except: |
| 69 content = 'File not found.' |
| 43 | 70 |
| 44 self.response.out.write(content) | 71 self.response.out.write(content) |
| 45 | 72 |
| 46 application = webapp.WSGIApplication([ | 73 application = webapp.WSGIApplication([ |
| 47 ('/.*', MainPage), | 74 ('/.*', MainPage), |
| 48 ], debug=False) | 75 ], debug=False) |
| 49 | 76 |
| 50 | |
| 51 def main(): | 77 def main(): |
| 52 run_wsgi_app(application) | 78 run_wsgi_app(application) |
| 53 | 79 |
| 54 | 80 |
| 55 if __name__ == '__main__': | 81 if __name__ == '__main__': |
| 56 main() | 82 main() |
| OLD | NEW |