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

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

Issue 10689144: Extensions Docs Server: Samples zip files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: better sample page 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 re
5 from path_utils import FormatKey 6 from path_utils import FormatKey
6 from third_party.handlebar import Handlebar 7 from third_party.handlebar import Handlebar
7 8
8 EXTENSIONS_URL = '/chrome/extensions' 9 EXTENSIONS_URL = '/chrome/extensions'
9 10
10 class TemplateDataSource(object): 11 class TemplateDataSource(object):
11 """This class fetches and compiles templates using the fetcher passed in with 12 """This class fetches and compiles templates using the fetcher passed in with
12 |cache_builder|. 13 |cache_builder|.
13 """ 14 """
14 def __init__(self, 15 def __init__(self,
15 branch, 16 branch,
16 api_data_source, 17 api_data_source,
17 intro_data_source, 18 intro_data_source,
18 cache_builder, 19 cache_builder,
20 samples_path,
19 base_paths): 21 base_paths):
20 self._branch_info = self._MakeBranchDict(branch) 22 self._branch_info = self._MakeBranchDict(branch)
21 self._static_resources = ((('/' + branch) if branch != 'local' else '') + 23 self._static_resources = ((('/' + branch) if branch != 'local' else '') +
22 '/static') 24 '/static')
23 self._api_data_source = api_data_source 25 self._api_data_source = api_data_source
24 self._intro_data_source = intro_data_source 26 self._intro_data_source = intro_data_source
25 self._cache = cache_builder.build(self._LoadTemplate) 27 self._cache = cache_builder.build(self._LoadTemplate)
28 self._dir_cache = cache_builder.build(self._MakeSamplesList)
29 self._file_cache = cache_builder.build(lambda x: x)
26 self._base_paths = base_paths 30 self._base_paths = base_paths
31 self._samples_path = samples_path
27 32
28 def _MakeBranchDict(self, branch): 33 def _MakeBranchDict(self, branch):
29 return { 34 return {
30 'showWarning': branch != 'stable', 35 'showWarning': branch != 'stable',
31 'branches': [ 36 'branches': [
32 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' }, 37 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' },
33 { 'name': 'Dev', 'path': EXTENSIONS_URL + '/dev' }, 38 { 'name': 'Dev', 'path': EXTENSIONS_URL + '/dev' },
34 { 'name': 'Beta', 'path': EXTENSIONS_URL + '/beta' }, 39 { 'name': 'Beta', 'path': EXTENSIONS_URL + '/beta' },
35 { 'name': 'Trunk', 'path': EXTENSIONS_URL + '/trunk' } 40 { 'name': 'Trunk', 'path': EXTENSIONS_URL + '/trunk' }
36 ], 41 ],
37 'current': branch 42 'current': branch
38 } 43 }
39 44
45 def _InsertApiFunctions(self, api_functions, js_file):
46 funcs = re.findall('(chrome\.[a-zA-Z0-9\.]+)',
47 self._file_cache.get(js_file))
48 return api_functions + list(set(funcs) - set(api_functions))
49
50 def _MakeSamplesList(self, files):
51 samples_list = []
52 for filename in files:
53 if filename.rsplit('/')[-1] == 'manifest.json':
54 # This is a little hacky, but it makes a sample page.
55 sample_path = filename.rsplit('/', 1)[-2]
56 sample_files = filter(lambda x: x.startswith(sample_path), files)
57 api_functions = []
58 for file_ in sample_files:
59 if file_.endswith('.js'):
60 api_functions = self._InsertApiFunctions(api_functions, file_)
61 samples_list.append({
62 'name': filename.rsplit('/')[-2],
63 'path': sample_path.split('/', 1)[1] + '.zip',
64 'files': [f.replace(sample_path + '/', '') for f in sample_files],
65 'api_functions': api_functions
66 })
67 return samples_list
68
40 def _LoadTemplate(self, template): 69 def _LoadTemplate(self, template):
41 return Handlebar(template) 70 return Handlebar(template)
42 71
43 def Render(self, template_name): 72 def Render(self, template_name):
44 """This method will render a template named |template_name|, fetching all 73 """This method will render a template named |template_name|, fetching all
45 the partial templates needed from |self._cache|. Partials are retrieved 74 the partial templates needed from |self._cache|. Partials are retrieved
46 from the TemplateDataSource with the |get| method. 75 from the TemplateDataSource with the |get| method.
47 """ 76 """
48 template = self.get(template_name) 77 template = self.get(template_name)
49 if not template: 78 if not template:
50 return '' 79 return ''
51 # TODO error handling 80 # TODO error handling
52 return template.render({ 81 return template.render({
53 'apis': self._api_data_source, 82 'apis': self._api_data_source,
54 'branchInfo': self._branch_info, 83 'branchInfo': self._branch_info,
55 'intros': self._intro_data_source, 84 'intros': self._intro_data_source,
56 'partials': self, 85 'partials': self,
86 'samples': self._dir_cache.get(self._samples_path, True),
57 'static': self._static_resources 87 'static': self._static_resources
58 }).text 88 }).text
59 89
60 def __getitem__(self, key): 90 def __getitem__(self, key):
61 return self.get(key) 91 return self.get(key)
62 92
63 def get(self, key): 93 def get(self, key):
64 real_path = FormatKey(key) 94 real_path = FormatKey(key)
65 for base_path in self._base_paths: 95 for base_path in self._base_paths:
66 try: 96 try:
67 return self._cache.get(base_path + '/' + real_path) 97 return self._cache.get(base_path + '/' + real_path)
68 except: 98 except:
69 pass 99 pass
70 return None 100 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698