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

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

Issue 10808065: Extensions Docs Server: TDS fix, no local caching (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixes Created 8 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
6
5 from path_utils import FormatKey 7 from path_utils import FormatKey
6 from third_party.handlebar import Handlebar 8 from third_party.handlebar import Handlebar
7 9
8 EXTENSIONS_URL = '/chrome/extensions' 10 EXTENSIONS_URL = '/chrome/extensions'
9 11
10 def _MakeBranchDict(branch): 12 def _MakeBranchDict(branch):
11 return { 13 return {
12 'showWarning': branch != 'stable', 14 'showWarning': branch != 'stable',
13 'branches': [ 15 'branches': [
14 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' }, 16 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' },
15 { 'name': 'Dev', 'path': EXTENSIONS_URL + '/dev' }, 17 { 'name': 'Dev', 'path': EXTENSIONS_URL + '/dev' },
16 { 'name': 'Beta', 'path': EXTENSIONS_URL + '/beta' }, 18 { 'name': 'Beta', 'path': EXTENSIONS_URL + '/beta' },
17 { 'name': 'Trunk', 'path': EXTENSIONS_URL + '/trunk' } 19 { 'name': 'Trunk', 'path': EXTENSIONS_URL + '/trunk' }
18 ], 20 ],
19 'current': branch 21 'current': branch
20 } 22 }
21 23
22 class TemplateDataSource(object): 24 class TemplateDataSource(object):
23 """ Renders Handlebar templates, providing them with the context in which to 25 """Renders Handlebar templates, providing them with the context in which to
24 render. 26 render.
25 27
26 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
27 those it renders. 29 those it renders.
28 30
29 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
30 render templates with request-specific data (such as Accept-Language); use 32 render templates with request-specific data (such as Accept-Language); use
31 a Factory to cheaply construct these. 33 a Factory to cheaply construct these.
32 """ 34 """
33 35
34 class Factory(object): 36 class Factory(object):
35 """ A factory to create lightweight TemplateDataSource instances bound to 37 """A factory to create lightweight TemplateDataSource instances bound to
36 individual Requests. 38 individual Requests.
37 """ 39 """
38 def __init__(self, 40 def __init__(self,
39 branch, 41 branch,
40 api_data_source, 42 api_data_source,
41 intro_data_source, 43 intro_data_source,
42 samples_data_source, 44 samples_data_source,
43 cache_builder, 45 cache_builder,
44 base_paths): 46 public_template_path,
47 private_template_path):
45 self._branch_info = _MakeBranchDict(branch) 48 self._branch_info = _MakeBranchDict(branch)
46 self._static_resources = ((('/' + branch) if branch != 'local' else '') + 49 self._static_resources = ((('/' + branch) if branch != 'local' else '') +
47 '/static') 50 '/static')
48 self._api_data_source = api_data_source 51 self._api_data_source = api_data_source
49 self._intro_data_source = intro_data_source 52 self._intro_data_source = intro_data_source
50 self._samples_data_source = samples_data_source 53 self._samples_data_source = samples_data_source
51 self._cache = cache_builder.build(Handlebar) 54 self._cache = cache_builder.build(Handlebar)
52 self._base_paths = base_paths 55 self._public_template_path = public_template_path
56 self._private_template_path = private_template_path
53 57
54 def Create(self, request): 58 def Create(self, request):
55 """ Returns a new TemplateDataSource bound to |request|. 59 """Returns a new TemplateDataSource bound to |request|.
56 """ 60 """
57 return TemplateDataSource(self._branch_info, 61 return TemplateDataSource(self._branch_info,
58 self._static_resources, 62 self._static_resources,
59 self._api_data_source, 63 self._api_data_source,
60 self._intro_data_source, 64 self._intro_data_source,
61 self._samples_data_source, 65 self._samples_data_source,
62 self._cache, 66 self._cache,
63 self._base_paths, 67 self._public_template_path,
68 self._private_template_path,
64 request) 69 request)
65 70
66 def __init__(self, 71 def __init__(self,
67 branch_info, 72 branch_info,
68 static_resources, 73 static_resources,
69 api_data_source, 74 api_data_source,
70 intro_data_source, 75 intro_data_source,
71 samples_data_source, 76 samples_data_source,
72 cache, 77 cache,
73 base_paths, 78 public_template_path,
79 private_template_path,
74 request): 80 request):
75 self._branch_info = branch_info 81 self._branch_info = branch_info
76 self._static_resources = static_resources 82 self._static_resources = static_resources
77 self._api_data_source = api_data_source 83 self._api_data_source = api_data_source
78 self._intro_data_source = intro_data_source 84 self._intro_data_source = intro_data_source
79 self._samples_data_source = samples_data_source 85 self._samples_data_source = samples_data_source
80 self._cache = cache 86 self._cache = cache
81 self._base_paths = base_paths 87 self._public_template_path = public_template_path
88 self._private_template_path = private_template_path
82 self._request = request 89 self._request = request
83 90
84 def Render(self, template_name): 91 def Render(self, template_name):
85 """This method will render a template named |template_name|, fetching all 92 """This method will render a template named |template_name|, fetching all
86 the partial templates needed from |self._cache|. Partials are retrieved 93 the partial templates needed from |self._cache|. Partials are retrieved
87 from the TemplateDataSource with the |get| method. 94 from the TemplateDataSource with the |get| method.
88 """ 95 """
89 template = self.get(template_name) 96 template = self.GetTemplate(self._public_template_path, template_name)
90 if not template: 97 if not template:
91 return '' 98 return ''
92 # TODO error handling 99 # TODO error handling
93 return template.render({ 100 return template.render({
94 'apis': self._api_data_source, 101 'apis': self._api_data_source,
95 'branchInfo': self._branch_info, 102 'branchInfo': self._branch_info,
96 'intros': self._intro_data_source, 103 'intros': self._intro_data_source,
97 'partials': self, 104 'partials': self,
98 'samples': self._samples_data_source, 105 'samples': self._samples_data_source,
99 'static': self._static_resources 106 'static': self._static_resources
100 }).text 107 }).text
101 108
102 def __getitem__(self, key): 109 def __getitem__(self, key):
103 return self.get(key) 110 return self.get(key)
104 111
105 def get(self, key): 112 def get(self, key):
106 real_path = FormatKey(key) 113 return self.GetTemplate(self._private_template_path, key)
107 for base_path in self._base_paths: 114
108 try: 115 def GetTemplate(self, base_path, template_name):
109 return self._cache.GetFromFile(base_path + '/' + real_path) 116 real_path = FormatKey(template_name)
110 except Exception: 117 try:
111 pass 118 return self._cache.GetFromFile(base_path + '/' + real_path)
112 return None 119 except Exception as e:
120 logging.warn(e)
121 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698