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

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

Issue 15842014: Doc Server Manifest Generation Followup Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Header absolute URL fix Created 7 years, 5 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 6
7 from branch_utility import BranchUtility 7 from branch_utility import BranchUtility
8 from docs_server_utils import FormatKey 8 from docs_server_utils import FormatKey
9 from file_system import FileNotFoundError 9 from file_system import FileNotFoundError
10 from third_party.handlebar import Handlebar 10 from third_party.handlebar import Handlebar
11 import url_constants 11 import url_constants
12 12
13 _EXTENSIONS_URL = '/chrome/extensions' 13 _EXTENSIONS_URL = '/chrome/extensions'
14 14
15 _STRING_CONSTANTS = { 15 _STRING_CONSTANTS = {
16 'app': 'app', 16 'app': 'app',
17 'apps_title': 'Apps', 17 'apps_title': 'Apps',
18 'extension': 'extension', 18 'extension': 'extension',
19 'extensions_title': 'Extensions', 19 'extensions_title': 'Extensions',
20 'events': 'events', 20 'events': 'events',
21 'methods': 'methods', 21 'methods': 'methods',
22 'properties': 'properties', 22 'properties': 'properties',
23 } 23 }
24 24
25 def _MakeChannelDict(channel_name): 25 def _MakeChannelDict(channel_name):
26 channel_dict = { 26 channel_dict = {
27 'channels': [{'name': name} for name in BranchUtility.GetAllChannelNames()], 27 'channels': [{'name': name} for name in BranchUtility.GetAllChannelNames()],
28 'current': channel_name 28 'current': channel_name
29 } 29 }
30
30 for channel in channel_dict['channels']: 31 for channel in channel_dict['channels']:
31 if channel['name'] == channel_name: 32 if channel['name'] == channel_name:
32 channel['isCurrent'] = True 33 channel['isCurrent'] = True
33 return channel_dict 34 return channel_dict
34 35
35 class TemplateDataSource(object): 36 class TemplateDataSource(object):
36 """Renders Handlebar templates, providing them with the context in which to 37 """Renders Handlebar templates, providing them with the context in which to
37 render. 38 render.
38 39
39 Also acts as a data source itself, providing partial Handlebar templates to 40 Also acts as a data source itself, providing partial Handlebar templates to
(...skipping 10 matching lines...) Expand all
50 """ 51 """
51 def __init__(self, 52 def __init__(self,
52 channel_name, 53 channel_name,
53 api_data_source_factory, 54 api_data_source_factory,
54 api_list_data_source_factory, 55 api_list_data_source_factory,
55 intro_data_source_factory, 56 intro_data_source_factory,
56 samples_data_source_factory, 57 samples_data_source_factory,
57 sidenav_data_source_factory, 58 sidenav_data_source_factory,
58 compiled_fs_factory, 59 compiled_fs_factory,
59 ref_resolver_factory, 60 ref_resolver_factory,
61 manifest_data_source,
60 public_template_path, 62 public_template_path,
61 private_template_path, 63 private_template_path,
62 base_path): 64 base_path):
63 self._branch_info = _MakeChannelDict(channel_name) 65 self._branch_info = _MakeChannelDict(channel_name)
64 self._api_data_source_factory = api_data_source_factory 66 self._api_data_source_factory = api_data_source_factory
65 self._api_list_data_source_factory = api_list_data_source_factory 67 self._api_list_data_source_factory = api_list_data_source_factory
66 self._intro_data_source_factory = intro_data_source_factory 68 self._intro_data_source_factory = intro_data_source_factory
67 self._samples_data_source_factory = samples_data_source_factory 69 self._samples_data_source_factory = samples_data_source_factory
68 self._sidenav_data_source_factory = sidenav_data_source_factory 70 self._sidenav_data_source_factory = sidenav_data_source_factory
69 self._cache = compiled_fs_factory.Create(self._CreateTemplate, 71 self._cache = compiled_fs_factory.Create(self._CreateTemplate,
70 TemplateDataSource) 72 TemplateDataSource)
71 self._ref_resolver = ref_resolver_factory.Create() 73 self._ref_resolver = ref_resolver_factory.Create()
72 self._public_template_path = public_template_path 74 self._public_template_path = public_template_path
73 self._private_template_path = private_template_path 75 self._private_template_path = private_template_path
76 self._manifest_data_source = manifest_data_source
74 self._static_resources = '%s/static' % base_path 77 self._static_resources = '%s/static' % base_path
75 78
76 def _CreateTemplate(self, template_name, text): 79 def _CreateTemplate(self, template_name, text):
77 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) 80 return Handlebar(self._ref_resolver.ResolveAllLinks(text))
78 81
79 def Create(self, request, path): 82 def Create(self, request, path):
80 """Returns a new TemplateDataSource bound to |request|. 83 """Returns a new TemplateDataSource bound to |request|.
81 """ 84 """
82 return TemplateDataSource( 85 return TemplateDataSource(
83 self._branch_info, 86 self._branch_info,
84 self._api_data_source_factory.Create(request), 87 self._api_data_source_factory.Create(request),
85 self._api_list_data_source_factory.Create(), 88 self._api_list_data_source_factory.Create(),
86 self._intro_data_source_factory.Create(), 89 self._intro_data_source_factory.Create(),
87 self._samples_data_source_factory.Create(request), 90 self._samples_data_source_factory.Create(request),
88 self._sidenav_data_source_factory.Create(path), 91 self._sidenav_data_source_factory.Create(path),
89 self._cache, 92 self._cache,
93 self._manifest_data_source,
90 self._public_template_path, 94 self._public_template_path,
91 self._private_template_path, 95 self._private_template_path,
92 self._static_resources) 96 self._static_resources)
93 97
94 def __init__(self, 98 def __init__(self,
95 branch_info, 99 branch_info,
96 api_data_source, 100 api_data_source,
97 api_list_data_source, 101 api_list_data_source,
98 intro_data_source, 102 intro_data_source,
99 samples_data_source, 103 samples_data_source,
100 sidenav_data_source, 104 sidenav_data_source,
101 cache, 105 cache,
106 manifest_data_source,
102 public_template_path, 107 public_template_path,
103 private_template_path, 108 private_template_path,
104 static_resources): 109 static_resources):
105 self._branch_info = branch_info 110 self._branch_info = branch_info
106 self._api_list_data_source = api_list_data_source 111 self._api_list_data_source = api_list_data_source
107 self._intro_data_source = intro_data_source 112 self._intro_data_source = intro_data_source
108 self._samples_data_source = samples_data_source 113 self._samples_data_source = samples_data_source
109 self._api_data_source = api_data_source 114 self._api_data_source = api_data_source
110 self._sidenav_data_source = sidenav_data_source 115 self._sidenav_data_source = sidenav_data_source
111 self._cache = cache 116 self._cache = cache
112 self._public_template_path = public_template_path 117 self._public_template_path = public_template_path
113 self._private_template_path = private_template_path 118 self._private_template_path = private_template_path
114 self._static_resources = static_resources 119 self._static_resources = static_resources
120 self._manifest_data_source = manifest_data_source
115 121
116 def Render(self, template_name): 122 def Render(self, template_name):
117 """This method will render a template named |template_name|, fetching all 123 """This method will render a template named |template_name|, fetching all
118 the partial templates needed from |self._cache|. Partials are retrieved 124 the partial templates needed from |self._cache|. Partials are retrieved
119 from the TemplateDataSource with the |get| method. 125 from the TemplateDataSource with the |get| method.
120 """ 126 """
121 template = self.GetTemplate(self._public_template_path, template_name) 127 template = self.GetTemplate(self._public_template_path, template_name)
122 if template is None: 128 if template is None:
123 return None 129 return None
124 # TODO error handling 130 # TODO error handling
125 render_data = template.render({ 131 render_data = template.render({
126 'api_list': self._api_list_data_source, 132 'api_list': self._api_list_data_source,
127 'apis': self._api_data_source, 133 'apis': self._api_data_source,
128 'branchInfo': self._branch_info, 134 'branchInfo': self._branch_info,
129 'intros': self._intro_data_source, 135 'intros': self._intro_data_source,
130 'sidenavs': self._sidenav_data_source, 136 'sidenavs': self._sidenav_data_source,
131 'partials': self, 137 'partials': self,
138 'manifest_source': self._manifest_data_source,
132 'samples': self._samples_data_source, 139 'samples': self._samples_data_source,
133 'static': self._static_resources, 140 'static': self._static_resources,
134 'apps_samples_url': url_constants.GITHUB_BASE, 141 'apps_samples_url': url_constants.GITHUB_BASE,
135 # TODO(kalman): this is wrong, it's always getting from trunk, but meh 142 # TODO(kalman): this is wrong, it's always getting from trunk, but meh
136 # it hardly ever shows up (only in the "cannot fetch samples" message). 143 # it hardly ever shows up (only in the "cannot fetch samples" message).
137 # In fact I don't even know if it can show up anymore due the samples data 144 # In fact I don't even know if it can show up anymore due the samples data
138 # being persisent. In any case, when the channel distinctions are gone 145 # being persisent. In any case, when the channel distinctions are gone
139 # this can go away, so, double meh. 146 # this can go away, so, double meh.
140 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, 147 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES,
141 'strings': _STRING_CONSTANTS, 148 'strings': _STRING_CONSTANTS,
142 'true': True, 149 'true': True,
143 'false': False 150 'false': False
144 }) 151 })
145 if render_data.errors: 152 if render_data.errors:
146 logging.error('Handlebar error(s) rendering %s:\n%s' % 153 logging.error('Handlebar error(s) rendering %s:\n%s' %
147 (template_name, ' \n'.join(render_data.errors))) 154 (template_name, ' \n'.join(render_data.errors)))
148 return render_data.text 155 return render_data.text
149 156
150 def get(self, key): 157 def get(self, key):
151 return self.GetTemplate(self._private_template_path, key) 158 return self.GetTemplate(self._private_template_path, key)
152 159
153 def GetTemplate(self, base_path, template_name): 160 def GetTemplate(self, base_path, template_name):
154 try: 161 try:
155 return self._cache.GetFromFile( 162 return self._cache.GetFromFile(
156 '/'.join((base_path, FormatKey(template_name)))) 163 '/'.join((base_path, FormatKey(template_name))))
157 except FileNotFoundError as e: 164 except FileNotFoundError:
158 return None 165 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698