Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: chrome/common/extensions/docs/server2/template_data_source.py

Issue 14273041: Doc server manifest page generation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Codereview fixes Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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):
19 channel_dict = { 17 channel_dict = {
20 'channels': [{'name': name} for name in BranchUtility.GetAllChannelNames()], 18 'channels': [{'name': name} for name in BranchUtility.GetAllChannelNames()],
21 'current': channel_name 19 'current': channel_name
22 } 20 }
21
23 for channel in channel_dict['channels']: 22 for channel in channel_dict['channels']:
24 if channel['name'] == channel_name: 23 if channel['name'] == channel_name:
25 channel['isCurrent'] = True 24 channel['isCurrent'] = True
26 return channel_dict 25 return channel_dict
27 26
28 class TemplateDataSource(object): 27 class TemplateDataSource(object):
29 """Renders Handlebar templates, providing them with the context in which to 28 """Renders Handlebar templates, providing them with the context in which to
30 render. 29 render.
31 30
32 Also acts as a data source itself, providing partial Handlebar templates to 31 Also acts as a data source itself, providing partial Handlebar templates to
(...skipping 10 matching lines...) Expand all
43 """ 42 """
44 def __init__(self, 43 def __init__(self,
45 channel_name, 44 channel_name,
46 api_data_source_factory, 45 api_data_source_factory,
47 api_list_data_source_factory, 46 api_list_data_source_factory,
48 intro_data_source_factory, 47 intro_data_source_factory,
49 samples_data_source_factory, 48 samples_data_source_factory,
50 sidenav_data_source_factory, 49 sidenav_data_source_factory,
51 compiled_fs_factory, 50 compiled_fs_factory,
52 ref_resolver_factory, 51 ref_resolver_factory,
52 manifest_data_source,
53 public_template_path, 53 public_template_path,
54 private_template_path): 54 private_template_path):
55 self._branch_info = _MakeChannelDict(channel_name) 55 self._branch_info = _MakeChannelDict(channel_name)
56 self._api_data_source_factory = api_data_source_factory 56 self._api_data_source_factory = api_data_source_factory
57 self._api_list_data_source_factory = api_list_data_source_factory 57 self._api_list_data_source_factory = api_list_data_source_factory
58 self._intro_data_source_factory = intro_data_source_factory 58 self._intro_data_source_factory = intro_data_source_factory
59 self._samples_data_source_factory = samples_data_source_factory 59 self._samples_data_source_factory = samples_data_source_factory
60 self._sidenav_data_source_factory = sidenav_data_source_factory 60 self._sidenav_data_source_factory = sidenav_data_source_factory
61 self._cache = compiled_fs_factory.Create(self._CreateTemplate, 61 self._cache = compiled_fs_factory.Create(self._CreateTemplate,
62 TemplateDataSource) 62 TemplateDataSource)
63 self._ref_resolver = ref_resolver_factory.Create() 63 self._ref_resolver = ref_resolver_factory.Create()
64 self._public_template_path = public_template_path 64 self._public_template_path = public_template_path
65 self._private_template_path = private_template_path 65 self._private_template_path = private_template_path
66 self._static_resources = '/%s/static' % channel_name 66 self._static_resources = '/%s/static' % channel_name
67 self._manifest_data_source = manifest_data_source
67 68
68 def _CreateTemplate(self, template_name, text): 69 def _CreateTemplate(self, template_name, text):
69 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) 70 return Handlebar(self._ref_resolver.ResolveAllLinks(text))
70 71
71 def Create(self, request, path): 72 def Create(self, request, path):
72 """Returns a new TemplateDataSource bound to |request|. 73 """Returns a new TemplateDataSource bound to |request|.
73 """ 74 """
74 return TemplateDataSource( 75 return TemplateDataSource(
75 self._branch_info, 76 self._branch_info,
76 self._api_data_source_factory.Create(request), 77 self._api_data_source_factory.Create(request),
77 self._api_list_data_source_factory.Create(), 78 self._api_list_data_source_factory.Create(),
78 self._intro_data_source_factory.Create(), 79 self._intro_data_source_factory.Create(),
79 self._samples_data_source_factory.Create(request), 80 self._samples_data_source_factory.Create(request),
80 self._sidenav_data_source_factory.Create(path), 81 self._sidenav_data_source_factory.Create(path),
81 self._cache, 82 self._cache,
83 self._manifest_data_source,
82 self._public_template_path, 84 self._public_template_path,
83 self._private_template_path, 85 self._private_template_path,
84 self._static_resources) 86 self._static_resources)
85 87
86 def __init__(self, 88 def __init__(self,
87 branch_info, 89 branch_info,
88 api_data_source, 90 api_data_source,
89 api_list_data_source, 91 api_list_data_source,
90 intro_data_source, 92 intro_data_source,
91 samples_data_source, 93 samples_data_source,
92 sidenav_data_source, 94 sidenav_data_source,
93 cache, 95 cache,
96 manifest_data_source,
94 public_template_path, 97 public_template_path,
95 private_template_path, 98 private_template_path,
96 static_resources): 99 static_resources):
97 self._branch_info = branch_info 100 self._branch_info = branch_info
98 self._api_list_data_source = api_list_data_source 101 self._api_list_data_source = api_list_data_source
99 self._intro_data_source = intro_data_source 102 self._intro_data_source = intro_data_source
100 self._samples_data_source = samples_data_source 103 self._samples_data_source = samples_data_source
101 self._api_data_source = api_data_source 104 self._api_data_source = api_data_source
102 self._sidenav_data_source = sidenav_data_source 105 self._sidenav_data_source = sidenav_data_source
103 self._cache = cache 106 self._cache = cache
104 self._public_template_path = public_template_path 107 self._public_template_path = public_template_path
105 self._private_template_path = private_template_path 108 self._private_template_path = private_template_path
106 self._static_resources = static_resources 109 self._static_resources = static_resources
110 self._manifest_data_source = manifest_data_source
107 111
108 def Render(self, template_name): 112 def Render(self, template_name):
109 """This method will render a template named |template_name|, fetching all 113 """This method will render a template named |template_name|, fetching all
110 the partial templates needed from |self._cache|. Partials are retrieved 114 the partial templates needed from |self._cache|. Partials are retrieved
111 from the TemplateDataSource with the |get| method. 115 from the TemplateDataSource with the |get| method.
112 """ 116 """
113 template = self.GetTemplate(self._public_template_path, template_name) 117 template = self.GetTemplate(self._public_template_path, template_name)
114 if not template: 118 if not template:
115 return None 119 return None
116 # TODO error handling 120 # TODO error handling
117 render_data = template.render({ 121 render_data = template.render({
118 'api_list': self._api_list_data_source, 122 'api_list': self._api_list_data_source,
119 'apis': self._api_data_source, 123 'apis': self._api_data_source,
120 'branchInfo': self._branch_info, 124 'branchInfo': self._branch_info,
121 'intros': self._intro_data_source, 125 'intros': self._intro_data_source,
122 'sidenavs': self._sidenav_data_source, 126 'sidenavs': self._sidenav_data_source,
123 'partials': self, 127 'partials': self,
128 'manifest_source': self._manifest_data_source,
124 'samples': self._samples_data_source, 129 'samples': self._samples_data_source,
125 'static': self._static_resources, 130 'static': self._static_resources,
126 'app': 'app', 131 'app': 'app',
127 'extension': 'extension', 132 'extension': 'extension',
128 'apps_title': 'Apps', 133 'apps_title': 'Apps',
129 'extensions_title': 'Extensions', 134 'extensions_title': 'Extensions',
130 'apps_samples_url': url_constants.GITHUB_BASE, 135 'apps_samples_url': url_constants.GITHUB_BASE,
131 # TODO(kalman): this is wrong, it's always getting from trunk, but meh 136 # 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). 137 # 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 138 # 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 139 # being persisent. In any case, when the channel distinctions are gone
135 # this can go away, so, double meh. 140 # this can go away, so, double meh.
136 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, 141 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES,
137 'true': True, 142 'true': True,
138 'false': False 143 'false': False
139 }) 144 })
140 if render_data.errors: 145 if render_data.errors:
141 logging.error('Handlebar error(s) rendering %s:\n%s' % 146 logging.error('Handlebar error(s) rendering %s:\n%s' %
142 (template_name, ' \n'.join(render_data.errors))) 147 (template_name, ' \n'.join(render_data.errors)))
143 return render_data.text 148 return render_data.text
144 149
145 def get(self, key): 150 def get(self, key):
146 return self.GetTemplate(self._private_template_path, key) 151 return self.GetTemplate(self._private_template_path, key)
147 152
148 def GetTemplate(self, base_path, template_name): 153 def GetTemplate(self, base_path, template_name):
149 try: 154 try:
150 return self._cache.GetFromFile( 155 return self._cache.GetFromFile(
151 '/'.join((base_path, FormatKey(template_name)))) 156 '/'.join((base_path, FormatKey(template_name))))
152 except FileNotFoundError as e: 157 except FileNotFoundError:
153 logging.warning(traceback.format_exc()) 158 logging.warning(traceback.format_exc())
154 return None 159 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698