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 logging | 5 import logging |
| 6 | 6 |
| 7 from docs_server_utils import FormatKey | 7 from docs_server_utils import FormatKey |
| 8 from file_system import FileNotFoundError | 8 from file_system import FileNotFoundError |
| 9 from third_party.handlebar import Handlebar | 9 from third_party.handlebar import Handlebar |
| 10 import url_constants | 10 import url_constants |
| 11 | 11 |
| 12 _EXTENSIONS_URL = '/chrome/extensions' | 12 _EXTENSIONS_URL = '/chrome/extensions' |
| 13 | 13 |
| 14 _STRING_CONSTANTS = { | 14 _STRING_CONSTANTS = { |
| 15 'app': 'app', | 15 'app': 'app', |
| 16 'apps_title': 'Apps', | 16 'apps_title': 'Apps', |
| 17 'extension': 'extension', | 17 'extension': 'extension', |
| 18 'extensions_title': 'Extensions', | 18 'extensions_title': 'Extensions', |
| 19 'events': 'events', | 19 'events': 'events', |
| 20 'methods': 'methods', | 20 'methods': 'methods', |
| 21 'properties': 'properties', | 21 'properties': 'properties', |
| 22 } | 22 } |
| 23 | 23 |
| 24 # TODO-PATCH: Do I actually need this? | |
| 25 def _MakeChannelDict(channel_name): | |
|
not at google - send to devlin
2013/07/20 02:49:03
No I deleted it :)
jshumway
2013/07/22 17:16:27
Ah, oops. Was in a bit of a rush to get it up and
| |
| 26 channel_dict = { | |
| 27 'channels': [{'name': name} for name in BranchUtility.GetAllChannelNames()], | |
| 28 'current': channel_name | |
| 29 } | |
| 30 | |
| 31 for channel in channel_dict['channels']: | |
| 32 if channel['name'] == channel_name: | |
| 33 channel['isCurrent'] = True | |
| 34 return channel_dict | |
| 35 | |
| 24 class TemplateDataSource(object): | 36 class TemplateDataSource(object): |
| 25 """Renders Handlebar templates, providing them with the context in which to | 37 """Renders Handlebar templates, providing them with the context in which to |
| 26 render. | 38 render. |
| 27 | 39 |
| 28 Also acts as a data source itself, providing partial Handlebar templates to | 40 Also acts as a data source itself, providing partial Handlebar templates to |
| 29 those it renders. | 41 those it renders. |
| 30 | 42 |
| 31 Each instance of TemplateDataSource is bound to a Request so that it can | 43 Each instance of TemplateDataSource is bound to a Request so that it can |
| 32 render templates with request-specific data (such as Accept-Language); use | 44 render templates with request-specific data (such as Accept-Language); use |
| 33 a Factory to cheaply construct these. | 45 a Factory to cheaply construct these. |
| 34 """ | 46 """ |
| 35 | 47 |
| 36 class Factory(object): | 48 class Factory(object): |
| 37 """A factory to create lightweight TemplateDataSource instances bound to | 49 """A factory to create lightweight TemplateDataSource instances bound to |
| 38 individual Requests. | 50 individual Requests. |
| 39 """ | 51 """ |
| 40 def __init__(self, | 52 def __init__(self, |
| 41 api_data_source_factory, | 53 api_data_source_factory, |
| 42 api_list_data_source_factory, | 54 api_list_data_source_factory, |
| 43 intro_data_source_factory, | 55 intro_data_source_factory, |
| 44 samples_data_source_factory, | 56 samples_data_source_factory, |
| 45 sidenav_data_source_factory, | 57 sidenav_data_source_factory, |
| 46 compiled_fs_factory, | 58 compiled_fs_factory, |
| 47 ref_resolver_factory, | 59 ref_resolver_factory, |
| 60 manifest_data_source, | |
| 48 public_template_path, | 61 public_template_path, |
| 49 private_template_path, | 62 private_template_path, |
| 50 base_path): | 63 base_path): |
| 51 self._api_data_source_factory = api_data_source_factory | 64 self._api_data_source_factory = api_data_source_factory |
| 52 self._api_list_data_source_factory = api_list_data_source_factory | 65 self._api_list_data_source_factory = api_list_data_source_factory |
| 53 self._intro_data_source_factory = intro_data_source_factory | 66 self._intro_data_source_factory = intro_data_source_factory |
| 54 self._samples_data_source_factory = samples_data_source_factory | 67 self._samples_data_source_factory = samples_data_source_factory |
| 55 self._sidenav_data_source_factory = sidenav_data_source_factory | 68 self._sidenav_data_source_factory = sidenav_data_source_factory |
| 56 self._cache = compiled_fs_factory.Create(self._CreateTemplate, | 69 self._cache = compiled_fs_factory.Create(self._CreateTemplate, |
| 57 TemplateDataSource) | 70 TemplateDataSource) |
| 58 self._ref_resolver = ref_resolver_factory.Create() | 71 self._ref_resolver = ref_resolver_factory.Create() |
| 59 self._public_template_path = public_template_path | 72 self._public_template_path = public_template_path |
| 60 self._private_template_path = private_template_path | 73 self._private_template_path = private_template_path |
| 74 self._manifest_data_source = manifest_data_source | |
| 61 self._base_path = base_path | 75 self._base_path = base_path |
| 62 | 76 |
| 63 def _CreateTemplate(self, template_name, text): | 77 def _CreateTemplate(self, template_name, text): |
| 64 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) | 78 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) |
| 65 | 79 |
| 66 def Create(self, request, path): | 80 def Create(self, request, path): |
| 67 """Returns a new TemplateDataSource bound to |request|. | 81 """Returns a new TemplateDataSource bound to |request|. |
| 68 """ | 82 """ |
| 69 return TemplateDataSource( | 83 return TemplateDataSource( |
| 70 self._api_data_source_factory.Create(request), | 84 self._api_data_source_factory.Create(request), |
| 71 self._api_list_data_source_factory.Create(), | 85 self._api_list_data_source_factory.Create(), |
| 72 self._intro_data_source_factory.Create(), | 86 self._intro_data_source_factory.Create(), |
| 73 self._samples_data_source_factory.Create(request), | 87 self._samples_data_source_factory.Create(request), |
| 74 self._sidenav_data_source_factory.Create(path), | 88 self._sidenav_data_source_factory.Create(path), |
| 75 self._cache, | 89 self._cache, |
| 90 self._manifest_data_source, | |
| 76 self._public_template_path, | 91 self._public_template_path, |
| 77 self._private_template_path, | 92 self._private_template_path, |
| 78 self._base_path) | 93 self._base_path) |
| 79 | 94 |
| 80 def __init__(self, | 95 def __init__(self, |
| 81 api_data_source, | 96 api_data_source, |
| 82 api_list_data_source, | 97 api_list_data_source, |
| 83 intro_data_source, | 98 intro_data_source, |
| 84 samples_data_source, | 99 samples_data_source, |
| 85 sidenav_data_source, | 100 sidenav_data_source, |
| 86 cache, | 101 cache, |
| 102 manifest_data_source, | |
| 87 public_template_path, | 103 public_template_path, |
| 88 private_template_path, | 104 private_template_path, |
| 89 base_path): | 105 base_path): |
| 90 self._api_list_data_source = api_list_data_source | 106 self._api_list_data_source = api_list_data_source |
| 91 self._intro_data_source = intro_data_source | 107 self._intro_data_source = intro_data_source |
| 92 self._samples_data_source = samples_data_source | 108 self._samples_data_source = samples_data_source |
| 93 self._api_data_source = api_data_source | 109 self._api_data_source = api_data_source |
| 94 self._sidenav_data_source = sidenav_data_source | 110 self._sidenav_data_source = sidenav_data_source |
| 95 self._cache = cache | 111 self._cache = cache |
| 96 self._public_template_path = public_template_path | 112 self._public_template_path = public_template_path |
| 97 self._private_template_path = private_template_path | 113 self._private_template_path = private_template_path |
| 114 self._manifest_data_source = manifest_data_source | |
| 98 self._base_path = base_path | 115 self._base_path = base_path |
| 99 | 116 |
| 100 def Render(self, template_name): | 117 def Render(self, template_name): |
| 101 """This method will render a template named |template_name|, fetching all | 118 """This method will render a template named |template_name|, fetching all |
| 102 the partial templates needed from |self._cache|. Partials are retrieved | 119 the partial templates needed from |self._cache|. Partials are retrieved |
| 103 from the TemplateDataSource with the |get| method. | 120 from the TemplateDataSource with the |get| method. |
| 104 """ | 121 """ |
| 105 template = self.GetTemplate(self._public_template_path, template_name) | 122 template = self.GetTemplate(self._public_template_path, template_name) |
| 106 if template is None: | 123 if template is None: |
| 107 return None | 124 return None |
| 108 # TODO error handling | 125 # TODO error handling |
| 109 render_data = template.render({ | 126 render_data = template.render({ |
| 110 'api_list': self._api_list_data_source, | 127 'api_list': self._api_list_data_source, |
| 111 'apis': self._api_data_source, | 128 'apis': self._api_data_source, |
| 112 'intros': self._intro_data_source, | 129 'intros': self._intro_data_source, |
| 113 'sidenavs': self._sidenav_data_source, | 130 'sidenavs': self._sidenav_data_source, |
| 114 'partials': self, | 131 'partials': self, |
| 132 'manifest_source': self._manifest_data_source, | |
| 115 'samples': self._samples_data_source, | 133 'samples': self._samples_data_source, |
| 116 'apps_samples_url': url_constants.GITHUB_BASE, | 134 'apps_samples_url': url_constants.GITHUB_BASE, |
| 117 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, | 135 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, |
| 118 'static': self._base_path + '/static', | 136 'static': self._base_path + '/static', |
| 119 'strings': _STRING_CONSTANTS, | 137 'strings': _STRING_CONSTANTS, |
| 120 'true': True, | 138 'true': True, |
| 121 'false': False | 139 'false': False |
| 122 }) | 140 }) |
| 123 if render_data.errors: | 141 if render_data.errors: |
| 124 logging.error('Handlebar error(s) rendering %s:\n%s' % | 142 logging.error('Handlebar error(s) rendering %s:\n%s' % |
| 125 (template_name, ' \n'.join(render_data.errors))) | 143 (template_name, ' \n'.join(render_data.errors))) |
| 126 return render_data.text | 144 return render_data.text |
| 127 | 145 |
| 128 def get(self, key): | 146 def get(self, key): |
| 129 return self.GetTemplate(self._private_template_path, key) | 147 return self.GetTemplate(self._private_template_path, key) |
| 130 | 148 |
| 131 def GetTemplate(self, base_path, template_name): | 149 def GetTemplate(self, base_path, template_name): |
| 132 try: | 150 try: |
| 133 return self._cache.GetFromFile( | 151 return self._cache.GetFromFile( |
| 134 '/'.join((base_path, FormatKey(template_name)))) | 152 '/'.join((base_path, FormatKey(template_name)))) |
| 135 except FileNotFoundError as e: | 153 except FileNotFoundError: |
| 136 return None | 154 return None |
| OLD | NEW |