| 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 logging | 5 import logging |
| 6 import os |
| 6 import traceback | 7 import traceback |
| 7 | 8 |
| 8 from branch_utility import BranchUtility | 9 from branch_utility import BranchUtility |
| 10 import compiled_file_system as compiled_fs |
| 9 from docs_server_utils import FormatKey | 11 from docs_server_utils import FormatKey |
| 10 from file_system import FileNotFoundError | 12 from file_system import FileNotFoundError |
| 11 from third_party.handlebar import Handlebar | 13 from third_party.handlebar import Handlebar |
| 12 import url_constants | 14 import url_constants |
| 13 | 15 |
| 14 EXTENSIONS_URL = '/chrome/extensions' | 16 EXTENSIONS_URL = '/chrome/extensions' |
| 15 | 17 |
| 16 def _MakeChannelDict(channel_name): | 18 def _MakeChannelDict(channel_name): |
| 17 channel_dict = { | 19 channel_dict = { |
| 18 'channels': [{'name': name} for name in BranchUtility.GetAllChannelNames()], | 20 'channels': [{'name': name} for name in BranchUtility.GetAllChannelNames()], |
| 19 'current': channel_name | 21 'current': channel_name |
| 20 } | 22 } |
| 21 | |
| 22 for channel in channel_dict['channels']: | 23 for channel in channel_dict['channels']: |
| 23 if channel['name'] == channel_name: | 24 if channel['name'] == channel_name: |
| 24 channel['isCurrent'] = True | 25 channel['isCurrent'] = True |
| 25 return channel_dict | 26 return channel_dict |
| 26 | 27 |
| 27 class TemplateDataSource(object): | 28 class TemplateDataSource(object): |
| 28 """Renders Handlebar templates, providing them with the context in which to | 29 """Renders Handlebar templates, providing them with the context in which to |
| 29 render. | 30 render. |
| 30 | 31 |
| 31 Also acts as a data source itself, providing partial Handlebar templates to | 32 Also acts as a data source itself, providing partial Handlebar templates to |
| (...skipping 10 matching lines...) Expand all Loading... |
| 42 """ | 43 """ |
| 43 def __init__(self, | 44 def __init__(self, |
| 44 channel_name, | 45 channel_name, |
| 45 api_data_source_factory, | 46 api_data_source_factory, |
| 46 api_list_data_source_factory, | 47 api_list_data_source_factory, |
| 47 intro_data_source_factory, | 48 intro_data_source_factory, |
| 48 samples_data_source_factory, | 49 samples_data_source_factory, |
| 49 sidenav_data_source_factory, | 50 sidenav_data_source_factory, |
| 50 compiled_fs_factory, | 51 compiled_fs_factory, |
| 51 ref_resolver_factory, | 52 ref_resolver_factory, |
| 52 manifest_data_source, | |
| 53 public_template_path, | 53 public_template_path, |
| 54 private_template_path): | 54 private_template_path): |
| 55 self._branch_info = _MakeChannelDict(channel_name) | 55 self._branch_info = _MakeChannelDict(channel_name) |
| 56 self._api_data_source_factory = api_data_source_factory | 56 self._api_data_source_factory = api_data_source_factory |
| 57 self._api_list_data_source_factory = api_list_data_source_factory | 57 self._api_list_data_source_factory = api_list_data_source_factory |
| 58 self._intro_data_source_factory = intro_data_source_factory | 58 self._intro_data_source_factory = intro_data_source_factory |
| 59 self._samples_data_source_factory = samples_data_source_factory | 59 self._samples_data_source_factory = samples_data_source_factory |
| 60 self._sidenav_data_source_factory = sidenav_data_source_factory | 60 self._sidenav_data_source_factory = sidenav_data_source_factory |
| 61 self._cache = compiled_fs_factory.Create(self._CreateTemplate, | 61 self._cache = compiled_fs_factory.Create(self._CreateTemplate, |
| 62 TemplateDataSource) | 62 TemplateDataSource) |
| 63 self._ref_resolver = ref_resolver_factory.Create() | 63 self._ref_resolver = ref_resolver_factory.Create() |
| 64 self._public_template_path = public_template_path | 64 self._public_template_path = public_template_path |
| 65 self._private_template_path = private_template_path | 65 self._private_template_path = private_template_path |
| 66 self._static_resources = '/%s/static' % channel_name | 66 self._static_resources = '/%s/static' % channel_name |
| 67 self._manifest_data_source = manifest_data_source | |
| 68 | 67 |
| 69 def _CreateTemplate(self, template_name, text): | 68 def _CreateTemplate(self, template_name, text): |
| 70 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) | 69 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) |
| 71 | 70 |
| 72 def Create(self, request, path): | 71 def Create(self, request, path): |
| 73 """Returns a new TemplateDataSource bound to |request|. | 72 """Returns a new TemplateDataSource bound to |request|. |
| 74 """ | 73 """ |
| 75 return TemplateDataSource( | 74 return TemplateDataSource( |
| 76 self._branch_info, | 75 self._branch_info, |
| 77 self._api_data_source_factory.Create(request), | 76 self._api_data_source_factory.Create(request), |
| 78 self._api_list_data_source_factory.Create(), | 77 self._api_list_data_source_factory.Create(), |
| 79 self._intro_data_source_factory.Create(), | 78 self._intro_data_source_factory.Create(), |
| 80 self._samples_data_source_factory.Create(request), | 79 self._samples_data_source_factory.Create(request), |
| 81 self._sidenav_data_source_factory.Create(path), | 80 self._sidenav_data_source_factory.Create(path), |
| 82 self._cache, | 81 self._cache, |
| 83 self._manifest_data_source, | |
| 84 self._public_template_path, | 82 self._public_template_path, |
| 85 self._private_template_path, | 83 self._private_template_path, |
| 86 self._static_resources) | 84 self._static_resources) |
| 87 | 85 |
| 88 def __init__(self, | 86 def __init__(self, |
| 89 branch_info, | 87 branch_info, |
| 90 api_data_source, | 88 api_data_source, |
| 91 api_list_data_source, | 89 api_list_data_source, |
| 92 intro_data_source, | 90 intro_data_source, |
| 93 samples_data_source, | 91 samples_data_source, |
| 94 sidenav_data_source, | 92 sidenav_data_source, |
| 95 cache, | 93 cache, |
| 96 manifest_data_source, | |
| 97 public_template_path, | 94 public_template_path, |
| 98 private_template_path, | 95 private_template_path, |
| 99 static_resources): | 96 static_resources): |
| 100 self._branch_info = branch_info | 97 self._branch_info = branch_info |
| 101 self._api_list_data_source = api_list_data_source | 98 self._api_list_data_source = api_list_data_source |
| 102 self._intro_data_source = intro_data_source | 99 self._intro_data_source = intro_data_source |
| 103 self._samples_data_source = samples_data_source | 100 self._samples_data_source = samples_data_source |
| 104 self._api_data_source = api_data_source | 101 self._api_data_source = api_data_source |
| 105 self._sidenav_data_source = sidenav_data_source | 102 self._sidenav_data_source = sidenav_data_source |
| 106 self._cache = cache | 103 self._cache = cache |
| 107 self._public_template_path = public_template_path | 104 self._public_template_path = public_template_path |
| 108 self._private_template_path = private_template_path | 105 self._private_template_path = private_template_path |
| 109 self._static_resources = static_resources | 106 self._static_resources = static_resources |
| 110 self._manifest_data_source = manifest_data_source | |
| 111 | 107 |
| 112 def Render(self, template_name): | 108 def Render(self, template_name): |
| 113 """This method will render a template named |template_name|, fetching all | 109 """This method will render a template named |template_name|, fetching all |
| 114 the partial templates needed from |self._cache|. Partials are retrieved | 110 the partial templates needed from |self._cache|. Partials are retrieved |
| 115 from the TemplateDataSource with the |get| method. | 111 from the TemplateDataSource with the |get| method. |
| 116 """ | 112 """ |
| 117 template = self.GetTemplate(self._public_template_path, template_name) | 113 template = self.GetTemplate(self._public_template_path, template_name) |
| 118 if not template: | 114 if not template: |
| 119 return None | 115 return None |
| 120 # TODO error handling | 116 # TODO error handling |
| 121 render_data = template.render({ | 117 render_data = template.render({ |
| 122 'api_list': self._api_list_data_source, | 118 'api_list': self._api_list_data_source, |
| 123 'apis': self._api_data_source, | 119 'apis': self._api_data_source, |
| 124 'branchInfo': self._branch_info, | 120 'branchInfo': self._branch_info, |
| 125 'intros': self._intro_data_source, | 121 'intros': self._intro_data_source, |
| 126 'sidenavs': self._sidenav_data_source, | 122 'sidenavs': self._sidenav_data_source, |
| 127 'partials': self, | 123 'partials': self, |
| 128 'manifest_source': self._manifest_data_source, | |
| 129 'samples': self._samples_data_source, | 124 'samples': self._samples_data_source, |
| 130 'static': self._static_resources, | 125 'static': self._static_resources, |
| 131 'app': 'app', | 126 'app': 'app', |
| 132 'extension': 'extension', | 127 'extension': 'extension', |
| 133 'apps_title': 'Apps', | 128 'apps_title': 'Apps', |
| 134 'extensions_title': 'Extensions', | 129 'extensions_title': 'Extensions', |
| 135 'apps_samples_url': url_constants.GITHUB_BASE, | 130 'apps_samples_url': url_constants.GITHUB_BASE, |
| 136 # TODO(kalman): this is wrong, it's always getting from trunk, but meh | 131 # TODO(kalman): this is wrong, it's always getting from trunk, but meh |
| 137 # it hardly ever shows up (only in the "cannot fetch samples" message). | 132 # it hardly ever shows up (only in the "cannot fetch samples" message). |
| 138 # In fact I don't even know if it can show up anymore due the samples data | 133 # In fact I don't even know if it can show up anymore due the samples data |
| 139 # being persisent. In any case, when the channel distinctions are gone | 134 # being persisent. In any case, when the channel distinctions are gone |
| 140 # this can go away, so, double meh. | 135 # this can go away, so, double meh. |
| 141 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, | 136 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, |
| 142 'true': True, | 137 'true': True, |
| 143 'false': False | 138 'false': False |
| 144 }) | 139 }) |
| 145 if render_data.errors: | 140 if render_data.errors: |
| 146 logging.error('Handlebar error(s) rendering %s:\n%s' % | 141 logging.error('Handlebar error(s) rendering %s:\n%s' % |
| 147 (template_name, ' \n'.join(render_data.errors))) | 142 (template_name, ' \n'.join(render_data.errors))) |
| 148 return render_data.text | 143 return render_data.text |
| 149 | 144 |
| 150 def get(self, key): | 145 def get(self, key): |
| 151 return self.GetTemplate(self._private_template_path, key) | 146 return self.GetTemplate(self._private_template_path, key) |
| 152 | 147 |
| 153 def GetTemplate(self, base_path, template_name): | 148 def GetTemplate(self, base_path, template_name): |
| 154 try: | 149 try: |
| 155 return self._cache.GetFromFile( | 150 return self._cache.GetFromFile( |
| 156 '/'.join((base_path, FormatKey(template_name)))) | 151 '/'.join((base_path, FormatKey(template_name)))) |
| 157 except FileNotFoundError: | 152 except FileNotFoundError as e: |
| 158 logging.warning(traceback.format_exc()) | 153 logging.warning(traceback.format_exc()) |
| 159 return None | 154 return None |
| OLD | NEW |