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 os | |
7 import traceback | 6 import traceback |
8 | 7 |
9 from branch_utility import BranchUtility | 8 from branch_utility import BranchUtility |
10 import compiled_file_system as compiled_fs | |
11 from docs_server_utils import FormatKey | 9 from docs_server_utils import FormatKey |
12 from file_system import FileNotFoundError | 10 from file_system import FileNotFoundError |
13 from third_party.handlebar import Handlebar | 11 from third_party.handlebar import Handlebar |
14 import url_constants | 12 import url_constants |
15 | 13 |
16 EXTENSIONS_URL = '/chrome/extensions' | 14 EXTENSIONS_URL = '/chrome/extensions' |
17 | 15 |
18 def _MakeChannelDict(channel_name): | 16 def _MakeChannelDict(channel_name, path): |
17 splitpath = path.split('/') | |
19 channel_dict = { | 18 channel_dict = { |
20 'channels': [{'name': name} for name in BranchUtility.GetAllChannelNames()], | 19 'channels': [{'name': name} for name in BranchUtility.GetAllChannelNames()], |
21 'current': channel_name | 20 'current': channel_name |
22 } | 21 } |
22 | |
23 for channel in channel_dict['channels']: | 23 for channel in channel_dict['channels']: |
24 if channel['name'] == channel_name: | 24 if channel['name'] == channel_name: |
25 channel['isCurrent'] = True | 25 channel['isCurrent'] = True |
26 if splitpath[0] in ['extensions', 'apps']: | |
27 channel_dict['base_url'] = '/%s/%s/' % (channel_name, splitpath[0]) | |
28 # else: | |
not at google - send to devlin
2013/05/10 04:14:14
what is this? getting late, running out of brainpo
jshumway
2013/05/11 02:37:08
This makes an absolute base URL available to all t
| |
29 # channel_dict['base_url'] = '/%s/' % channel_name | |
26 return channel_dict | 30 return channel_dict |
27 | 31 |
28 class TemplateDataSource(object): | 32 class TemplateDataSource(object): |
29 """Renders Handlebar templates, providing them with the context in which to | 33 """Renders Handlebar templates, providing them with the context in which to |
30 render. | 34 render. |
31 | 35 |
32 Also acts as a data source itself, providing partial Handlebar templates to | 36 Also acts as a data source itself, providing partial Handlebar templates to |
33 those it renders. | 37 those it renders. |
34 | 38 |
35 Each instance of TemplateDataSource is bound to a Request so that it can | 39 Each instance of TemplateDataSource is bound to a Request so that it can |
36 render templates with request-specific data (such as Accept-Language); use | 40 render templates with request-specific data (such as Accept-Language); use |
37 a Factory to cheaply construct these. | 41 a Factory to cheaply construct these. |
38 """ | 42 """ |
39 | 43 |
40 class Factory(object): | 44 class Factory(object): |
41 """A factory to create lightweight TemplateDataSource instances bound to | 45 """A factory to create lightweight TemplateDataSource instances bound to |
42 individual Requests. | 46 individual Requests. |
43 """ | 47 """ |
44 def __init__(self, | 48 def __init__(self, |
45 channel_name, | 49 channel_name, |
46 api_data_source_factory, | 50 api_data_source_factory, |
47 api_list_data_source_factory, | 51 api_list_data_source_factory, |
48 intro_data_source_factory, | 52 intro_data_source_factory, |
49 samples_data_source_factory, | 53 samples_data_source_factory, |
50 sidenav_data_source_factory, | 54 sidenav_data_source_factory, |
51 compiled_fs_factory, | 55 compiled_fs_factory, |
52 ref_resolver_factory, | 56 ref_resolver_factory, |
57 manifest_data_source, | |
53 public_template_path, | 58 public_template_path, |
54 private_template_path): | 59 private_template_path): |
55 self._branch_info = _MakeChannelDict(channel_name) | 60 self.channel_name = channel_name |
56 self._api_data_source_factory = api_data_source_factory | 61 self._api_data_source_factory = api_data_source_factory |
57 self._api_list_data_source_factory = api_list_data_source_factory | 62 self._api_list_data_source_factory = api_list_data_source_factory |
58 self._intro_data_source_factory = intro_data_source_factory | 63 self._intro_data_source_factory = intro_data_source_factory |
59 self._samples_data_source_factory = samples_data_source_factory | 64 self._samples_data_source_factory = samples_data_source_factory |
60 self._sidenav_data_source_factory = sidenav_data_source_factory | 65 self._sidenav_data_source_factory = sidenav_data_source_factory |
61 self._cache = compiled_fs_factory.Create(self._CreateTemplate, | 66 self._cache = compiled_fs_factory.Create(self._CreateTemplate, |
62 TemplateDataSource) | 67 TemplateDataSource) |
63 self._ref_resolver = ref_resolver_factory.Create() | 68 self._ref_resolver = ref_resolver_factory.Create() |
64 self._public_template_path = public_template_path | 69 self._public_template_path = public_template_path |
65 self._private_template_path = private_template_path | 70 self._private_template_path = private_template_path |
66 self._static_resources = '/%s/static' % channel_name | 71 self._static_resources = '/%s/static' % channel_name |
72 self._manifest_data_source = manifest_data_source | |
67 | 73 |
68 def _CreateTemplate(self, template_name, text): | 74 def _CreateTemplate(self, template_name, text): |
69 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) | 75 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) |
70 | 76 |
71 def Create(self, request, path): | 77 def Create(self, request, path): |
72 """Returns a new TemplateDataSource bound to |request|. | 78 """Returns a new TemplateDataSource bound to |request|. |
73 """ | 79 """ |
74 return TemplateDataSource( | 80 return TemplateDataSource( |
75 self._branch_info, | 81 _MakeChannelDict(self.channel_name, path), |
76 self._api_data_source_factory.Create(request), | 82 self._api_data_source_factory.Create(request), |
77 self._api_list_data_source_factory.Create(), | 83 self._api_list_data_source_factory.Create(), |
78 self._intro_data_source_factory.Create(), | 84 self._intro_data_source_factory.Create(), |
79 self._samples_data_source_factory.Create(request), | 85 self._samples_data_source_factory.Create(request), |
80 self._sidenav_data_source_factory.Create(path), | 86 self._sidenav_data_source_factory.Create(path), |
81 self._cache, | 87 self._cache, |
88 self._manifest_data_source, | |
82 self._public_template_path, | 89 self._public_template_path, |
83 self._private_template_path, | 90 self._private_template_path, |
84 self._static_resources) | 91 self._static_resources) |
85 | 92 |
86 def __init__(self, | 93 def __init__(self, |
87 branch_info, | 94 branch_info, |
88 api_data_source, | 95 api_data_source, |
89 api_list_data_source, | 96 api_list_data_source, |
90 intro_data_source, | 97 intro_data_source, |
91 samples_data_source, | 98 samples_data_source, |
92 sidenav_data_source, | 99 sidenav_data_source, |
93 cache, | 100 cache, |
101 manifest_data_source, | |
94 public_template_path, | 102 public_template_path, |
95 private_template_path, | 103 private_template_path, |
96 static_resources): | 104 static_resources): |
97 self._branch_info = branch_info | 105 self._branch_info = branch_info |
98 self._api_list_data_source = api_list_data_source | 106 self._api_list_data_source = api_list_data_source |
99 self._intro_data_source = intro_data_source | 107 self._intro_data_source = intro_data_source |
100 self._samples_data_source = samples_data_source | 108 self._samples_data_source = samples_data_source |
101 self._api_data_source = api_data_source | 109 self._api_data_source = api_data_source |
102 self._sidenav_data_source = sidenav_data_source | 110 self._sidenav_data_source = sidenav_data_source |
103 self._cache = cache | 111 self._cache = cache |
104 self._public_template_path = public_template_path | 112 self._public_template_path = public_template_path |
105 self._private_template_path = private_template_path | 113 self._private_template_path = private_template_path |
106 self._static_resources = static_resources | 114 self._static_resources = static_resources |
115 self._manifest_data_source = manifest_data_source | |
107 | 116 |
108 def Render(self, template_name): | 117 def Render(self, template_name): |
109 """This method will render a template named |template_name|, fetching all | 118 """This method will render a template named |template_name|, fetching all |
110 the partial templates needed from |self._cache|. Partials are retrieved | 119 the partial templates needed from |self._cache|. Partials are retrieved |
111 from the TemplateDataSource with the |get| method. | 120 from the TemplateDataSource with the |get| method. |
112 """ | 121 """ |
113 template = self.GetTemplate(self._public_template_path, template_name) | 122 template = self.GetTemplate(self._public_template_path, template_name) |
114 if not template: | 123 if not template: |
115 return None | 124 return None |
116 # TODO error handling | 125 # TODO error handling |
117 render_data = template.render({ | 126 render_data = template.render({ |
118 'api_list': self._api_list_data_source, | 127 'api_list': self._api_list_data_source, |
119 'apis': self._api_data_source, | 128 'apis': self._api_data_source, |
120 'branchInfo': self._branch_info, | 129 'branchInfo': self._branch_info, |
121 'intros': self._intro_data_source, | 130 'intros': self._intro_data_source, |
122 'sidenavs': self._sidenav_data_source, | 131 'sidenavs': self._sidenav_data_source, |
123 'partials': self, | 132 'partials': self, |
133 'manifest_source': self._manifest_data_source, | |
124 'samples': self._samples_data_source, | 134 'samples': self._samples_data_source, |
125 'static': self._static_resources, | 135 'static': self._static_resources, |
126 'app': 'app', | 136 'app': 'app', |
127 'extension': 'extension', | 137 'extension': 'extension', |
128 'apps_title': 'Apps', | 138 'apps_title': 'Apps', |
129 'extensions_title': 'Extensions', | 139 'extensions_title': 'Extensions', |
130 'apps_samples_url': url_constants.GITHUB_BASE, | 140 'apps_samples_url': url_constants.GITHUB_BASE, |
131 # TODO(kalman): this is wrong, it's always getting from trunk, but meh | 141 # TODO(kalman): this is wrong, it's always getting from trunk, but meh |
132 # it hardly ever shows up (only in the "cannot fetch samples" message). | 142 # it hardly ever shows up (only in the "cannot fetch samples" message). |
133 # In fact I don't even know if it can show up anymore due the samples data | 143 # In fact I don't even know if it can show up anymore due the samples data |
134 # being persisent. In any case, when the channel distinctions are gone | 144 # being persisent. In any case, when the channel distinctions are gone |
135 # this can go away, so, double meh. | 145 # this can go away, so, double meh. |
136 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, | 146 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, |
137 'true': True, | 147 'true': True, |
138 'false': False | 148 'false': False |
139 }) | 149 }) |
140 if render_data.errors: | 150 if render_data.errors: |
141 logging.error('Handlebar error(s) rendering %s:\n%s' % | 151 logging.error('Handlebar error(s) rendering %s:\n%s' % |
142 (template_name, ' \n'.join(render_data.errors))) | 152 (template_name, ' \n'.join(render_data.errors))) |
143 return render_data.text | 153 return render_data.text |
144 | 154 |
145 def get(self, key): | 155 def get(self, key): |
146 return self.GetTemplate(self._private_template_path, key) | 156 return self.GetTemplate(self._private_template_path, key) |
147 | 157 |
148 def GetTemplate(self, base_path, template_name): | 158 def GetTemplate(self, base_path, template_name): |
149 try: | 159 try: |
150 return self._cache.GetFromFile( | 160 return self._cache.GetFromFile( |
151 '/'.join((base_path, FormatKey(template_name)))) | 161 '/'.join((base_path, FormatKey(template_name)))) |
152 except FileNotFoundError as e: | 162 except FileNotFoundError: |
153 logging.warning(traceback.format_exc()) | 163 logging.warning(traceback.format_exc()) |
154 return None | 164 return None |
OLD | NEW |