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 import time |
6 | 7 |
7 from docs_server_utils import FormatKey | 8 from docs_server_utils import FormatKey |
8 from file_system import FileNotFoundError | 9 from file_system import FileNotFoundError |
| 10 from request_scoped_fs_cache import RequestScopedFsCache |
9 from third_party.handlebar import Handlebar | 11 from third_party.handlebar import Handlebar |
10 | 12 |
11 EXTENSIONS_URL = '/chrome/extensions' | 13 EXTENSIONS_URL = '/chrome/extensions' |
12 | 14 |
13 def _MakeChannelDict(channel_name): | 15 def _MakeChannelDict(channel_name): |
14 return { | 16 return { |
15 'showWarning': channel_name != 'stable', | 17 'showWarning': channel_name != 'stable', |
16 'channels': [ | 18 'channels': [ |
17 { 'name': 'Stable', 'path': 'stable' }, | 19 { 'name': 'Stable', 'path': 'stable' }, |
18 { 'name': 'Dev', 'path': 'dev' }, | 20 { 'name': 'Dev', 'path': 'dev' }, |
(...skipping 15 matching lines...) Expand all Loading... |
34 a Factory to cheaply construct these. | 36 a Factory to cheaply construct these. |
35 """ | 37 """ |
36 | 38 |
37 class Factory(object): | 39 class Factory(object): |
38 """A factory to create lightweight TemplateDataSource instances bound to | 40 """A factory to create lightweight TemplateDataSource instances bound to |
39 individual Requests. | 41 individual Requests. |
40 """ | 42 """ |
41 def __init__(self, | 43 def __init__(self, |
42 channel_name, | 44 channel_name, |
43 api_data_source_factory, | 45 api_data_source_factory, |
44 api_list_data_source, | 46 api_list_data_source_factory, |
45 intro_data_source, | 47 intro_data_source_factory, |
46 samples_data_source_factory, | 48 samples_data_source_factory, |
47 cache_builder, | 49 cache_builder, |
48 public_template_path, | 50 public_template_path, |
49 private_template_path): | 51 private_template_path): |
50 self._branch_info = _MakeChannelDict(channel_name) | 52 self._branch_info = _MakeChannelDict(channel_name) |
51 self._api_data_source_factory = api_data_source_factory | 53 self._api_data_source_factory = api_data_source_factory |
52 self._api_list_data_source = api_list_data_source | 54 self._api_list_data_source_factory = api_list_data_source_factory |
53 self._intro_data_source = intro_data_source | 55 self._intro_data_source_factory = intro_data_source_factory |
54 self._samples_data_source_factory = samples_data_source_factory | 56 self._samples_data_source_factory = samples_data_source_factory |
55 self._cache = cache_builder.build(Handlebar) | 57 self._cache = cache_builder.build(Handlebar) |
56 self._public_template_path = public_template_path | 58 self._public_template_path = public_template_path |
57 self._private_template_path = private_template_path | 59 self._private_template_path = private_template_path |
58 self._static_resources = ( | 60 self._static_resources = ( |
59 (('/' + channel_name) if channel_name != 'local' else '') + '/static') | 61 (('/' + channel_name) if channel_name != 'local' else '') + '/static') |
60 | 62 |
61 def Create(self, request): | 63 def Create(self, request): |
62 """Returns a new TemplateDataSource bound to |request|. | 64 """Returns a new TemplateDataSource bound to |request|. |
63 """ | 65 """ |
64 return TemplateDataSource( | 66 return TemplateDataSource( |
65 self._branch_info, | 67 self._branch_info, |
66 self._api_data_source_factory.Create(request), | 68 self._api_data_source_factory.Create(request), |
67 self._api_list_data_source, | 69 self._api_list_data_source_factory.Create(), |
68 self._intro_data_source, | 70 self._intro_data_source_factory.Create(), |
69 self._samples_data_source_factory.Create(request), | 71 self._samples_data_source_factory.Create(request), |
70 self._cache, | 72 self._cache.ScopeToRequest(), |
71 self._public_template_path, | 73 self._public_template_path, |
72 self._private_template_path, | 74 self._private_template_path, |
73 self._static_resources, | 75 self._static_resources, |
74 request) | 76 request) |
75 | 77 |
76 def __init__(self, | 78 def __init__(self, |
77 branch_info, | 79 branch_info, |
78 api_data_source, | 80 api_data_source, |
79 api_list_data_source, | 81 api_list_data_source, |
80 intro_data_source, | 82 intro_data_source, |
81 samples_data_source, | 83 samples_data_source, |
82 cache, | 84 cache, |
83 public_template_path, | 85 public_template_path, |
84 private_template_path, | 86 private_template_path, |
85 static_resources, | 87 static_resources, |
86 request): | 88 request): |
87 self._branch_info = branch_info | 89 self._branch_info = branch_info |
88 self._api_list_data_source = api_list_data_source | 90 self._api_list_data_source = api_list_data_source |
89 self._intro_data_source = intro_data_source | 91 self._intro_data_source = intro_data_source |
90 self._samples_data_source = samples_data_source | 92 self._samples_data_source = samples_data_source |
91 self._api_data_source = api_data_source | 93 self._api_data_source = api_data_source |
92 self._cache = cache | 94 self._cache = cache |
93 self._public_template_path = public_template_path | 95 self._public_template_path = public_template_path |
94 self._private_template_path = private_template_path | 96 self._private_template_path = private_template_path |
95 self._static_resources = static_resources | 97 self._static_resources = static_resources |
96 self._request = request | 98 self._request = request |
97 | 99 |
98 def Render(self, template_name): | 100 def Render(self, template_name): |
| 101 start_time = time.time() |
| 102 try: |
| 103 return self._DoRender(template_name) |
| 104 finally: |
| 105 logging.info("RENDER: %sms", (time.time() - start_time) * 1000) |
| 106 |
| 107 def _DoRender(self, template_name): |
99 """This method will render a template named |template_name|, fetching all | 108 """This method will render a template named |template_name|, fetching all |
100 the partial templates needed from |self._cache|. Partials are retrieved | 109 the partial templates needed from |self._cache|. Partials are retrieved |
101 from the TemplateDataSource with the |get| method. | 110 from the TemplateDataSource with the |get| method. |
102 """ | 111 """ |
103 template = self.GetTemplate(self._public_template_path, template_name) | 112 template = self.GetTemplate(self._public_template_path, template_name) |
104 if not template: | 113 if not template: |
105 return '' | 114 return '' |
106 # TODO error handling | 115 # TODO error handling |
107 return template.render({ | 116 return template.render({ |
108 'api_list': self._api_list_data_source, | 117 'api_list': self._api_list_data_source, |
109 'apis': self._api_data_source, | 118 'apis': self._api_data_source, |
110 'branchInfo': self._branch_info, | 119 'branchInfo': self._branch_info, |
111 'intros': self._intro_data_source, | 120 'intros': self._intro_data_source, |
112 'partials': self, | 121 'partials': self, |
113 'samples': self._samples_data_source, | 122 'samples': self._samples_data_source, |
114 'static': self._static_resources, | 123 'static': self._static_resources, |
115 'apps_title': 'Apps', | 124 'apps_title': 'Apps', |
116 'extensions_title': 'Extensions', | 125 'extensions_title': 'Extensions', |
117 'true': True, | 126 'true': True, |
118 'false': False | 127 'false': False |
119 }).text | 128 }).text |
120 | 129 |
121 def __getitem__(self, key): | 130 def __getitem__(self, key): |
122 return self.get(key) | 131 return self.get(key) |
123 | 132 |
124 def get(self, key): | 133 def get(self, key): |
| 134 start_time = time.time() |
| 135 try: |
| 136 return self._do_get(key) |
| 137 finally: |
| 138 logging.info("TemplateDataSource: %sms", (time.time() - start_time) * 1000
) |
| 139 |
| 140 def _do_get(self, key): |
125 return self.GetTemplate(self._private_template_path, key) | 141 return self.GetTemplate(self._private_template_path, key) |
126 | 142 |
127 def GetTemplate(self, base_path, template_name): | 143 def GetTemplate(self, base_path, template_name): |
128 real_path = FormatKey(template_name) | 144 real_path = FormatKey(template_name) |
129 try: | 145 try: |
130 return self._cache.GetFromFile(base_path + '/' + real_path) | 146 return self._cache.GetFromFile(base_path + '/' + real_path) |
131 except FileNotFoundError as e: | 147 except FileNotFoundError as e: |
132 logging.error(e) | 148 logging.error(e) |
133 return None | 149 return None |
OLD | NEW |