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

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

Issue 13470005: Refactor the devserver to make it easier to control caching (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cduvall, rebase Created 7 years, 8 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 7 from branch_utility import BranchUtility
8 from docs_server_utils import FormatKey 8 from docs_server_utils import FormatKey
9 import compiled_file_system as compiled_fs 9 import compiled_file_system as compiled_fs
10 from file_system import FileNotFoundError 10 from file_system import FileNotFoundError
11 from third_party.handlebar import Handlebar 11 from third_party.handlebar import Handlebar
12 import url_constants 12 import url_constants
13 13
14 # Increment this if there are changes to the data stored about templates. 14 # Increment this if the data model changes for TemplateDataSource.
15 _VERSION = 2 15 _VERSION = 3
16 16
17 EXTENSIONS_URL = '/chrome/extensions' 17 EXTENSIONS_URL = '/chrome/extensions'
18 18
19 def _MakeChannelDict(channel_name): 19 def _MakeChannelDict(channel_name):
20 channel_dict = { 20 channel_dict = {
21 'channels': [{'name': name} for name in BranchUtility.GetAllBranchNames()], 21 'channels': [{'name': name} for name in BranchUtility.GetAllBranchNames()],
22 'current': channel_name 22 'current': channel_name
23 } 23 }
24 for channel in channel_dict['channels']: 24 for channel in channel_dict['channels']:
25 if channel['name'] == channel_name: 25 if channel['name'] == channel_name:
(...skipping 15 matching lines...) Expand all
41 class Factory(object): 41 class Factory(object):
42 """A factory to create lightweight TemplateDataSource instances bound to 42 """A factory to create lightweight TemplateDataSource instances bound to
43 individual Requests. 43 individual Requests.
44 """ 44 """
45 def __init__(self, 45 def __init__(self,
46 channel_name, 46 channel_name,
47 api_data_source_factory, 47 api_data_source_factory,
48 api_list_data_source_factory, 48 api_list_data_source_factory,
49 intro_data_source_factory, 49 intro_data_source_factory,
50 samples_data_source_factory, 50 samples_data_source_factory,
51 known_issues_data_source,
52 sidenav_data_source_factory, 51 sidenav_data_source_factory,
53 cache_factory, 52 compiled_fs_factory,
54 ref_resolver_factory, 53 ref_resolver_factory,
55 public_template_path, 54 public_template_path,
56 private_template_path): 55 private_template_path):
57 self._branch_info = _MakeChannelDict(channel_name) 56 self._branch_info = _MakeChannelDict(channel_name)
58 self._api_data_source_factory = api_data_source_factory 57 self._api_data_source_factory = api_data_source_factory
59 self._api_list_data_source_factory = api_list_data_source_factory 58 self._api_list_data_source_factory = api_list_data_source_factory
60 self._intro_data_source_factory = intro_data_source_factory 59 self._intro_data_source_factory = intro_data_source_factory
61 self._samples_data_source_factory = samples_data_source_factory 60 self._samples_data_source_factory = samples_data_source_factory
62 self._known_issues_data_source = known_issues_data_source
63 self._sidenav_data_source_factory = sidenav_data_source_factory 61 self._sidenav_data_source_factory = sidenav_data_source_factory
64 self._cache = cache_factory.Create(self._CreateTemplate, 62 self._cache = compiled_fs_factory.Create(self._CreateTemplate,
65 compiled_fs.HANDLEBAR, 63 TemplateDataSource,
66 version=_VERSION) 64 version=_VERSION)
67 self._ref_resolver = ref_resolver_factory.Create() 65 self._ref_resolver = ref_resolver_factory.Create()
68 self._public_template_path = public_template_path 66 self._public_template_path = public_template_path
69 self._private_template_path = private_template_path 67 self._private_template_path = private_template_path
70 self._static_resources = '/%s/static' % channel_name 68 self._static_resources = '/%s/static' % channel_name
71 69
72 def _CreateTemplate(self, template_name, text): 70 def _CreateTemplate(self, template_name, text):
73 return Handlebar(self._ref_resolver.ResolveAllLinks(text)) 71 return Handlebar(self._ref_resolver.ResolveAllLinks(text))
74 72
75 def Create(self, request, path): 73 def Create(self, request, path):
76 """Returns a new TemplateDataSource bound to |request|. 74 """Returns a new TemplateDataSource bound to |request|.
77 """ 75 """
78 return TemplateDataSource( 76 return TemplateDataSource(
79 self._branch_info, 77 self._branch_info,
80 self._api_data_source_factory.Create(request), 78 self._api_data_source_factory.Create(request),
81 self._api_list_data_source_factory.Create(), 79 self._api_list_data_source_factory.Create(),
82 self._intro_data_source_factory.Create(), 80 self._intro_data_source_factory.Create(),
83 self._samples_data_source_factory.Create(request), 81 self._samples_data_source_factory.Create(request),
84 self._known_issues_data_source,
85 self._sidenav_data_source_factory.Create(path), 82 self._sidenav_data_source_factory.Create(path),
86 self._cache, 83 self._cache,
87 self._public_template_path, 84 self._public_template_path,
88 self._private_template_path, 85 self._private_template_path,
89 self._static_resources, 86 self._static_resources)
90 request)
91 87
92 def __init__(self, 88 def __init__(self,
93 branch_info, 89 branch_info,
94 api_data_source, 90 api_data_source,
95 api_list_data_source, 91 api_list_data_source,
96 intro_data_source, 92 intro_data_source,
97 samples_data_source, 93 samples_data_source,
98 known_issues_data_source,
99 sidenav_data_source, 94 sidenav_data_source,
100 cache, 95 cache,
101 public_template_path, 96 public_template_path,
102 private_template_path, 97 private_template_path,
103 static_resources, 98 static_resources):
104 request):
105 self._branch_info = branch_info 99 self._branch_info = branch_info
106 self._api_list_data_source = api_list_data_source 100 self._api_list_data_source = api_list_data_source
107 self._intro_data_source = intro_data_source 101 self._intro_data_source = intro_data_source
108 self._samples_data_source = samples_data_source 102 self._samples_data_source = samples_data_source
109 self._api_data_source = api_data_source 103 self._api_data_source = api_data_source
110 self._known_issues_data_source = known_issues_data_source
111 self._sidenav_data_source = sidenav_data_source 104 self._sidenav_data_source = sidenav_data_source
112 self._cache = cache 105 self._cache = cache
113 self._public_template_path = public_template_path 106 self._public_template_path = public_template_path
114 self._private_template_path = private_template_path 107 self._private_template_path = private_template_path
115 self._static_resources = static_resources 108 self._static_resources = static_resources
116 self._request = request
117 109
118 def Render(self, template_name): 110 def Render(self, template_name):
119 """This method will render a template named |template_name|, fetching all 111 """This method will render a template named |template_name|, fetching all
120 the partial templates needed from |self._cache|. Partials are retrieved 112 the partial templates needed from |self._cache|. Partials are retrieved
121 from the TemplateDataSource with the |get| method. 113 from the TemplateDataSource with the |get| method.
122 """ 114 """
123 template = self.GetTemplate(self._public_template_path, template_name) 115 template = self.GetTemplate(self._public_template_path, template_name)
124 if not template: 116 if not template:
125 return '' 117 return ''
126 # TODO error handling 118 # TODO error handling
127 render_data = template.render({ 119 render_data = template.render({
128 'api_list': self._api_list_data_source, 120 'api_list': self._api_list_data_source,
129 'apis': self._api_data_source, 121 'apis': self._api_data_source,
130 'branchInfo': self._branch_info, 122 'branchInfo': self._branch_info,
131 'intros': self._intro_data_source, 123 'intros': self._intro_data_source,
132 'known_issues': self._known_issues_data_source,
133 'sidenavs': self._sidenav_data_source, 124 'sidenavs': self._sidenav_data_source,
134 'partials': self, 125 'partials': self,
135 'samples': self._samples_data_source, 126 'samples': self._samples_data_source,
136 'static': self._static_resources, 127 'static': self._static_resources,
137 'app': 'app', 128 'app': 'app',
138 'extension': 'extension', 129 'extension': 'extension',
139 'apps_title': 'Apps', 130 'apps_title': 'Apps',
140 'extensions_title': 'Extensions', 131 'extensions_title': 'Extensions',
141 'apps_samples_url': url_constants.GITHUB_BASE, 132 'apps_samples_url': url_constants.GITHUB_BASE,
142 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES, 133 'extensions_samples_url': url_constants.EXTENSIONS_SAMPLES,
143 'true': True, 134 'true': True,
144 'false': False 135 'false': False
145 }) 136 })
146 if render_data.errors: 137 if render_data.errors:
147 logging.error('Handlebar error(s) rendering %s:\n%s' % 138 logging.error('Handlebar error(s) rendering %s:\n%s' %
148 (template_name, ' \n'.join(render_data.errors))) 139 (template_name, ' \n'.join(render_data.errors)))
149 return render_data.text 140 return render_data.text
150 141
151 def get(self, key): 142 def get(self, key):
152 return self.GetTemplate(self._private_template_path, key) 143 return self.GetTemplate(self._private_template_path, key)
153 144
154 def GetTemplate(self, base_path, template_name): 145 def GetTemplate(self, base_path, template_name):
155 real_path = FormatKey(template_name) 146 real_path = FormatKey(template_name)
156 try: 147 try:
157 return self._cache.GetFromFile(base_path + '/' + real_path) 148 return self._cache.GetFromFile(base_path + '/' + real_path)
158 except FileNotFoundError as e: 149 except FileNotFoundError as e:
159 logging.info(e) 150 logging.info(e)
160 return None 151 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698