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

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

Issue 15087006: Docserver: there is only one. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: epic rebase 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 | Annotate | Revision Log
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
8 from docs_server_utils import FormatKey 7 from docs_server_utils import FormatKey
9 from file_system import FileNotFoundError 8 from file_system import FileNotFoundError
10 from third_party.handlebar import Handlebar 9 from third_party.handlebar import Handlebar
11 import url_constants 10 import url_constants
12 11
13 _EXTENSIONS_URL = '/chrome/extensions' 12 _EXTENSIONS_URL = '/chrome/extensions'
14 13
15 _STRING_CONSTANTS = { 14 _STRING_CONSTANTS = {
16 'app': 'app', 15 'app': 'app',
17 'apps_title': 'Apps', 16 'apps_title': 'Apps',
18 'extension': 'extension', 17 'extension': 'extension',
19 'extensions_title': 'Extensions', 18 'extensions_title': 'Extensions',
20 'events': 'events', 19 'events': 'events',
21 'methods': 'methods', 20 'methods': 'methods',
22 'properties': 'properties', 21 'properties': 'properties',
23 } 22 }
24 23
25 def _MakeChannelDict(channel_name):
26 channel_dict = {
27 'channels': [{'name': name} for name in BranchUtility.GetAllChannelNames()],
28 'current': channel_name
29 }
30 for channel in channel_dict['channels']:
31 if channel['name'] == channel_name:
32 channel['isCurrent'] = True
33 return channel_dict
34
35 class TemplateDataSource(object): 24 class TemplateDataSource(object):
36 """Renders Handlebar templates, providing them with the context in which to 25 """Renders Handlebar templates, providing them with the context in which to
37 render. 26 render.
38 27
39 Also acts as a data source itself, providing partial Handlebar templates to 28 Also acts as a data source itself, providing partial Handlebar templates to
40 those it renders. 29 those it renders.
41 30
42 Each instance of TemplateDataSource is bound to a Request so that it can 31 Each instance of TemplateDataSource is bound to a Request so that it can
43 render templates with request-specific data (such as Accept-Language); use 32 render templates with request-specific data (such as Accept-Language); use
44 a Factory to cheaply construct these. 33 a Factory to cheaply construct these.
45 """ 34 """
46 35
47 class Factory(object): 36 class Factory(object):
48 """A factory to create lightweight TemplateDataSource instances bound to 37 """A factory to create lightweight TemplateDataSource instances bound to
49 individual Requests. 38 individual Requests.
50 """ 39 """
51 def __init__(self, 40 def __init__(self,
52 channel_name,
53 api_data_source_factory, 41 api_data_source_factory,
54 api_list_data_source_factory, 42 api_list_data_source_factory,
55 intro_data_source_factory, 43 intro_data_source_factory,
56 samples_data_source_factory, 44 samples_data_source_factory,
57 sidenav_data_source_factory, 45 sidenav_data_source_factory,
58 compiled_fs_factory, 46 compiled_fs_factory,
59 ref_resolver_factory, 47 ref_resolver_factory,
60 public_template_path, 48 public_template_path,
61 private_template_path, 49 private_template_path,
62 base_path): 50 base_path):
63 self._branch_info = _MakeChannelDict(channel_name)
64 self._api_data_source_factory = api_data_source_factory 51 self._api_data_source_factory = api_data_source_factory
65 self._api_list_data_source_factory = api_list_data_source_factory 52 self._api_list_data_source_factory = api_list_data_source_factory
66 self._intro_data_source_factory = intro_data_source_factory 53 self._intro_data_source_factory = intro_data_source_factory
67 self._samples_data_source_factory = samples_data_source_factory 54 self._samples_data_source_factory = samples_data_source_factory
68 self._sidenav_data_source_factory = sidenav_data_source_factory 55 self._sidenav_data_source_factory = sidenav_data_source_factory
69 self._cache = compiled_fs_factory.Create(self._CreateTemplate, 56 self._cache = compiled_fs_factory.Create(self._CreateTemplate,
70 TemplateDataSource) 57 TemplateDataSource)
71 self._ref_resolver = ref_resolver_factory.Create() 58 self._ref_resolver = ref_resolver_factory.Create()
72 self._public_template_path = public_template_path 59 self._public_template_path = public_template_path
73 self._private_template_path = private_template_path 60 self._private_template_path = private_template_path
74 self._static_resources = '%s/static' % base_path 61 self._base_path = base_path
75 62
76 def _CreateTemplate(self, template_name, text): 63 def _CreateTemplate(self, template_name, text):
77 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) 64 return Handlebar(self._ref_resolver.ResolveAllLinks(text))
78 65
79 def Create(self, request, path): 66 def Create(self, request, path):
80 """Returns a new TemplateDataSource bound to |request|. 67 """Returns a new TemplateDataSource bound to |request|.
81 """ 68 """
82 return TemplateDataSource( 69 return TemplateDataSource(
83 self._branch_info,
84 self._api_data_source_factory.Create(request), 70 self._api_data_source_factory.Create(request),
85 self._api_list_data_source_factory.Create(), 71 self._api_list_data_source_factory.Create(),
86 self._intro_data_source_factory.Create(), 72 self._intro_data_source_factory.Create(),
87 self._samples_data_source_factory.Create(request), 73 self._samples_data_source_factory.Create(request),
88 self._sidenav_data_source_factory.Create(path), 74 self._sidenav_data_source_factory.Create(path),
89 self._cache, 75 self._cache,
90 self._public_template_path, 76 self._public_template_path,
91 self._private_template_path, 77 self._private_template_path,
92 self._static_resources) 78 self._base_path)
93 79
94 def __init__(self, 80 def __init__(self,
95 branch_info,
96 api_data_source, 81 api_data_source,
97 api_list_data_source, 82 api_list_data_source,
98 intro_data_source, 83 intro_data_source,
99 samples_data_source, 84 samples_data_source,
100 sidenav_data_source, 85 sidenav_data_source,
101 cache, 86 cache,
102 public_template_path, 87 public_template_path,
103 private_template_path, 88 private_template_path,
104 static_resources): 89 base_path):
105 self._branch_info = branch_info
106 self._api_list_data_source = api_list_data_source 90 self._api_list_data_source = api_list_data_source
107 self._intro_data_source = intro_data_source 91 self._intro_data_source = intro_data_source
108 self._samples_data_source = samples_data_source 92 self._samples_data_source = samples_data_source
109 self._api_data_source = api_data_source 93 self._api_data_source = api_data_source
110 self._sidenav_data_source = sidenav_data_source 94 self._sidenav_data_source = sidenav_data_source
111 self._cache = cache 95 self._cache = cache
112 self._public_template_path = public_template_path 96 self._public_template_path = public_template_path
113 self._private_template_path = private_template_path 97 self._private_template_path = private_template_path
114 self._static_resources = static_resources 98 self._base_path = base_path
115 99
116 def Render(self, template_name): 100 def Render(self, template_name):
117 """This method will render a template named |template_name|, fetching all 101 """This method will render a template named |template_name|, fetching all
118 the partial templates needed from |self._cache|. Partials are retrieved 102 the partial templates needed from |self._cache|. Partials are retrieved
119 from the TemplateDataSource with the |get| method. 103 from the TemplateDataSource with the |get| method.
120 """ 104 """
121 template = self.GetTemplate(self._public_template_path, template_name) 105 template = self.GetTemplate(self._public_template_path, template_name)
122 if template is None: 106 if template is None:
123 return None 107 return None
124 # TODO error handling 108 # TODO error handling
125 render_data = template.render({ 109 render_data = template.render({
126 'api_list': self._api_list_data_source, 110 'api_list': self._api_list_data_source,
127 'apis': self._api_data_source, 111 'apis': self._api_data_source,
128 'branchInfo': self._branch_info,
129 'intros': self._intro_data_source, 112 'intros': self._intro_data_source,
130 'sidenavs': self._sidenav_data_source, 113 'sidenavs': self._sidenav_data_source,
131 'partials': self, 114 'partials': self,
132 'samples': self._samples_data_source, 115 'samples': self._samples_data_source,
133 'static': self._static_resources,
134 'apps_samples_url': url_constants.GITHUB_BASE, 116 'apps_samples_url': url_constants.GITHUB_BASE,
135 # 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).
137 # 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
139 # this can go away, so, double meh.
140 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, 117 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES,
118 'static': self._base_path + '/static',
141 'strings': _STRING_CONSTANTS, 119 'strings': _STRING_CONSTANTS,
142 'true': True, 120 'true': True,
143 'false': False 121 'false': False
144 }) 122 })
145 if render_data.errors: 123 if render_data.errors:
146 logging.error('Handlebar error(s) rendering %s:\n%s' % 124 logging.error('Handlebar error(s) rendering %s:\n%s' %
147 (template_name, ' \n'.join(render_data.errors))) 125 (template_name, ' \n'.join(render_data.errors)))
148 return render_data.text 126 return render_data.text
149 127
150 def get(self, key): 128 def get(self, key):
151 return self.GetTemplate(self._private_template_path, key) 129 return self.GetTemplate(self._private_template_path, key)
152 130
153 def GetTemplate(self, base_path, template_name): 131 def GetTemplate(self, base_path, template_name):
154 try: 132 try:
155 return self._cache.GetFromFile( 133 return self._cache.GetFromFile(
156 '/'.join((base_path, FormatKey(template_name)))) 134 '/'.join((base_path, FormatKey(template_name))))
157 except FileNotFoundError as e: 135 except FileNotFoundError as e:
158 return None 136 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698