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

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

Issue 10831269: Extensions Docs Server: BranchUtility not fetching branch numbers correctly (fixed) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: better testing and fixes Created 8 years, 4 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 docs_server_utils import FormatKey 7 from docs_server_utils import FormatKey
8 from file_system import FileNotFoundError 8 from file_system import FileNotFoundError
9 from third_party.handlebar import Handlebar 9 from third_party.handlebar import Handlebar
10 10
11 EXTENSIONS_URL = '/chrome/extensions' 11 EXTENSIONS_URL = '/chrome/extensions'
12 12
13 def _MakeBranchDict(branch): 13 def _MakeBranchDict(channel_name):
14 return { 14 return {
15 'showWarning': branch != 'stable', 15 'showWarning': channel_name != 'stable',
16 'branches': [ 16 'branches': [
not at google - send to devlin 2012/08/13 22:36:46 By the same logic, should rename this to _MakeChan
cduvall 2012/08/13 23:17:22 Done.
17 { 'name': 'Stable', 'path': 'stable' }, 17 { 'name': 'Stable', 'path': 'stable' },
18 { 'name': 'Dev', 'path': 'dev' }, 18 { 'name': 'Dev', 'path': 'dev' },
19 { 'name': 'Beta', 'path': 'beta' }, 19 { 'name': 'Beta', 'path': 'beta' },
20 { 'name': 'Trunk', 'path': 'trunk' } 20 { 'name': 'Trunk', 'path': 'trunk' }
21 ], 21 ],
22 'current': branch 22 'current': channel_name
23 } 23 }
24 24
25 class TemplateDataSource(object): 25 class TemplateDataSource(object):
26 """Renders Handlebar templates, providing them with the context in which to 26 """Renders Handlebar templates, providing them with the context in which to
27 render. 27 render.
28 28
29 Also acts as a data source itself, providing partial Handlebar templates to 29 Also acts as a data source itself, providing partial Handlebar templates to
30 those it renders. 30 those it renders.
31 31
32 Each instance of TemplateDataSource is bound to a Request so that it can 32 Each instance of TemplateDataSource is bound to a Request so that it can
33 render templates with request-specific data (such as Accept-Language); use 33 render templates with request-specific data (such as Accept-Language); use
34 a Factory to cheaply construct these. 34 a Factory to cheaply construct these.
35 """ 35 """
36 36
37 class Factory(object): 37 class Factory(object):
38 """A factory to create lightweight TemplateDataSource instances bound to 38 """A factory to create lightweight TemplateDataSource instances bound to
39 individual Requests. 39 individual Requests.
40 """ 40 """
41 def __init__(self, 41 def __init__(self,
42 branch, 42 channel_name,
43 api_data_source_factory, 43 api_data_source_factory,
44 api_list_data_source, 44 api_list_data_source,
45 intro_data_source, 45 intro_data_source,
46 samples_data_source_factory, 46 samples_data_source_factory,
47 cache_builder, 47 cache_builder,
48 public_template_path, 48 public_template_path,
49 private_template_path): 49 private_template_path):
50 self._branch_info = _MakeBranchDict(branch) 50 self._branch_info = _MakeBranchDict(channel_name)
51 self._static_resources = ((('/' + branch) if branch != 'local' else '') +
52 '/static')
53 self._api_data_source_factory = api_data_source_factory 51 self._api_data_source_factory = api_data_source_factory
54 self._api_list_data_source = api_list_data_source 52 self._api_list_data_source = api_list_data_source
55 self._intro_data_source = intro_data_source 53 self._intro_data_source = intro_data_source
56 self._samples_data_source_factory = samples_data_source_factory 54 self._samples_data_source_factory = samples_data_source_factory
57 self._cache = cache_builder.build(Handlebar) 55 self._cache = cache_builder.build(Handlebar)
58 self._public_template_path = public_template_path 56 self._public_template_path = public_template_path
59 self._private_template_path = private_template_path 57 self._private_template_path = private_template_path
58 self._static_resources = (
59 (('/' + channel_name) if channel_name != 'local' else '') + '/static')
60 60
61 def Create(self, request): 61 def Create(self, request):
62 """Returns a new TemplateDataSource bound to |request|. 62 """Returns a new TemplateDataSource bound to |request|.
63 """ 63 """
64 return TemplateDataSource( 64 return TemplateDataSource(
65 self._branch_info, 65 self._branch_info,
66 self._static_resources,
67 self._api_data_source_factory.Create(request), 66 self._api_data_source_factory.Create(request),
68 self._api_list_data_source, 67 self._api_list_data_source,
69 self._intro_data_source, 68 self._intro_data_source,
70 self._samples_data_source_factory.Create(request), 69 self._samples_data_source_factory.Create(request),
71 self._cache, 70 self._cache,
72 self._public_template_path, 71 self._public_template_path,
73 self._private_template_path, 72 self._private_template_path,
73 self._static_resources,
74 request) 74 request)
75 75
76 def __init__(self, 76 def __init__(self,
77 branch_info, 77 branch_info,
78 static_resources,
79 api_data_source, 78 api_data_source,
80 api_list_data_source, 79 api_list_data_source,
81 intro_data_source, 80 intro_data_source,
82 samples_data_source, 81 samples_data_source,
83 cache, 82 cache,
84 public_template_path, 83 public_template_path,
85 private_template_path, 84 private_template_path,
85 static_resources,
86 request): 86 request):
87 self._branch_info = branch_info 87 self._branch_info = branch_info
88 self._static_resources = static_resources
89 self._api_list_data_source = api_list_data_source 88 self._api_list_data_source = api_list_data_source
90 self._intro_data_source = intro_data_source 89 self._intro_data_source = intro_data_source
91 self._samples_data_source = samples_data_source 90 self._samples_data_source = samples_data_source
92 self._api_data_source = api_data_source 91 self._api_data_source = api_data_source
93 self._cache = cache 92 self._cache = cache
94 self._public_template_path = public_template_path 93 self._public_template_path = public_template_path
95 self._private_template_path = private_template_path 94 self._private_template_path = private_template_path
95 self._static_resources = static_resources
96 self._request = request 96 self._request = request
97 97
98 def Render(self, template_name): 98 def Render(self, template_name):
99 """This method will render a template named |template_name|, fetching all 99 """This method will render a template named |template_name|, fetching all
100 the partial templates needed from |self._cache|. Partials are retrieved 100 the partial templates needed from |self._cache|. Partials are retrieved
101 from the TemplateDataSource with the |get| method. 101 from the TemplateDataSource with the |get| method.
102 """ 102 """
103 template = self.GetTemplate(self._public_template_path, template_name) 103 template = self.GetTemplate(self._public_template_path, template_name)
104 if not template: 104 if not template:
105 return '' 105 return ''
(...skipping 18 matching lines...) Expand all
124 def get(self, key): 124 def get(self, key):
125 return self.GetTemplate(self._private_template_path, key) 125 return self.GetTemplate(self._private_template_path, key)
126 126
127 def GetTemplate(self, base_path, template_name): 127 def GetTemplate(self, base_path, template_name):
128 real_path = FormatKey(template_name) 128 real_path = FormatKey(template_name)
129 try: 129 try:
130 return self._cache.GetFromFile(base_path + '/' + real_path) 130 return self._cache.GetFromFile(base_path + '/' + real_path)
131 except FileNotFoundError as e: 131 except FileNotFoundError as e:
132 logging.error(e) 132 logging.error(e)
133 return None 133 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698