| 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 third_party.handlebar import Handlebar | 8 from third_party.handlebar import Handlebar |
| 9 | 9 |
| 10 EXTENSIONS_URL = '/chrome/extensions' | 10 EXTENSIONS_URL = '/chrome/extensions' |
| 11 | 11 |
| 12 def _MakeBranchDict(branch): | 12 def _MakeBranchDict(branch, doc_type): |
| 13 return { | 13 return { |
| 14 'is_apps': doc_type == 'apps', |
| 14 'showWarning': branch != 'stable', | 15 'showWarning': branch != 'stable', |
| 15 'branches': [ | 16 'branches': [ |
| 16 { 'name': 'Stable', 'path': 'stable' }, | 17 { 'name': 'Stable', 'path': 'stable' }, |
| 17 { 'name': 'Dev', 'path': 'dev' }, | 18 { 'name': 'Dev', 'path': 'dev' }, |
| 18 { 'name': 'Beta', 'path': 'beta' }, | 19 { 'name': 'Beta', 'path': 'beta' }, |
| 19 { 'name': 'Trunk', 'path': 'trunk' } | 20 { 'name': 'Trunk', 'path': 'trunk' } |
| 20 ], | 21 ], |
| 21 'current': branch | 22 'current': branch |
| 22 } | 23 } |
| 23 | 24 |
| 24 class TemplateDataSource(object): | 25 class TemplateDataSource(object): |
| 25 """Renders Handlebar templates, providing them with the context in which to | 26 """Renders Handlebar templates, providing them with the context in which to |
| 26 render. | 27 render. |
| 27 | 28 |
| 28 Also acts as a data source itself, providing partial Handlebar templates to | 29 Also acts as a data source itself, providing partial Handlebar templates to |
| 29 those it renders. | 30 those it renders. |
| 30 | 31 |
| 31 Each instance of TemplateDataSource is bound to a Request so that it can | 32 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 | 33 render templates with request-specific data (such as Accept-Language); use |
| 33 a Factory to cheaply construct these. | 34 a Factory to cheaply construct these. |
| 34 """ | 35 """ |
| 35 | 36 |
| 36 class Factory(object): | 37 class Factory(object): |
| 37 """A factory to create lightweight TemplateDataSource instances bound to | 38 """A factory to create lightweight TemplateDataSource instances bound to |
| 38 individual Requests. | 39 individual Requests. |
| 39 """ | 40 """ |
| 40 def __init__(self, | 41 def __init__(self, |
| 41 branch, | 42 branch, |
| 43 doc_type, |
| 42 api_data_source_factory, | 44 api_data_source_factory, |
| 43 api_list_data_source, | 45 api_list_data_source, |
| 44 intro_data_source, | 46 intro_data_source, |
| 45 samples_data_source_factory, | 47 samples_data_source_factory, |
| 46 cache_builder, | 48 cache_builder, |
| 47 public_template_path, | 49 public_template_path, |
| 48 private_template_path): | 50 private_template_path): |
| 49 self._branch_info = _MakeBranchDict(branch) | 51 self._branch_info = _MakeBranchDict(branch, doc_type) |
| 50 self._static_resources = ((('/' + branch) if branch != 'local' else '') + | 52 self._static_resources = ((('/' + branch) if branch != 'local' else '') + |
| 51 '/static') | 53 '/static') |
| 52 self._api_data_source_factory = api_data_source_factory | 54 self._api_data_source_factory = api_data_source_factory |
| 53 self._api_list_data_source = api_list_data_source | 55 self._api_list_data_source = api_list_data_source |
| 54 self._intro_data_source = intro_data_source | 56 self._intro_data_source = intro_data_source |
| 55 self._samples_data_source_factory = samples_data_source_factory | 57 self._samples_data_source_factory = samples_data_source_factory |
| 56 self._cache = cache_builder.build(Handlebar) | 58 self._cache = cache_builder.build(Handlebar) |
| 57 self._public_template_path = public_template_path | 59 self._public_template_path = public_template_path |
| 58 self._private_template_path = private_template_path | 60 self._private_template_path = private_template_path |
| 59 | 61 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 def get(self, key): | 121 def get(self, key): |
| 120 return self.GetTemplate(self._private_template_path, key) | 122 return self.GetTemplate(self._private_template_path, key) |
| 121 | 123 |
| 122 def GetTemplate(self, base_path, template_name): | 124 def GetTemplate(self, base_path, template_name): |
| 123 real_path = FormatKey(template_name) | 125 real_path = FormatKey(template_name) |
| 124 try: | 126 try: |
| 125 return self._cache.GetFromFile(base_path + '/' + real_path) | 127 return self._cache.GetFromFile(base_path + '/' + real_path) |
| 126 except Exception as e: | 128 except Exception as e: |
| 127 logging.error(e) | 129 logging.error(e) |
| 128 return None | 130 return None |
| OLD | NEW |