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 docs_server_utils import FormatKey | 7 from docs_server_utils import FormatKey |
8 from file_system import FileNotFoundError | 8 from file_system import FileNotFoundError |
9 from third_party.handlebar import Handlebar | 9 from third_party.handlebar import Handlebar |
10 import url_constants | 10 import url_constants |
11 | 11 |
12 _EXTENSIONS_URL = '/chrome/extensions' | 12 _EXTENSIONS_URL = '/chrome/extensions' |
13 | 13 |
14 class TemplateDataSource(object): | 14 class TemplateDataSource(object): |
15 """Renders Handlebar templates, providing them with the context in which to | 15 '''Renders Handlebar templates, providing them with the context in which to |
16 render. | 16 render. |
17 | 17 |
18 Also acts as a data source itself, providing partial Handlebar templates to | 18 Also acts as a data source itself, providing partial Handlebar templates to |
19 those it renders. | 19 those it renders. |
20 | 20 |
21 Each instance of TemplateDataSource is bound to a Request so that it can | 21 Each instance of TemplateDataSource is bound to a Request so that it can |
22 render templates with request-specific data (such as Accept-Language); use | 22 render templates with request-specific data (such as Accept-Language); use |
23 a Factory to cheaply construct these. | 23 a Factory to cheaply construct these. |
24 """ | 24 ''' |
25 | 25 |
26 class Factory(object): | 26 class Factory(object): |
27 """A factory to create lightweight TemplateDataSource instances bound to | 27 '''A factory to create lightweight TemplateDataSource instances bound to |
28 individual Requests. | 28 individual Requests. |
29 """ | 29 ''' |
30 def __init__(self, | 30 def __init__(self, |
31 api_data_source_factory, | 31 api_data_source_factory, |
32 api_list_data_source_factory, | 32 api_list_data_source_factory, |
33 intro_data_source_factory, | 33 intro_data_source_factory, |
34 samples_data_source_factory, | 34 samples_data_source_factory, |
35 sidenav_data_source_factory, | |
36 compiled_fs_factory, | 35 compiled_fs_factory, |
37 ref_resolver_factory, | 36 ref_resolver_factory, |
38 permissions_data_source, | 37 permissions_data_source, |
39 public_template_path, | 38 public_template_path, |
40 private_template_path, | 39 private_template_path, |
41 base_path, | 40 base_path): |
42 data_sources): | |
43 self._api_data_source_factory = api_data_source_factory | 41 self._api_data_source_factory = api_data_source_factory |
44 self._api_list_data_source_factory = api_list_data_source_factory | 42 self._api_list_data_source_factory = api_list_data_source_factory |
45 self._intro_data_source_factory = intro_data_source_factory | 43 self._intro_data_source_factory = intro_data_source_factory |
46 self._samples_data_source_factory = samples_data_source_factory | 44 self._samples_data_source_factory = samples_data_source_factory |
47 self._sidenav_data_source_factory = sidenav_data_source_factory | |
48 self._cache = compiled_fs_factory.Create(self._CreateTemplate, | 45 self._cache = compiled_fs_factory.Create(self._CreateTemplate, |
49 TemplateDataSource) | 46 TemplateDataSource) |
50 self._ref_resolver = ref_resolver_factory.Create() | 47 self._ref_resolver = ref_resolver_factory.Create() |
51 self._permissions_data_source = permissions_data_source | 48 self._permissions_data_source = permissions_data_source |
52 self._public_template_path = public_template_path | 49 self._public_template_path = public_template_path |
53 self._private_template_path = private_template_path | 50 self._private_template_path = private_template_path |
54 self._base_path = base_path | 51 self._base_path = base_path |
55 self._data_sources = data_sources | |
56 | 52 |
57 def _CreateTemplate(self, template_name, text): | 53 def _CreateTemplate(self, template_name, text): |
58 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) | 54 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) |
59 | 55 |
60 def Create(self, request, path): | 56 def Create(self, request, data_sources): |
61 """Returns a new TemplateDataSource bound to |request|. | 57 '''Bind a new TemplateDataSource to a servlet.Request |request| and a |
62 """ | 58 dictionary of instantiated DataSources*. Keys in |data_sources| are the |
| 59 names that templates will use to access the corresponding DataSource. |
| 60 |
| 61 * this is temporary until the data source registry transition is complete. |
| 62 ''' |
63 return TemplateDataSource( | 63 return TemplateDataSource( |
64 self._api_data_source_factory.Create(request), | 64 self._api_data_source_factory.Create(request), |
65 self._api_list_data_source_factory.Create(), | 65 self._api_list_data_source_factory.Create(), |
66 self._intro_data_source_factory.Create(), | 66 self._intro_data_source_factory.Create(), |
67 self._samples_data_source_factory.Create(request), | 67 self._samples_data_source_factory.Create(request), |
68 self._sidenav_data_source_factory.Create(path), | |
69 self._cache, | 68 self._cache, |
70 self._permissions_data_source, | 69 self._permissions_data_source, |
71 self._public_template_path, | 70 self._public_template_path, |
72 self._private_template_path, | 71 self._private_template_path, |
73 self._base_path, | 72 self._base_path, |
74 self._data_sources) | 73 data_sources) |
75 | 74 |
76 def __init__(self, | 75 def __init__(self, |
77 api_data_source, | 76 api_data_source, |
78 api_list_data_source, | 77 api_list_data_source, |
79 intro_data_source, | 78 intro_data_source, |
80 samples_data_source, | 79 samples_data_source, |
81 sidenav_data_source, | |
82 cache, | 80 cache, |
83 permissions_data_source, | 81 permissions_data_source, |
84 public_template_path, | 82 public_template_path, |
85 private_template_path, | 83 private_template_path, |
86 base_path, | 84 base_path, |
87 data_sources): | 85 data_sources): |
88 self._api_list_data_source = api_list_data_source | 86 self._api_list_data_source = api_list_data_source |
89 self._intro_data_source = intro_data_source | 87 self._intro_data_source = intro_data_source |
90 self._samples_data_source = samples_data_source | 88 self._samples_data_source = samples_data_source |
91 self._api_data_source = api_data_source | 89 self._api_data_source = api_data_source |
92 self._sidenav_data_source = sidenav_data_source | |
93 self._cache = cache | 90 self._cache = cache |
94 self._public_template_path = public_template_path | 91 self._public_template_path = public_template_path |
95 self._private_template_path = private_template_path | 92 self._private_template_path = private_template_path |
96 self._permissions_data_source = permissions_data_source | 93 self._permissions_data_source = permissions_data_source |
97 self._base_path = base_path | 94 self._base_path = base_path |
98 self._data_sources = data_sources | 95 self._data_sources = data_sources |
99 | 96 |
100 def Render(self, template_name): | 97 def Render(self, template_name): |
101 """This method will render a template named |template_name|, fetching all | 98 '''This method will render a template named |template_name|, fetching all |
102 the partial templates needed from |self._cache|. Partials are retrieved | 99 the partial templates needed from |self._cache|. Partials are retrieved |
103 from the TemplateDataSource with the |get| method. | 100 from the TemplateDataSource with the |get| method. |
104 """ | 101 ''' |
105 template = self.GetTemplate(self._public_template_path, template_name) | 102 template = self.GetTemplate(self._public_template_path, template_name) |
106 if template is None: | 103 if template is None: |
107 return None | 104 return None |
108 # TODO error handling | 105 # TODO error handling |
109 render_context = { | 106 render_context = { |
110 'api_list': self._api_list_data_source, | 107 'api_list': self._api_list_data_source, |
111 'apis': self._api_data_source, | 108 'apis': self._api_data_source, |
112 'intros': self._intro_data_source, | 109 'intros': self._intro_data_source, |
113 'sidenavs': self._sidenav_data_source, | |
114 'partials': self, | 110 'partials': self, |
115 'permissions': self._permissions_data_source, | 111 'permissions': self._permissions_data_source, |
116 'samples': self._samples_data_source, | 112 'samples': self._samples_data_source, |
117 'apps_samples_url': url_constants.GITHUB_BASE, | 113 'apps_samples_url': url_constants.GITHUB_BASE, |
118 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, | 114 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, |
119 'static': self._base_path + '/static', | 115 'static': self._base_path + '/static', |
120 'true': True, | 116 'true': True, |
121 'false': False | 117 'false': False |
122 } | 118 } |
123 render_context.update(self._data_sources) | 119 render_context.update(self._data_sources) |
124 render_data = template.render(render_context) | 120 render_data = template.render(render_context) |
125 if render_data.errors: | 121 if render_data.errors: |
126 logging.error('Handlebar error(s) rendering %s:\n%s' % | 122 logging.error('Handlebar error(s) rendering %s:\n%s' % |
127 (template_name, ' \n'.join(render_data.errors))) | 123 (template_name, ' \n'.join(render_data.errors))) |
128 return render_data.text | 124 return render_data.text |
129 | 125 |
130 def get(self, key): | 126 def get(self, key): |
131 return self.GetTemplate(self._private_template_path, key) | 127 return self.GetTemplate(self._private_template_path, key) |
132 | 128 |
133 def GetTemplate(self, base_path, template_name): | 129 def GetTemplate(self, base_path, template_name): |
134 try: | 130 try: |
135 return self._cache.GetFromFile( | 131 return self._cache.GetFromFile( |
136 '/'.join((base_path, FormatKey(template_name)))) | 132 '/'.join((base_path, FormatKey(template_name)))) |
137 except FileNotFoundError: | 133 except FileNotFoundError: |
138 return None | 134 return None |
OLD | NEW |