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 |
(...skipping 16 matching lines...) Expand all Loading... |
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 compiled_fs_factory, | 35 compiled_fs_factory, |
36 ref_resolver_factory, | 36 ref_resolver_factory, |
37 permissions_data_source, | 37 features_bundle, |
38 public_template_path, | 38 public_template_path, |
39 private_template_path, | 39 private_template_path, |
40 base_path): | 40 base_path): |
41 self._api_data_source_factory = api_data_source_factory | 41 self._api_data_source_factory = api_data_source_factory |
42 self._api_list_data_source_factory = api_list_data_source_factory | 42 self._api_list_data_source_factory = api_list_data_source_factory |
43 self._intro_data_source_factory = intro_data_source_factory | 43 self._intro_data_source_factory = intro_data_source_factory |
44 self._samples_data_source_factory = samples_data_source_factory | 44 self._samples_data_source_factory = samples_data_source_factory |
45 self._cache = compiled_fs_factory.Create(self._CreateTemplate, | 45 self._cache = compiled_fs_factory.Create(self._CreateTemplate, |
46 TemplateDataSource) | 46 TemplateDataSource) |
47 self._ref_resolver = ref_resolver_factory.Create() | 47 self._ref_resolver = ref_resolver_factory.Create() |
48 self._permissions_data_source = permissions_data_source | 48 self._features_bundle = features_bundle |
49 self._public_template_path = public_template_path | 49 self._public_template_path = public_template_path |
50 self._private_template_path = private_template_path | 50 self._private_template_path = private_template_path |
51 self._base_path = base_path | 51 self._base_path = base_path |
52 | 52 |
53 def _CreateTemplate(self, template_name, text): | 53 def _CreateTemplate(self, template_name, text): |
54 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) | 54 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) |
55 | 55 |
56 def Create(self, request, data_sources): | 56 def Create(self, request, data_sources): |
57 '''Bind a new TemplateDataSource to a servlet.Request |request| and a | 57 '''Bind a new TemplateDataSource to a servlet.Request |request| and a |
58 dictionary of instantiated DataSources*. Keys in |data_sources| are the | 58 dictionary of instantiated DataSources*. Keys in |data_sources| are the |
59 names that templates will use to access the corresponding DataSource. | 59 names that templates will use to access the corresponding DataSource. |
60 | 60 |
61 * this is temporary until the data source registry transition is complete. | 61 * this is temporary until the data source registry transition is complete. |
62 ''' | 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._cache, | 68 self._cache, |
69 self._permissions_data_source, | 69 self._features_bundle, |
70 self._public_template_path, | 70 self._public_template_path, |
71 self._private_template_path, | 71 self._private_template_path, |
72 self._base_path, | 72 self._base_path, |
73 data_sources) | 73 data_sources) |
74 | 74 |
75 def __init__(self, | 75 def __init__(self, |
76 api_data_source, | 76 api_data_source, |
77 api_list_data_source, | 77 api_list_data_source, |
78 intro_data_source, | 78 intro_data_source, |
79 samples_data_source, | 79 samples_data_source, |
80 cache, | 80 cache, |
81 permissions_data_source, | 81 features_bundle, |
82 public_template_path, | 82 public_template_path, |
83 private_template_path, | 83 private_template_path, |
84 base_path, | 84 base_path, |
85 data_sources): | 85 data_sources): |
86 self._api_list_data_source = api_list_data_source | 86 self._api_list_data_source = api_list_data_source |
87 self._intro_data_source = intro_data_source | 87 self._intro_data_source = intro_data_source |
88 self._samples_data_source = samples_data_source | 88 self._samples_data_source = samples_data_source |
89 self._api_data_source = api_data_source | 89 self._api_data_source = api_data_source |
90 self._cache = cache | 90 self._cache = cache |
91 self._public_template_path = public_template_path | 91 self._public_template_path = public_template_path |
92 self._private_template_path = private_template_path | 92 self._private_template_path = private_template_path |
93 self._permissions_data_source = permissions_data_source | 93 self._features_bundle = features_bundle |
94 self._base_path = base_path | 94 self._base_path = base_path |
95 self._data_sources = data_sources | 95 self._data_sources = data_sources |
96 | 96 |
97 def Render(self, template_name): | 97 def Render(self, template_name): |
98 '''This method will render a template named |template_name|, fetching all | 98 '''This method will render a template named |template_name|, fetching all |
99 the partial templates needed from |self._cache|. Partials are retrieved | 99 the partial templates needed from |self._cache|. Partials are retrieved |
100 from the TemplateDataSource with the |get| method. | 100 from the TemplateDataSource with the |get| method. |
101 ''' | 101 ''' |
102 template = self.GetTemplate(self._public_template_path, template_name) | 102 template = self.GetTemplate(self._public_template_path, template_name) |
103 if template is None: | 103 if template is None: |
104 return None | 104 return None |
105 # TODO error handling | 105 # TODO error handling |
106 render_context = { | 106 render_context = { |
107 'api_list': self._api_list_data_source, | 107 'api_list': self._api_list_data_source, |
108 'apis': self._api_data_source, | 108 'apis': self._api_data_source, |
109 'intros': self._intro_data_source, | 109 'intros': self._intro_data_source, |
110 'partials': self, | 110 'partials': self, |
111 'permissions': self._permissions_data_source, | 111 'permissions': self._features_bundle.get('permission'), |
112 'samples': self._samples_data_source, | 112 'samples': self._samples_data_source, |
113 'apps_samples_url': url_constants.GITHUB_BASE, | 113 'apps_samples_url': url_constants.GITHUB_BASE, |
114 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, | 114 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, |
115 'static': self._base_path + '/static', | 115 'static': self._base_path + '/static', |
116 'true': True, | 116 'true': True, |
117 'false': False | 117 'false': False |
118 } | 118 } |
119 render_context.update(self._data_sources) | 119 render_context.update(self._data_sources) |
120 render_data = template.render(render_context) | 120 render_data = template.render(render_context) |
121 if render_data.errors: | 121 if render_data.errors: |
122 logging.error('Handlebar error(s) rendering %s:\n%s' % | 122 logging.error('Handlebar error(s) rendering %s:\n%s' % |
123 (template_name, ' \n'.join(render_data.errors))) | 123 (template_name, ' \n'.join(render_data.errors))) |
124 return render_data.text | 124 return render_data.text |
125 | 125 |
126 def get(self, key): | 126 def get(self, key): |
127 return self.GetTemplate(self._private_template_path, key) | 127 return self.GetTemplate(self._private_template_path, key) |
128 | 128 |
129 def GetTemplate(self, base_path, template_name): | 129 def GetTemplate(self, base_path, template_name): |
130 try: | 130 try: |
131 return self._cache.GetFromFile( | 131 return self._cache.GetFromFile( |
132 '/'.join((base_path, FormatKey(template_name)))) | 132 '/'.join((base_path, FormatKey(template_name)))) |
133 except FileNotFoundError: | 133 except FileNotFoundError: |
134 return None | 134 return None |
OLD | NEW |