| 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 from path_utils import FormatKey | 6 from path_utils import FormatKey |
| 6 from third_party.handlebar import Handlebar | 7 from third_party.handlebar import Handlebar |
| 7 | 8 |
| 8 EXTENSIONS_URL = '/chrome/extensions' | 9 EXTENSIONS_URL = '/chrome/extensions' |
| 9 | 10 |
| 10 class TemplateDataSource(object): | 11 class TemplateDataSource(object): |
| 11 """This class fetches and compiles templates using the fetcher passed in with | 12 """This class fetches and compiles templates using the fetcher passed in with |
| 12 |cache_builder|. | 13 |cache_builder|. |
| 13 """ | 14 """ |
| 14 def __init__(self, | 15 def __init__(self, |
| 15 branch, | 16 branch, |
| 16 api_data_source, | 17 api_data_source, |
| 17 intro_data_source, | 18 intro_data_source, |
| 18 samples_data_source, | 19 samples_data_source, |
| 19 cache_builder, | 20 cache_builder, |
| 20 base_paths): | 21 base_paths): |
| 21 self._branch_info = self._MakeBranchDict(branch) | 22 self._branch_info = self._MakeBranchDict(branch) |
| 22 self._static_resources = ((('/' + branch) if branch != 'local' else '') + | 23 self._static_resources = ((('/' + branch) if branch != 'local' else '') + |
| 23 '/static') | 24 '/static') |
| 24 self._api_data_source = api_data_source | 25 self._api_data_source = api_data_source |
| 25 self._intro_data_source = intro_data_source | 26 self._intro_data_source = intro_data_source |
| 26 self._samples_data_source = samples_data_source | 27 self._samples_data_source = samples_data_source |
| 27 self._cache = cache_builder.build(self._LoadTemplate) | 28 self._cache = cache_builder.build(self._LoadTemplate) |
| 28 self._base_paths = base_paths | 29 self._base_paths = base_paths |
| 30 self._use = 0 |
| 29 | 31 |
| 30 def _MakeBranchDict(self, branch): | 32 def _MakeBranchDict(self, branch): |
| 31 return { | 33 return { |
| 32 'showWarning': branch != 'stable', | 34 'showWarning': branch != 'stable', |
| 33 'branches': [ | 35 'branches': [ |
| 34 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' }, | 36 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' }, |
| 35 { 'name': 'Dev', 'path': EXTENSIONS_URL + '/dev' }, | 37 { 'name': 'Dev', 'path': EXTENSIONS_URL + '/dev' }, |
| 36 { 'name': 'Beta', 'path': EXTENSIONS_URL + '/beta' }, | 38 { 'name': 'Beta', 'path': EXTENSIONS_URL + '/beta' }, |
| 37 { 'name': 'Trunk', 'path': EXTENSIONS_URL + '/trunk' } | 39 { 'name': 'Trunk', 'path': EXTENSIONS_URL + '/trunk' } |
| 38 ], | 40 ], |
| 39 'current': branch | 41 'current': branch |
| 40 } | 42 } |
| 41 | 43 |
| 42 def _LoadTemplate(self, template): | 44 def _LoadTemplate(self, template): |
| 43 return Handlebar(template) | 45 return Handlebar(template) |
| 44 | 46 |
| 45 def Render(self, template_name): | 47 def Render(self, template_name): |
| 46 """This method will render a template named |template_name|, fetching all | 48 """This method will render a template named |template_name|, fetching all |
| 47 the partial templates needed from |self._cache|. Partials are retrieved | 49 the partial templates needed from |self._cache|. Partials are retrieved |
| 48 from the TemplateDataSource with the |get| method. | 50 from the TemplateDataSource with the |get| method. |
| 49 """ | 51 """ |
| 52 self._use = 0 |
| 50 template = self.get(template_name) | 53 template = self.get(template_name) |
| 51 if not template: | 54 if not template: |
| 52 return '' | 55 return '' |
| 53 # TODO error handling | 56 # TODO error handling |
| 57 self._use = 1 |
| 54 return template.render({ | 58 return template.render({ |
| 55 'apis': self._api_data_source, | 59 'apis': self._api_data_source, |
| 56 'branchInfo': self._branch_info, | 60 'branchInfo': self._branch_info, |
| 57 'intros': self._intro_data_source, | 61 'intros': self._intro_data_source, |
| 58 'partials': self, | 62 'partials': self, |
| 59 'samples': self._samples_data_source, | 63 'samples': self._samples_data_source, |
| 60 'static': self._static_resources | 64 'static': self._static_resources |
| 61 }).text | 65 }).text |
| 62 | 66 |
| 63 def __getitem__(self, key): | 67 def __getitem__(self, key): |
| 64 return self.get(key) | 68 return self.get(key) |
| 65 | 69 |
| 66 def get(self, key): | 70 def get(self, key): |
| 67 real_path = FormatKey(key) | 71 real_path = FormatKey(key) |
| 68 for base_path in self._base_paths: | 72 try: |
| 69 try: | 73 return self._cache.getFromFile( |
| 70 return self._cache.getFromFile(base_path + '/' + real_path) | 74 self._base_paths[self._use] + '/' + real_path) |
| 71 except: | 75 except Exception as e: |
| 72 pass | 76 logging.warn(e) |
| 73 return None | 77 return None |
| OLD | NEW |