| 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 from path_utils import FormatKey | 5 from path_utils import FormatKey |
| 6 from third_party.handlebar import Handlebar | 6 from third_party.handlebar import Handlebar |
| 7 | 7 |
| 8 EXTENSIONS_URL = '/chrome/extensions' | 8 EXTENSIONS_URL = '/chrome/extensions' |
| 9 | 9 |
| 10 def _MakeBranchDict(branch): | 10 def _MakeBranchDict(branch): |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 a Factory to cheaply construct these. | 31 a Factory to cheaply construct these. |
| 32 """ | 32 """ |
| 33 | 33 |
| 34 class Factory(object): | 34 class Factory(object): |
| 35 """ A factory to create lightweight TemplateDataSource instances bound to | 35 """ A factory to create lightweight TemplateDataSource instances bound to |
| 36 individual Requests. | 36 individual Requests. |
| 37 """ | 37 """ |
| 38 def __init__(self, | 38 def __init__(self, |
| 39 branch, | 39 branch, |
| 40 api_data_source, | 40 api_data_source, |
| 41 api_list_data_source, |
| 41 intro_data_source, | 42 intro_data_source, |
| 42 samples_data_source, | 43 samples_data_source, |
| 43 cache_builder, | 44 cache_builder, |
| 44 base_paths): | 45 base_paths): |
| 45 self._branch_info = _MakeBranchDict(branch) | 46 self._branch_info = _MakeBranchDict(branch) |
| 46 self._static_resources = ((('/' + branch) if branch != 'local' else '') + | 47 self._static_resources = ((('/' + branch) if branch != 'local' else '') + |
| 47 '/static') | 48 '/static') |
| 48 self._api_data_source = api_data_source | 49 self._api_data_source = api_data_source |
| 50 self._api_list_data_source = api_list_data_source |
| 49 self._intro_data_source = intro_data_source | 51 self._intro_data_source = intro_data_source |
| 50 self._samples_data_source = samples_data_source | 52 self._samples_data_source = samples_data_source |
| 51 self._cache = cache_builder.build(Handlebar) | 53 self._cache = cache_builder.build(Handlebar) |
| 52 self._base_paths = base_paths | 54 self._base_paths = base_paths |
| 53 | 55 |
| 54 def Create(self, request): | 56 def Create(self, request): |
| 55 """ Returns a new TemplateDataSource bound to |request|. | 57 """ Returns a new TemplateDataSource bound to |request|. |
| 56 """ | 58 """ |
| 57 return TemplateDataSource(self._branch_info, | 59 return TemplateDataSource(self._branch_info, |
| 58 self._static_resources, | 60 self._static_resources, |
| 59 self._api_data_source, | 61 self._api_data_source, |
| 62 self._api_list_data_source, |
| 60 self._intro_data_source, | 63 self._intro_data_source, |
| 61 self._samples_data_source, | 64 self._samples_data_source, |
| 62 self._cache, | 65 self._cache, |
| 63 self._base_paths, | 66 self._base_paths, |
| 64 request) | 67 request) |
| 65 | 68 |
| 66 def __init__(self, | 69 def __init__(self, |
| 67 branch_info, | 70 branch_info, |
| 68 static_resources, | 71 static_resources, |
| 69 api_data_source, | 72 api_data_source, |
| 73 api_list_data_source, |
| 70 intro_data_source, | 74 intro_data_source, |
| 71 samples_data_source, | 75 samples_data_source, |
| 72 cache, | 76 cache, |
| 73 base_paths, | 77 base_paths, |
| 74 request): | 78 request): |
| 75 self._branch_info = branch_info | 79 self._branch_info = branch_info |
| 76 self._static_resources = static_resources | 80 self._static_resources = static_resources |
| 77 self._api_data_source = api_data_source | 81 self._api_data_source = api_data_source |
| 82 self._api_list_data_source = api_list_data_source |
| 78 self._intro_data_source = intro_data_source | 83 self._intro_data_source = intro_data_source |
| 79 self._samples_data_source = samples_data_source | 84 self._samples_data_source = samples_data_source |
| 80 self._cache = cache | 85 self._cache = cache |
| 81 self._base_paths = base_paths | 86 self._base_paths = base_paths |
| 82 self._request = request | 87 self._request = request |
| 83 | 88 |
| 84 def Render(self, template_name): | 89 def Render(self, template_name): |
| 85 """This method will render a template named |template_name|, fetching all | 90 """This method will render a template named |template_name|, fetching all |
| 86 the partial templates needed from |self._cache|. Partials are retrieved | 91 the partial templates needed from |self._cache|. Partials are retrieved |
| 87 from the TemplateDataSource with the |get| method. | 92 from the TemplateDataSource with the |get| method. |
| 88 """ | 93 """ |
| 89 template = self.get(template_name) | 94 template = self.get(template_name) |
| 90 if not template: | 95 if not template: |
| 91 return '' | 96 return '' |
| 92 # TODO error handling | 97 # TODO error handling |
| 93 return template.render({ | 98 return template.render({ |
| 99 'api_list': self._api_list_data_source, |
| 94 'apis': self._api_data_source, | 100 'apis': self._api_data_source, |
| 95 'branchInfo': self._branch_info, | 101 'branchInfo': self._branch_info, |
| 96 'intros': self._intro_data_source, | 102 'intros': self._intro_data_source, |
| 97 'partials': self, | 103 'partials': self, |
| 98 'samples': self._samples_data_source, | 104 'samples': self._samples_data_source, |
| 99 'static': self._static_resources | 105 'static': self._static_resources |
| 100 }).text | 106 }).text |
| 101 | 107 |
| 102 def __getitem__(self, key): | 108 def __getitem__(self, key): |
| 103 return self.get(key) | 109 return self.get(key) |
| 104 | 110 |
| 105 def get(self, key): | 111 def get(self, key): |
| 106 real_path = FormatKey(key) | 112 real_path = FormatKey(key) |
| 107 for base_path in self._base_paths: | 113 for base_path in self._base_paths: |
| 108 try: | 114 try: |
| 109 return self._cache.GetFromFile(base_path + '/' + real_path) | 115 return self._cache.GetFromFile(base_path + '/' + real_path) |
| 110 except Exception: | 116 except Exception: |
| 111 pass | 117 pass |
| 112 return None | 118 return None |
| OLD | NEW |