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 | |
| 6 | |
| 5 from path_utils import FormatKey | 7 from path_utils import FormatKey |
| 6 from third_party.handlebar import Handlebar | 8 from third_party.handlebar import Handlebar |
| 7 | 9 |
| 8 EXTENSIONS_URL = '/chrome/extensions' | 10 EXTENSIONS_URL = '/chrome/extensions' |
| 9 | 11 |
| 10 def _MakeBranchDict(branch): | 12 def _MakeBranchDict(branch): |
| 11 return { | 13 return { |
| 12 'showWarning': branch != 'stable', | 14 'showWarning': branch != 'stable', |
| 13 'branches': [ | 15 'branches': [ |
| 14 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' }, | 16 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' }, |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 34 class Factory(object): | 36 class Factory(object): |
| 35 """ A factory to create lightweight TemplateDataSource instances bound to | 37 """ A factory to create lightweight TemplateDataSource instances bound to |
| 36 individual Requests. | 38 individual Requests. |
| 37 """ | 39 """ |
| 38 def __init__(self, | 40 def __init__(self, |
| 39 branch, | 41 branch, |
| 40 api_data_source, | 42 api_data_source, |
| 41 intro_data_source, | 43 intro_data_source, |
| 42 samples_data_source, | 44 samples_data_source, |
| 43 cache_builder, | 45 cache_builder, |
| 44 base_paths): | 46 public_template_path, |
| 47 private_template_path): | |
| 45 self._branch_info = _MakeBranchDict(branch) | 48 self._branch_info = _MakeBranchDict(branch) |
| 46 self._static_resources = ((('/' + branch) if branch != 'local' else '') + | 49 self._static_resources = ((('/' + branch) if branch != 'local' else '') + |
| 47 '/static') | 50 '/static') |
| 48 self._api_data_source = api_data_source | 51 self._api_data_source = api_data_source |
| 49 self._intro_data_source = intro_data_source | 52 self._intro_data_source = intro_data_source |
| 50 self._samples_data_source = samples_data_source | 53 self._samples_data_source = samples_data_source |
| 51 self._cache = cache_builder.build(Handlebar) | 54 self._cache = cache_builder.build(Handlebar) |
| 52 self._base_paths = base_paths | 55 self._public_template_path = public_template_path |
| 56 self._private_template_path = private_template_path | |
| 53 | 57 |
| 54 def Create(self, request): | 58 def Create(self, request): |
| 55 """ Returns a new TemplateDataSource bound to |request|. | 59 """ Returns a new TemplateDataSource bound to |request|. |
| 56 """ | 60 """ |
| 57 return TemplateDataSource(self._branch_info, | 61 return TemplateDataSource(self._branch_info, |
| 58 self._static_resources, | 62 self._static_resources, |
| 59 self._api_data_source, | 63 self._api_data_source, |
| 60 self._intro_data_source, | 64 self._intro_data_source, |
| 61 self._samples_data_source, | 65 self._samples_data_source, |
| 62 self._cache, | 66 self._cache, |
| 63 self._base_paths, | 67 self._public_template_path, |
| 68 self._private_template_path, | |
| 64 request) | 69 request) |
| 65 | 70 |
| 66 def __init__(self, | 71 def __init__(self, |
| 67 branch_info, | 72 branch_info, |
| 68 static_resources, | 73 static_resources, |
| 69 api_data_source, | 74 api_data_source, |
| 70 intro_data_source, | 75 intro_data_source, |
| 71 samples_data_source, | 76 samples_data_source, |
| 72 cache, | 77 cache, |
| 73 base_paths, | 78 public_template_path, |
| 79 private_template_path, | |
| 74 request): | 80 request): |
| 75 self._branch_info = branch_info | 81 self._branch_info = branch_info |
| 76 self._static_resources = static_resources | 82 self._static_resources = static_resources |
| 77 self._api_data_source = api_data_source | 83 self._api_data_source = api_data_source |
| 78 self._intro_data_source = intro_data_source | 84 self._intro_data_source = intro_data_source |
| 79 self._samples_data_source = samples_data_source | 85 self._samples_data_source = samples_data_source |
| 80 self._cache = cache | 86 self._cache = cache |
| 81 self._base_paths = base_paths | 87 self._public_template_path = public_template_path |
| 88 self._private_template_path = private_template_path | |
| 82 self._request = request | 89 self._request = request |
| 83 | 90 |
| 84 def Render(self, template_name): | 91 def Render(self, template_name): |
| 85 """This method will render a template named |template_name|, fetching all | 92 """This method will render a template named |template_name|, fetching all |
| 86 the partial templates needed from |self._cache|. Partials are retrieved | 93 the partial templates needed from |self._cache|. Partials are retrieved |
| 87 from the TemplateDataSource with the |get| method. | 94 from the TemplateDataSource with the |get| method. |
| 88 """ | 95 """ |
| 89 template = self.get(template_name) | 96 template = self.get(template_name, self._public_template_path) |
| 90 if not template: | 97 if not template: |
| 91 return '' | 98 return '' |
| 92 # TODO error handling | 99 # TODO error handling |
| 93 return template.render({ | 100 return template.render({ |
| 94 'apis': self._api_data_source, | 101 'apis': self._api_data_source, |
| 95 'branchInfo': self._branch_info, | 102 'branchInfo': self._branch_info, |
| 96 'intros': self._intro_data_source, | 103 'intros': self._intro_data_source, |
| 97 'partials': self, | 104 'partials': self, |
| 98 'samples': self._samples_data_source, | 105 'samples': self._samples_data_source, |
| 99 'static': self._static_resources | 106 'static': self._static_resources |
| 100 }).text | 107 }).text |
| 101 | 108 |
| 102 def __getitem__(self, key): | 109 def __getitem__(self, key): |
| 103 return self.get(key) | 110 return self.get(key) |
| 104 | 111 |
| 105 def get(self, key): | 112 def get(self, key, use_path=None): |
|
not at google - send to devlin
2012/07/23 13:13:27
Have two methods rather than overloading, like
cduvall
2012/07/23 19:04:41
Done.
| |
| 106 real_path = FormatKey(key) | 113 real_path = FormatKey(key) |
| 107 for base_path in self._base_paths: | 114 if use_path is None: |
| 108 try: | 115 base_path = self._private_template_path |
| 109 return self._cache.GetFromFile(base_path + '/' + real_path) | 116 else: |
| 110 except Exception: | 117 base_path = use_path |
| 111 pass | 118 try: |
| 112 return None | 119 return self._cache.GetFromFile(base_path + '/' + real_path) |
| 120 except Exception as e: | |
| 121 logging.warn(e) | |
| 122 return None | |
| OLD | NEW |