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 |
11 from branch_utility import BranchUtility | 20 from branch_utility import BranchUtility |
12 from resource_fetcher import SubversionFetcher | 21 from resource_fetcher import SubversionFetcher, LocalFetcher |
22 from template_utility import TemplateUtility | |
13 | 23 |
14 EXTENSIONS_PATH = 'src/chrome/common/extensions/docs/' | 24 DOCS_PATH = 'chrome/common/extensions/docs/' |
Aaron Boodman
2012/06/02 07:50:22
os.path.join() ... all throughout this code.
cduvall
2012/06/02 19:12:05
So these paths can be used as URLs or as file path
Aaron Boodman
2012/06/03 05:43:14
I see. When you use it as a path, can you split by
cduvall
2012/06/04 21:53:25
Done.
| |
15 | 25 STATIC_PATH = DOCS_PATH + 'static/' |
26 PUBLIC_TEMPLATE_PATH = DOCS_PATH + 'template2/public/' | |
27 PRIVATE_TEMPLATE_PATH = DOCS_PATH + 'template2/private/' | |
16 | 28 |
17 class MainPage(webapp.RequestHandler): | 29 class MainPage(webapp.RequestHandler): |
30 def _FetchResource(self, fetcher, branch, path): | |
31 result = fetcher.FetchResource(branch, path) | |
32 for key in result.headers: | |
33 self.response.headers[key] = result.headers[key] | |
34 return result.content | |
35 | |
18 def get(self): | 36 def get(self): |
19 path = self.request.path.replace('/chrome/extensions/', '') | 37 path = self.request.path |
20 | 38 |
39 # Hack for getting local files. Still researching how to use a command line | |
40 # argument to toggle this. | |
Aaron Boodman
2012/06/02 07:50:22
They don't show up in sys.argv?
cduvall
2012/06/02 19:12:05
Nope, since dev_appserver.py calls this script sep
| |
41 if path.startswith('/local'): | |
42 logging.info(path) | |
43 path = path.replace('/local', '') | |
44 fetcher = LocalFetcher() | |
45 else: | |
46 fetcher = SubversionFetcher(urlfetch) | |
47 branch_util = BranchUtility(urlfetch) | |
48 template_util = TemplateUtility() | |
49 | |
50 path = path.replace('/chrome/extensions/', '') | |
21 parts = path.split('/') | 51 parts = path.split('/') |
22 if len(parts) > 1: | 52 if len(parts) > 1: |
23 filename = parts[1] | 53 filename = parts[1] |
24 else: | 54 else: |
25 filename = parts[0] | 55 filename = parts[0] |
26 | 56 |
27 if len(path) > 0 and path[0] == '/': | 57 if len(path) > 0 and path[0] == '/': |
28 path = path.strip('/') | 58 path = path.strip('/') |
29 | 59 |
30 fetcher = SubversionFetcher(urlfetch) | 60 channel_name = branch_util.GetChannelNameFromPath(path) |
31 b_util = BranchUtility(urlfetch) | 61 branch = branch_util.GetBranchNumberForChannelName(channel_name) |
32 channel_name = b_util.GetChannelNameFromPath(path) | |
33 branch = b_util.GetBranchNumberForChannelName(channel_name) | |
34 | 62 |
35 logging.info(channel_name + ' ' + branch) | |
36 try: | 63 try: |
37 result = fetcher.FetchResource(branch, EXTENSIONS_PATH + filename) | 64 content = self._FetchResource(fetcher, branch, |
38 content = result.content | 65 PUBLIC_TEMPLATE_PATH + filename) |
39 for key in result.headers: | 66 content = template_util.RenderTemplate('{"test": "Hello"}', content) |
40 self.response.headers[key] = result.headers[key] | |
41 except: | 67 except: |
42 content = 'File not found.' | 68 logging.info('Template for %s not found.' % filename) |
69 | |
70 try: | |
71 content = self._FetchResource(fetcher, branch, STATIC_PATH + filename) | |
72 except: | |
73 content = 'File not found.' | |
43 | 74 |
44 self.response.out.write(content) | 75 self.response.out.write(content) |
45 | 76 |
46 application = webapp.WSGIApplication([ | 77 application = webapp.WSGIApplication([ |
47 ('/.*', MainPage), | 78 ('/.*', MainPage), |
48 ], debug=False) | 79 ], debug=False) |
49 | 80 |
50 | |
51 def main(): | 81 def main(): |
52 run_wsgi_app(application) | 82 run_wsgi_app(application) |
53 | 83 |
54 | 84 |
55 if __name__ == '__main__': | 85 if __name__ == '__main__': |
56 main() | 86 main() |
OLD | NEW |