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 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 | 25 |
| 26 class Factory(object): | 26 class Factory(object): |
| 27 """A factory to create lightweight TemplateDataSource instances bound to | 27 """A factory to create lightweight TemplateDataSource instances bound to |
| 28 individual Requests. | 28 individual Requests. |
| 29 """ | 29 """ |
| 30 def __init__(self, | 30 def __init__(self, |
| 31 api_data_source_factory, | 31 api_data_source_factory, |
| 32 api_list_data_source_factory, | 32 api_list_data_source_factory, |
| 33 intro_data_source_factory, | 33 intro_data_source_factory, |
| 34 samples_data_source_factory, | 34 samples_data_source_factory, |
| 35 sidenav_data_source_factory, | |
| 36 compiled_fs_factory, | 35 compiled_fs_factory, |
| 37 ref_resolver_factory, | 36 ref_resolver_factory, |
| 38 permissions_data_source, | 37 permissions_data_source, |
| 39 public_template_path, | 38 public_template_path, |
| 40 private_template_path, | 39 private_template_path, |
| 41 base_path, | 40 base_path): |
| 42 data_sources): | |
| 43 self._api_data_source_factory = api_data_source_factory | 41 self._api_data_source_factory = api_data_source_factory |
| 44 self._api_list_data_source_factory = api_list_data_source_factory | 42 self._api_list_data_source_factory = api_list_data_source_factory |
| 45 self._intro_data_source_factory = intro_data_source_factory | 43 self._intro_data_source_factory = intro_data_source_factory |
| 46 self._samples_data_source_factory = samples_data_source_factory | 44 self._samples_data_source_factory = samples_data_source_factory |
| 47 self._sidenav_data_source_factory = sidenav_data_source_factory | |
| 48 self._cache = compiled_fs_factory.Create(self._CreateTemplate, | 45 self._cache = compiled_fs_factory.Create(self._CreateTemplate, |
| 49 TemplateDataSource) | 46 TemplateDataSource) |
| 50 self._ref_resolver = ref_resolver_factory.Create() | 47 self._ref_resolver = ref_resolver_factory.Create() |
| 51 self._permissions_data_source = permissions_data_source | 48 self._permissions_data_source = permissions_data_source |
| 52 self._public_template_path = public_template_path | 49 self._public_template_path = public_template_path |
| 53 self._private_template_path = private_template_path | 50 self._private_template_path = private_template_path |
| 54 self._base_path = base_path | 51 self._base_path = base_path |
| 55 self._data_sources = data_sources | |
| 56 | 52 |
| 57 def _CreateTemplate(self, template_name, text): | 53 def _CreateTemplate(self, template_name, text): |
| 58 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) | 54 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) |
| 59 | 55 |
| 60 def Create(self, request, path): | 56 def Create(self, request, path, data_sources): |
|
Jeffrey Yasskin
2013/08/28 19:12:37
Please document the types of these arguments, and
jshumway
2013/08/29 00:13:09
Done.
| |
| 61 """Returns a new TemplateDataSource bound to |request|. | 57 """Returns a new TemplateDataSource bound to |request|. |
| 62 """ | 58 """ |
| 63 return TemplateDataSource( | 59 return TemplateDataSource( |
| 64 self._api_data_source_factory.Create(request), | 60 self._api_data_source_factory.Create(request), |
| 65 self._api_list_data_source_factory.Create(), | 61 self._api_list_data_source_factory.Create(), |
| 66 self._intro_data_source_factory.Create(), | 62 self._intro_data_source_factory.Create(), |
| 67 self._samples_data_source_factory.Create(request), | 63 self._samples_data_source_factory.Create(request), |
| 68 self._sidenav_data_source_factory.Create(path), | |
| 69 self._cache, | 64 self._cache, |
| 70 self._permissions_data_source, | 65 self._permissions_data_source, |
| 71 self._public_template_path, | 66 self._public_template_path, |
| 72 self._private_template_path, | 67 self._private_template_path, |
| 73 self._base_path, | 68 self._base_path, |
| 74 self._data_sources) | 69 data_sources) |
| 75 | 70 |
| 76 def __init__(self, | 71 def __init__(self, |
| 77 api_data_source, | 72 api_data_source, |
| 78 api_list_data_source, | 73 api_list_data_source, |
| 79 intro_data_source, | 74 intro_data_source, |
| 80 samples_data_source, | 75 samples_data_source, |
| 81 sidenav_data_source, | |
| 82 cache, | 76 cache, |
| 83 permissions_data_source, | 77 permissions_data_source, |
| 84 public_template_path, | 78 public_template_path, |
| 85 private_template_path, | 79 private_template_path, |
| 86 base_path, | 80 base_path, |
| 87 data_sources): | 81 data_sources): |
| 88 self._api_list_data_source = api_list_data_source | 82 self._api_list_data_source = api_list_data_source |
| 89 self._intro_data_source = intro_data_source | 83 self._intro_data_source = intro_data_source |
| 90 self._samples_data_source = samples_data_source | 84 self._samples_data_source = samples_data_source |
| 91 self._api_data_source = api_data_source | 85 self._api_data_source = api_data_source |
| 92 self._sidenav_data_source = sidenav_data_source | |
| 93 self._cache = cache | 86 self._cache = cache |
| 94 self._public_template_path = public_template_path | 87 self._public_template_path = public_template_path |
| 95 self._private_template_path = private_template_path | 88 self._private_template_path = private_template_path |
| 96 self._permissions_data_source = permissions_data_source | 89 self._permissions_data_source = permissions_data_source |
| 97 self._base_path = base_path | 90 self._base_path = base_path |
| 98 self._data_sources = data_sources | 91 self._data_sources = data_sources |
| 99 | 92 |
| 100 def Render(self, template_name): | 93 def Render(self, template_name): |
| 101 """This method will render a template named |template_name|, fetching all | 94 """This method will render a template named |template_name|, fetching all |
| 102 the partial templates needed from |self._cache|. Partials are retrieved | 95 the partial templates needed from |self._cache|. Partials are retrieved |
| 103 from the TemplateDataSource with the |get| method. | 96 from the TemplateDataSource with the |get| method. |
| 104 """ | 97 """ |
| 105 template = self.GetTemplate(self._public_template_path, template_name) | 98 template = self.GetTemplate(self._public_template_path, template_name) |
| 106 if template is None: | 99 if template is None: |
| 107 return None | 100 return None |
| 108 # TODO error handling | 101 # TODO error handling |
| 109 render_context = { | 102 render_context = { |
| 110 'api_list': self._api_list_data_source, | 103 'api_list': self._api_list_data_source, |
| 111 'apis': self._api_data_source, | 104 'apis': self._api_data_source, |
| 112 'intros': self._intro_data_source, | 105 'intros': self._intro_data_source, |
| 113 'sidenavs': self._sidenav_data_source, | |
| 114 'partials': self, | 106 'partials': self, |
| 115 'permissions': self._permissions_data_source, | 107 'permissions': self._permissions_data_source, |
| 116 'samples': self._samples_data_source, | 108 'samples': self._samples_data_source, |
| 117 'apps_samples_url': url_constants.GITHUB_BASE, | 109 'apps_samples_url': url_constants.GITHUB_BASE, |
| 118 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, | 110 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, |
| 119 'static': self._base_path + '/static', | 111 'static': self._base_path + '/static', |
| 120 'true': True, | 112 'true': True, |
| 121 'false': False | 113 'false': False |
| 122 } | 114 } |
| 123 render_context.update(self._data_sources) | 115 render_context.update(self._data_sources) |
| 124 render_data = template.render(render_context) | 116 render_data = template.render(render_context) |
| 125 if render_data.errors: | 117 if render_data.errors: |
| 126 logging.error('Handlebar error(s) rendering %s:\n%s' % | 118 logging.error('Handlebar error(s) rendering %s:\n%s' % |
| 127 (template_name, ' \n'.join(render_data.errors))) | 119 (template_name, ' \n'.join(render_data.errors))) |
| 128 return render_data.text | 120 return render_data.text |
| 129 | 121 |
| 130 def get(self, key): | 122 def get(self, key): |
| 131 return self.GetTemplate(self._private_template_path, key) | 123 return self.GetTemplate(self._private_template_path, key) |
| 132 | 124 |
| 133 def GetTemplate(self, base_path, template_name): | 125 def GetTemplate(self, base_path, template_name): |
| 134 try: | 126 try: |
| 135 return self._cache.GetFromFile( | 127 return self._cache.GetFromFile( |
| 136 '/'.join((base_path, FormatKey(template_name)))) | 128 '/'.join((base_path, FormatKey(template_name)))) |
| 137 except FileNotFoundError: | 129 except FileNotFoundError: |
| 138 return None | 130 return None |
| OLD | NEW |