OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
Aaron Boodman
2012/06/06 23:49:59
Is the name 'echo_handler.py' still correct for th
| |
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 | 6 import sys |
7 import os | 7 import os |
8 | 8 |
9 # Add the original server location to sys.path so we are able to import | 9 # Add the original server location to sys.path so we are able to import |
10 # modules from there. | 10 # modules from there. |
11 SERVER_PATH = 'chrome/common/extensions/docs/server2/' | 11 SERVER_PATH = 'chrome/common/extensions/docs/server2/' |
12 if os.path.abspath(SERVER_PATH) not in sys.path: | 12 if os.path.abspath(SERVER_PATH) not in sys.path: |
13 sys.path.append(os.path.abspath(SERVER_PATH)) | 13 sys.path.append(os.path.abspath(SERVER_PATH)) |
14 | 14 |
15 import logging | 15 import logging |
16 import urlfetch | 16 import urlfetch |
17 | 17 |
18 from google.appengine.ext import webapp | 18 from google.appengine.ext import webapp |
19 from google.appengine.ext.webapp.util import run_wsgi_app | 19 from google.appengine.ext.webapp.util import run_wsgi_app |
20 | 20 |
21 from branch_utility import BranchUtility | 21 from branch_utility import BranchUtility |
22 from local_fetcher import LocalFetcher | 22 from local_fetcher import LocalFetcher |
23 from subversion_fetcher import SubversionFetcher | 23 from subversion_fetcher import SubversionFetcher |
24 from template_fetcher import TemplateFetcher | 24 from template_data_source import TemplateDataSource |
25 from template_utility import TemplateUtility | 25 from template_utility import TemplateUtility |
26 | 26 |
27 EXTENSIONS_PATH = 'chrome/common/extensions/' | 27 EXTENSIONS_PATH = 'chrome/common/extensions/' |
28 DOCS_PATH = 'docs/' | 28 DOCS_PATH = 'docs/' |
29 STATIC_PATH = DOCS_PATH + 'static/' | 29 STATIC_PATH = DOCS_PATH + 'static/' |
30 PUBLIC_TEMPLATE_PATH = DOCS_PATH + 'template2/public/' | 30 PUBLIC_TEMPLATE_PATH = DOCS_PATH + 'template2/public/' |
31 PRIVATE_TEMPLATE_PATH = DOCS_PATH + 'template2/private/' | 31 PRIVATE_TEMPLATE_PATH = DOCS_PATH + 'template2/private/' |
32 | 32 |
33 class MainPage(webapp.RequestHandler): | 33 # Global cache of instances because the Server is recreated for every request. |
34 def _FetchResource(self, fetcher, branch, path): | 34 SERVER_INSTANCES = {} |
35 result = fetcher.FetchResource(branch, path) | 35 |
36 for key in result.headers: | 36 class Instance(object): |
Aaron Boodman
2012/06/06 23:49:59
Consider moving this into its own class. Easier to
not at google - send to devlin
2012/06/07 04:43:57
Yes please. Might make sense to rename ServerInsta
cduvall
2012/06/08 00:39:23
Done.
| |
37 self.response.headers[key] = result.headers[key] | 37 def __init__(self, data_source, fetcher): |
38 return result.content | 38 self._data_source = data_source |
39 self._fetcher = fetcher | |
40 self._template_util = TemplateUtility() | |
not at google - send to devlin
2012/06/07 04:43:57
Usually it doesn't make sense to instantiate util
cduvall
2012/06/08 00:39:23
Done.
| |
41 | |
42 def Run(self, path, request_handler): | |
43 parts = path.split('/') | |
44 filename = parts[-1] | |
45 template = self._data_source[filename] | |
46 if template: | |
47 content = self._template_util.RenderTemplate(template, | |
48 '{"test": "Hello"}', self._data_source) | |
49 else: | |
50 logging.info('Template not found for: ' + filename) | |
51 try: | |
52 result = self._fetcher.FetchResource(STATIC_PATH + filename) | |
53 for key in result.headers: | |
54 request_handler.response.headers[key] = result.headers[key] | |
55 content = result.content | |
56 except: | |
57 # TODO should be an actual not found page. | |
58 content = 'File not found.' | |
59 request_handler.response.out.write(content) | |
60 | |
61 | |
62 class Server(webapp.RequestHandler): | |
63 def __init__(self): | |
64 self._branch_util = BranchUtility(urlfetch) | |
65 | |
66 def _GetInstanceForBranch(self, branch): | |
67 if branch in SERVER_INSTANCES: | |
68 return SERVER_INSTANCES[branch] | |
69 if branch == 'local': | |
70 fetcher = LocalFetcher(EXTENSIONS_PATH) | |
71 # No cache for local doc server. | |
72 cache_timeout = 0 | |
73 else: | |
74 fetcher = SubversionFetcher(branch, EXTENSIONS_PATH, urlfetch) | |
75 cache_timeout = 300 | |
not at google - send to devlin
2012/06/07 04:43:57
nit: cache_timeout_seconds
cduvall
2012/06/08 00:39:23
Done.
| |
76 data_source = TemplateDataSource(fetcher, | |
77 [PUBLIC_TEMPLATE_PATH, PRIVATE_TEMPLATE_PATH], cache_timeout) | |
not at google - send to devlin
2012/06/07 04:43:57
nit: parameters all on same line, or all on vertic
cduvall
2012/06/08 00:39:23
Done.
| |
78 SERVER_INSTANCES[branch] = Instance(data_source, fetcher) | |
79 return SERVER_INSTANCES[branch] | |
80 | |
81 def _HandleRequest(self, path): | |
82 channel_name = self._branch_util.GetChannelNameFromPath(path) | |
not at google - send to devlin
2012/06/07 04:43:57
ditto comment about instantiating util classes.
cduvall
2012/06/08 00:39:23
Done.
| |
83 branch = self._branch_util.GetBranchNumberForChannelName(channel_name) | |
84 | |
85 instance = self._GetInstanceForBranch(branch) | |
86 instance.Run(path, self) | |
not at google - send to devlin
2012/06/07 04:43:57
nit: can inline instance
cduvall
2012/06/08 00:39:23
Done.
| |
39 | 87 |
40 def get(self): | 88 def get(self): |
41 path = self.request.path | 89 path = self.request.path |
42 path = path.replace('/chrome/extensions/', '') | 90 path = path.replace('/chrome/extensions/', '') |
43 parts = path.split('/') | |
44 filename = parts[-1] | |
45 | |
46 if len(path) > 0 and path[0] == '/': | 91 if len(path) > 0 and path[0] == '/': |
47 path = path.strip('/') | 92 path = path.strip('/') |
48 | 93 self._HandleRequest(path) |
49 branch_util = BranchUtility(urlfetch) | |
50 channel_name = branch_util.GetChannelNameFromPath(path) | |
51 branch = branch_util.GetBranchNumberForChannelName(channel_name) | |
52 | |
53 if channel_name == 'local': | |
54 fetcher = LocalFetcher(EXTENSIONS_PATH) | |
55 else: | |
56 fetcher = SubversionFetcher(EXTENSIONS_PATH, urlfetch) | |
57 template_fetcher = TemplateFetcher(branch, fetcher) | |
58 template_util = TemplateUtility() | |
59 | |
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.' | |
70 | |
71 self.response.out.write(content) | |
72 | 94 |
73 application = webapp.WSGIApplication([ | 95 application = webapp.WSGIApplication([ |
74 ('/.*', MainPage), | 96 ('/.*', Server), |
75 ], debug=False) | 97 ], debug=False) |
76 | 98 |
77 def main(): | 99 def main(): |
78 run_wsgi_app(application) | 100 run_wsgi_app(application) |
not at google - send to devlin
2012/06/07 04:43:57
wondering if this a python or appengine idiom or s
cduvall
2012/06/08 00:39:23
This is just how the old server was set up. I like
| |
79 | 101 |
80 | 102 |
81 if __name__ == '__main__': | 103 if __name__ == '__main__': |
82 main() | 104 main() |
OLD | NEW |