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

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 and polish 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, 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])
not at google - send to devlin 2013/05/11 18:32:01 Ok I think this is the wrong place for this. siden
jshumway 2013/05/11 22:08:20 I went with the changes to *_sidenav.json and remo
26 return channel_dict 28 return channel_dict
27 29
28 class TemplateDataSource(object): 30 class TemplateDataSource(object):
29 """Renders Handlebar templates, providing them with the context in which to 31 """Renders Handlebar templates, providing them with the context in which to
30 render. 32 render.
31 33
32 Also acts as a data source itself, providing partial Handlebar templates to 34 Also acts as a data source itself, providing partial Handlebar templates to
33 those it renders. 35 those it renders.
34 36
35 Each instance of TemplateDataSource is bound to a Request so that it can 37 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 38 render templates with request-specific data (such as Accept-Language); use
37 a Factory to cheaply construct these. 39 a Factory to cheaply construct these.
38 """ 40 """
39 41
40 class Factory(object): 42 class Factory(object):
41 """A factory to create lightweight TemplateDataSource instances bound to 43 """A factory to create lightweight TemplateDataSource instances bound to
42 individual Requests. 44 individual Requests.
43 """ 45 """
44 def __init__(self, 46 def __init__(self,
45 channel_name, 47 channel_name,
46 api_data_source_factory, 48 api_data_source_factory,
47 api_list_data_source_factory, 49 api_list_data_source_factory,
48 intro_data_source_factory, 50 intro_data_source_factory,
49 samples_data_source_factory, 51 samples_data_source_factory,
50 sidenav_data_source_factory, 52 sidenav_data_source_factory,
51 compiled_fs_factory, 53 compiled_fs_factory,
52 ref_resolver_factory, 54 ref_resolver_factory,
55 manifest_data_source,
53 public_template_path, 56 public_template_path,
54 private_template_path): 57 private_template_path):
55 self._branch_info = _MakeChannelDict(channel_name) 58 self.channel_name = channel_name
56 self._api_data_source_factory = api_data_source_factory 59 self._api_data_source_factory = api_data_source_factory
57 self._api_list_data_source_factory = api_list_data_source_factory 60 self._api_list_data_source_factory = api_list_data_source_factory
58 self._intro_data_source_factory = intro_data_source_factory 61 self._intro_data_source_factory = intro_data_source_factory
59 self._samples_data_source_factory = samples_data_source_factory 62 self._samples_data_source_factory = samples_data_source_factory
60 self._sidenav_data_source_factory = sidenav_data_source_factory 63 self._sidenav_data_source_factory = sidenav_data_source_factory
61 self._cache = compiled_fs_factory.Create(self._CreateTemplate, 64 self._cache = compiled_fs_factory.Create(self._CreateTemplate,
62 TemplateDataSource) 65 TemplateDataSource)
63 self._ref_resolver = ref_resolver_factory.Create() 66 self._ref_resolver = ref_resolver_factory.Create()
64 self._public_template_path = public_template_path 67 self._public_template_path = public_template_path
65 self._private_template_path = private_template_path 68 self._private_template_path = private_template_path
66 self._static_resources = '/%s/static' % channel_name 69 self._static_resources = '/%s/static' % channel_name
70 self._manifest_data_source = manifest_data_source
67 71
68 def _CreateTemplate(self, template_name, text): 72 def _CreateTemplate(self, template_name, text):
69 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) 73 return Handlebar(self._ref_resolver.ResolveAllLinks(text))
70 74
71 def Create(self, request, path): 75 def Create(self, request, path):
72 """Returns a new TemplateDataSource bound to |request|. 76 """Returns a new TemplateDataSource bound to |request|.
73 """ 77 """
74 return TemplateDataSource( 78 return TemplateDataSource(
75 self._branch_info, 79 _MakeChannelDict(self.channel_name, path),
not at google - send to devlin 2013/05/11 18:32:01 I preferred the way it was with storing the _branc
jshumway 2013/05/11 22:08:20 Done.
76 self._api_data_source_factory.Create(request), 80 self._api_data_source_factory.Create(request),
77 self._api_list_data_source_factory.Create(), 81 self._api_list_data_source_factory.Create(),
78 self._intro_data_source_factory.Create(), 82 self._intro_data_source_factory.Create(),
79 self._samples_data_source_factory.Create(request), 83 self._samples_data_source_factory.Create(request),
80 self._sidenav_data_source_factory.Create(path), 84 self._sidenav_data_source_factory.Create(path),
81 self._cache, 85 self._cache,
86 self._manifest_data_source,
82 self._public_template_path, 87 self._public_template_path,
83 self._private_template_path, 88 self._private_template_path,
84 self._static_resources) 89 self._static_resources)
85 90
86 def __init__(self, 91 def __init__(self,
87 branch_info, 92 branch_info,
88 api_data_source, 93 api_data_source,
89 api_list_data_source, 94 api_list_data_source,
90 intro_data_source, 95 intro_data_source,
91 samples_data_source, 96 samples_data_source,
92 sidenav_data_source, 97 sidenav_data_source,
93 cache, 98 cache,
99 manifest_data_source,
94 public_template_path, 100 public_template_path,
95 private_template_path, 101 private_template_path,
96 static_resources): 102 static_resources):
97 self._branch_info = branch_info 103 self._branch_info = branch_info
98 self._api_list_data_source = api_list_data_source 104 self._api_list_data_source = api_list_data_source
99 self._intro_data_source = intro_data_source 105 self._intro_data_source = intro_data_source
100 self._samples_data_source = samples_data_source 106 self._samples_data_source = samples_data_source
101 self._api_data_source = api_data_source 107 self._api_data_source = api_data_source
102 self._sidenav_data_source = sidenav_data_source 108 self._sidenav_data_source = sidenav_data_source
103 self._cache = cache 109 self._cache = cache
104 self._public_template_path = public_template_path 110 self._public_template_path = public_template_path
105 self._private_template_path = private_template_path 111 self._private_template_path = private_template_path
106 self._static_resources = static_resources 112 self._static_resources = static_resources
113 self._manifest_data_source = manifest_data_source
107 114
108 def Render(self, template_name): 115 def Render(self, template_name):
109 """This method will render a template named |template_name|, fetching all 116 """This method will render a template named |template_name|, fetching all
110 the partial templates needed from |self._cache|. Partials are retrieved 117 the partial templates needed from |self._cache|. Partials are retrieved
111 from the TemplateDataSource with the |get| method. 118 from the TemplateDataSource with the |get| method.
112 """ 119 """
113 template = self.GetTemplate(self._public_template_path, template_name) 120 template = self.GetTemplate(self._public_template_path, template_name)
114 if not template: 121 if not template:
115 return None 122 return None
116 # TODO error handling 123 # TODO error handling
117 render_data = template.render({ 124 render_data = template.render({
118 'api_list': self._api_list_data_source, 125 'api_list': self._api_list_data_source,
119 'apis': self._api_data_source, 126 'apis': self._api_data_source,
120 'branchInfo': self._branch_info, 127 'branchInfo': self._branch_info,
121 'intros': self._intro_data_source, 128 'intros': self._intro_data_source,
122 'sidenavs': self._sidenav_data_source, 129 'sidenavs': self._sidenav_data_source,
123 'partials': self, 130 'partials': self,
131 'manifest_source': self._manifest_data_source,
124 'samples': self._samples_data_source, 132 'samples': self._samples_data_source,
125 'static': self._static_resources, 133 'static': self._static_resources,
126 'app': 'app', 134 'app': 'app',
127 'extension': 'extension', 135 'extension': 'extension',
128 'apps_title': 'Apps', 136 'apps_title': 'Apps',
129 'extensions_title': 'Extensions', 137 'extensions_title': 'Extensions',
130 'apps_samples_url': url_constants.GITHUB_BASE, 138 'apps_samples_url': url_constants.GITHUB_BASE,
131 # TODO(kalman): this is wrong, it's always getting from trunk, but meh 139 # 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). 140 # 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 141 # 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 142 # being persisent. In any case, when the channel distinctions are gone
135 # this can go away, so, double meh. 143 # this can go away, so, double meh.
136 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, 144 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES,
137 'true': True, 145 'true': True,
138 'false': False 146 'false': False
139 }) 147 })
140 if render_data.errors: 148 if render_data.errors:
141 logging.error('Handlebar error(s) rendering %s:\n%s' % 149 logging.error('Handlebar error(s) rendering %s:\n%s' %
142 (template_name, ' \n'.join(render_data.errors))) 150 (template_name, ' \n'.join(render_data.errors)))
143 return render_data.text 151 return render_data.text
144 152
145 def get(self, key): 153 def get(self, key):
146 return self.GetTemplate(self._private_template_path, key) 154 return self.GetTemplate(self._private_template_path, key)
147 155
148 def GetTemplate(self, base_path, template_name): 156 def GetTemplate(self, base_path, template_name):
149 try: 157 try:
150 return self._cache.GetFromFile( 158 return self._cache.GetFromFile(
151 '/'.join((base_path, FormatKey(template_name)))) 159 '/'.join((base_path, FormatKey(template_name))))
152 except FileNotFoundError as e: 160 except FileNotFoundError:
153 logging.warning(traceback.format_exc()) 161 logging.warning(traceback.format_exc())
154 return None 162 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698