Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import json | |
| 5 import logging | 6 import logging |
| 6 import os | 7 import os |
| 7 import traceback | 8 import traceback |
| 8 | 9 |
| 9 from branch_utility import BranchUtility | 10 from branch_utility import BranchUtility |
| 10 import compiled_file_system as compiled_fs | 11 import compiled_file_system as compiled_fs |
| 12 from compiled_file_system_source import CompiledFileSystemSource as CSFSource | |
| 11 from docs_server_utils import FormatKey | 13 from docs_server_utils import FormatKey |
| 12 from file_system import FileNotFoundError | 14 from file_system import FileNotFoundError |
| 15 from svn_constants import JSON_PATH | |
| 13 from third_party.handlebar import Handlebar | 16 from third_party.handlebar import Handlebar |
| 14 import url_constants | 17 import url_constants |
| 15 | 18 |
| 16 EXTENSIONS_URL = '/chrome/extensions' | 19 EXTENSIONS_URL = '/chrome/extensions' |
| 17 | 20 |
| 18 def _MakeChannelDict(channel_name): | 21 def _MakeChannelDict(channel_name): |
| 19 channel_dict = { | 22 channel_dict = { |
| 20 'channels': [{'name': name} for name in BranchUtility.GetAllBranchNames()], | 23 'channels': [{'name': name} for name in BranchUtility.GetAllBranchNames()], |
| 21 'current': channel_name | 24 'current': channel_name |
| 22 } | 25 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 self._api_list_data_source_factory = api_list_data_source_factory | 60 self._api_list_data_source_factory = api_list_data_source_factory |
| 58 self._intro_data_source_factory = intro_data_source_factory | 61 self._intro_data_source_factory = intro_data_source_factory |
| 59 self._samples_data_source_factory = samples_data_source_factory | 62 self._samples_data_source_factory = samples_data_source_factory |
| 60 self._sidenav_data_source_factory = sidenav_data_source_factory | 63 self._sidenav_data_source_factory = sidenav_data_source_factory |
| 61 self._cache = compiled_fs_factory.Create(self._CreateTemplate, | 64 self._cache = compiled_fs_factory.Create(self._CreateTemplate, |
| 62 TemplateDataSource) | 65 TemplateDataSource) |
| 63 self._ref_resolver = ref_resolver_factory.Create() | 66 self._ref_resolver = ref_resolver_factory.Create() |
| 64 self._public_template_path = public_template_path | 67 self._public_template_path = public_template_path |
| 65 self._private_template_path = private_template_path | 68 self._private_template_path = private_template_path |
| 66 self._static_resources = '/%s/static' % channel_name | 69 self._static_resources = '/%s/static' % channel_name |
| 70 self._manifest_json = compiled_fs_factory.Create( | |
| 71 lambda _, contents: json.loads(contents), TemplateDataSource) | |
|
not at google - send to devlin
2013/04/30 16:21:09
Use compiled_fs_factory.CreateIdentity() here.
be
jshumway
2013/05/03 03:44:39
Done.
| |
| 72 | |
| 67 | 73 |
| 68 def _CreateTemplate(self, template_name, text): | 74 def _CreateTemplate(self, template_name, text): |
| 69 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) | 75 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) |
| 70 | 76 |
| 71 def Create(self, request, path): | 77 def Create(self, request, path): |
| 72 """Returns a new TemplateDataSource bound to |request|. | 78 """Returns a new TemplateDataSource bound to |request|. |
| 73 """ | 79 """ |
| 74 return TemplateDataSource( | 80 return TemplateDataSource( |
| 75 self._branch_info, | 81 self._branch_info, |
| 76 self._api_data_source_factory.Create(request), | 82 self._api_data_source_factory.Create(request), |
| 77 self._api_list_data_source_factory.Create(), | 83 self._api_list_data_source_factory.Create(), |
| 78 self._intro_data_source_factory.Create(), | 84 self._intro_data_source_factory.Create(), |
| 79 self._samples_data_source_factory.Create(request), | 85 self._samples_data_source_factory.Create(request), |
| 80 self._sidenav_data_source_factory.Create(path), | 86 self._sidenav_data_source_factory.Create(path), |
| 87 self._manifest_json, | |
| 81 self._cache, | 88 self._cache, |
| 82 self._public_template_path, | 89 self._public_template_path, |
| 83 self._private_template_path, | 90 self._private_template_path, |
| 84 self._static_resources) | 91 self._static_resources) |
| 85 | 92 |
| 86 def __init__(self, | 93 def __init__(self, |
| 87 branch_info, | 94 branch_info, |
| 88 api_data_source, | 95 api_data_source, |
| 89 api_list_data_source, | 96 api_list_data_source, |
| 90 intro_data_source, | 97 intro_data_source, |
| 91 samples_data_source, | 98 samples_data_source, |
| 92 sidenav_data_source, | 99 sidenav_data_source, |
| 100 manifest_json, | |
| 93 cache, | 101 cache, |
| 94 public_template_path, | 102 public_template_path, |
| 95 private_template_path, | 103 private_template_path, |
| 96 static_resources): | 104 static_resources): |
| 97 self._branch_info = branch_info | 105 self._branch_info = branch_info |
| 98 self._api_list_data_source = api_list_data_source | 106 self._api_list_data_source = api_list_data_source |
| 99 self._intro_data_source = intro_data_source | 107 self._intro_data_source = intro_data_source |
| 100 self._samples_data_source = samples_data_source | 108 self._samples_data_source = samples_data_source |
| 101 self._api_data_source = api_data_source | 109 self._api_data_source = api_data_source |
| 102 self._sidenav_data_source = sidenav_data_source | 110 self._sidenav_data_source = sidenav_data_source |
| 103 self._cache = cache | 111 self._cache = cache |
| 104 self._public_template_path = public_template_path | 112 self._public_template_path = public_template_path |
| 105 self._private_template_path = private_template_path | 113 self._private_template_path = private_template_path |
| 106 self._static_resources = static_resources | 114 self._static_resources = static_resources |
| 115 self._manifest_json = CSFSource( | |
| 116 manifest_json, JSON_PATH, lambda key: key + '.json') | |
| 117 | |
| 118 # import types | |
| 119 | |
| 120 # self._manifest_json.get = types.MethodType( | |
| 121 # lambda self, key: self.GetFromFile(os.path.join(JSON_PATH, key + '.json' )), | |
| 122 # self._manifest_json) | |
|
not at google - send to devlin
2013/04/30 16:21:09
delete?
jshumway
2013/05/03 03:44:39
Done.
| |
| 107 | 123 |
| 108 def Render(self, template_name): | 124 def Render(self, template_name): |
| 109 """This method will render a template named |template_name|, fetching all | 125 """This method will render a template named |template_name|, fetching all |
| 110 the partial templates needed from |self._cache|. Partials are retrieved | 126 the partial templates needed from |self._cache|. Partials are retrieved |
| 111 from the TemplateDataSource with the |get| method. | 127 from the TemplateDataSource with the |get| method. |
| 112 """ | 128 """ |
| 113 template = self.GetTemplate(self._public_template_path, template_name) | 129 template = self.GetTemplate(self._public_template_path, template_name) |
| 114 if not template: | 130 if not template: |
| 115 return None | 131 return None |
| 116 # TODO error handling | 132 # TODO error handling |
| 117 render_data = template.render({ | 133 render_data = template.render({ |
| 118 'api_list': self._api_list_data_source, | 134 'api_list': self._api_list_data_source, |
| 119 'apis': self._api_data_source, | 135 'apis': self._api_data_source, |
| 120 'branchInfo': self._branch_info, | 136 'branchInfo': self._branch_info, |
| 121 'intros': self._intro_data_source, | 137 'intros': self._intro_data_source, |
| 122 'sidenavs': self._sidenav_data_source, | 138 'sidenavs': self._sidenav_data_source, |
| 123 'partials': self, | 139 'partials': self, |
| 140 'manifest_json': self._manifest_json, | |
| 124 'samples': self._samples_data_source, | 141 'samples': self._samples_data_source, |
| 125 'static': self._static_resources, | 142 'static': self._static_resources, |
| 126 'app': 'app', | 143 'app': 'app', |
| 127 'extension': 'extension', | 144 'extension': 'extension', |
| 128 'apps_title': 'Apps', | 145 'apps_title': 'Apps', |
| 129 'extensions_title': 'Extensions', | 146 'extensions_title': 'Extensions', |
| 130 'apps_samples_url': url_constants.GITHUB_BASE, | 147 'apps_samples_url': url_constants.GITHUB_BASE, |
| 131 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, | 148 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, |
| 132 'true': True, | 149 'true': True, |
| 133 'false': False | 150 'false': False |
| 134 }) | 151 }) |
| 135 if render_data.errors: | 152 if render_data.errors: |
| 136 logging.error('Handlebar error(s) rendering %s:\n%s' % | 153 logging.error('Handlebar error(s) rendering %s:\n%s' % |
| 137 (template_name, ' \n'.join(render_data.errors))) | 154 (template_name, ' \n'.join(render_data.errors))) |
| 138 return render_data.text | 155 return render_data.text |
| 139 | 156 |
| 140 def get(self, key): | 157 def get(self, key): |
| 141 return self.GetTemplate(self._private_template_path, key) | 158 return self.GetTemplate(self._private_template_path, key) |
| 142 | 159 |
| 143 def GetTemplate(self, base_path, template_name): | 160 def GetTemplate(self, base_path, template_name): |
| 144 try: | 161 try: |
| 145 return self._cache.GetFromFile( | 162 return self._cache.GetFromFile( |
| 146 '/'.join((base_path, FormatKey(template_name)))) | 163 '/'.join((base_path, FormatKey(template_name)))) |
| 147 except FileNotFoundError as e: | 164 except FileNotFoundError as e: |
| 148 logging.warning(traceback.format_exc()) | 165 logging.warning(traceback.format_exc()) |
| 149 return None | 166 return None |
| OLD | NEW |