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 path_utils import FormatKey | 7 from path_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' |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 class Factory(object): | 36 class Factory(object): |
37 """A factory to create lightweight TemplateDataSource instances bound to | 37 """A factory to create lightweight TemplateDataSource instances bound to |
38 individual Requests. | 38 individual Requests. |
39 """ | 39 """ |
40 def __init__(self, | 40 def __init__(self, |
41 branch, | 41 branch, |
42 api_data_source, | 42 api_data_source, |
43 api_list_data_source, | 43 api_list_data_source, |
44 intro_data_source, | 44 intro_data_source, |
45 samples_data_source, | 45 samples_data_source_factory, |
46 cache_builder, | 46 cache_builder, |
47 public_template_path, | 47 public_template_path, |
48 private_template_path): | 48 private_template_path): |
49 self._branch_info = _MakeBranchDict(branch) | 49 self._branch_info = _MakeBranchDict(branch) |
50 self._static_resources = ((('/' + branch) if branch != 'local' else '') + | 50 self._static_resources = ((('/' + branch) if branch != 'local' else '') + |
51 '/static') | 51 '/static') |
52 self._api_data_source = api_data_source | 52 self._api_data_source = api_data_source |
53 self._api_list_data_source = api_list_data_source | 53 self._api_list_data_source = api_list_data_source |
54 self._intro_data_source = intro_data_source | 54 self._intro_data_source = intro_data_source |
55 self._samples_data_source = samples_data_source | 55 self._samples_data_source_factory = samples_data_source_factory |
56 self._cache = cache_builder.build(Handlebar) | 56 self._cache = cache_builder.build(Handlebar) |
57 self._public_template_path = public_template_path | 57 self._public_template_path = public_template_path |
58 self._private_template_path = private_template_path | 58 self._private_template_path = private_template_path |
59 | 59 |
60 def Create(self, request): | 60 def Create(self, request): |
61 """Returns a new TemplateDataSource bound to |request|. | 61 """Returns a new TemplateDataSource bound to |request|. |
62 """ | 62 """ |
63 return TemplateDataSource(self._branch_info, | 63 return TemplateDataSource(self._branch_info, |
64 self._static_resources, | 64 self._static_resources, |
65 self._api_data_source, | 65 self._api_data_source, |
66 self._api_list_data_source, | 66 self._api_list_data_source, |
67 self._intro_data_source, | 67 self._intro_data_source, |
68 self._samples_data_source, | 68 self._samples_data_source_factory, |
69 self._cache, | 69 self._cache, |
70 self._public_template_path, | 70 self._public_template_path, |
71 self._private_template_path, | 71 self._private_template_path, |
72 request) | 72 request) |
73 | 73 |
74 def __init__(self, | 74 def __init__(self, |
75 branch_info, | 75 branch_info, |
76 static_resources, | 76 static_resources, |
77 api_data_source, | 77 api_data_source, |
78 api_list_data_source, | 78 api_list_data_source, |
79 intro_data_source, | 79 intro_data_source, |
80 samples_data_source, | 80 samples_data_source_factory, |
81 cache, | 81 cache, |
82 public_template_path, | 82 public_template_path, |
83 private_template_path, | 83 private_template_path, |
84 request): | 84 request): |
85 self._branch_info = branch_info | 85 self._branch_info = branch_info |
86 self._static_resources = static_resources | 86 self._static_resources = static_resources |
87 self._api_data_source = api_data_source | 87 self._api_data_source = api_data_source |
88 self._api_list_data_source = api_list_data_source | 88 self._api_list_data_source = api_list_data_source |
89 self._intro_data_source = intro_data_source | 89 self._intro_data_source = intro_data_source |
90 self._samples_data_source = samples_data_source | 90 self._samples_data_source = samples_data_source_factory.Create(request) |
91 self._cache = cache | 91 self._cache = cache |
92 self._public_template_path = public_template_path | 92 self._public_template_path = public_template_path |
93 self._private_template_path = private_template_path | 93 self._private_template_path = private_template_path |
94 self._request = request | 94 self._request = request |
95 | 95 |
96 def Render(self, template_name): | 96 def Render(self, template_name): |
97 """This method will render a template named |template_name|, fetching all | 97 """This method will render a template named |template_name|, fetching all |
98 the partial templates needed from |self._cache|. Partials are retrieved | 98 the partial templates needed from |self._cache|. Partials are retrieved |
99 from the TemplateDataSource with the |get| method. | 99 from the TemplateDataSource with the |get| method. |
100 """ | 100 """ |
(...skipping 17 matching lines...) Expand all Loading... |
118 def get(self, key): | 118 def get(self, key): |
119 return self.GetTemplate(self._private_template_path, key) | 119 return self.GetTemplate(self._private_template_path, key) |
120 | 120 |
121 def GetTemplate(self, base_path, template_name): | 121 def GetTemplate(self, base_path, template_name): |
122 real_path = FormatKey(template_name) | 122 real_path = FormatKey(template_name) |
123 try: | 123 try: |
124 return self._cache.GetFromFile(base_path + '/' + real_path) | 124 return self._cache.GetFromFile(base_path + '/' + real_path) |
125 except Exception as e: | 125 except Exception as e: |
126 logging.warn(e) | 126 logging.warn(e) |
127 return None | 127 return None |
OLD | NEW |