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

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: Samples page with full links and descriptions 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 json
6 import re
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 class TemplateDataSource(object): 12 class TemplateDataSource(object):
11 """This class fetches and compiles templates using the fetcher passed in with 13 """This class fetches and compiles templates using the fetcher passed in with
12 |cache_builder|. 14 |cache_builder|.
13 """ 15 """
14 def __init__(self, 16 def __init__(self,
15 branch, 17 branch,
16 api_data_source, 18 api_data_source,
17 intro_data_source, 19 intro_data_source,
18 cache_builder, 20 cache_builder,
21 samples_path,
19 base_paths): 22 base_paths):
20 self._branch_info = self._MakeBranchDict(branch) 23 self._branch_info = self._MakeBranchDict(branch)
21 self._static_resources = ((('/' + branch) if branch != 'local' else '') + 24 self._static_resources = ((('/' + branch) if branch != 'local' else '') +
22 '/static') 25 '/static')
23 self._api_data_source = api_data_source 26 self._api_data_source = api_data_source
24 self._intro_data_source = intro_data_source 27 self._intro_data_source = intro_data_source
25 self._cache = cache_builder.build(self._LoadTemplate) 28 self._cache = cache_builder.build(self._LoadTemplate)
29 self._dir_cache = cache_builder.build(self._MakeSamplesList)
30 self._file_cache = cache_builder.build(lambda x: x)
not at google - send to devlin 2012/07/12 00:22:48 Same comment as earlier. file_cache not necessary
cduvall 2012/07/12 01:51:23 Done.
26 self._base_paths = base_paths 31 self._base_paths = base_paths
32 self._samples_path = samples_path
27 33
28 def _MakeBranchDict(self, branch): 34 def _MakeBranchDict(self, branch):
29 return { 35 return {
30 'showWarning': branch != 'stable', 36 'showWarning': branch != 'stable',
31 'branches': [ 37 'branches': [
32 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' }, 38 { 'name': 'Stable', 'path': EXTENSIONS_URL + '/stable' },
33 { 'name': 'Dev', 'path': EXTENSIONS_URL + '/dev' }, 39 { 'name': 'Dev', 'path': EXTENSIONS_URL + '/dev' },
34 { 'name': 'Beta', 'path': EXTENSIONS_URL + '/beta' }, 40 { 'name': 'Beta', 'path': EXTENSIONS_URL + '/beta' },
35 { 'name': 'Trunk', 'path': EXTENSIONS_URL + '/trunk' } 41 { 'name': 'Trunk', 'path': EXTENSIONS_URL + '/trunk' }
36 ], 42 ],
37 'current': branch 43 'current': branch
38 } 44 }
39 45
46 def _InsertApiItems(self, api_items, js_file):
47 funcs = re.findall('(chrome\.[a-zA-Z0-9\.]+)',
48 self._file_cache.get(js_file))
49 return api_items + list(set(funcs) - set(api_items))
not at google - send to devlin 2012/07/12 00:22:48 This is pretty indirect, just pass through set and
cduvall 2012/07/12 01:51:23 Done.
50
51 def _MakeApiLink(self, prefix, item):
52 api, name = item.replace('chrome.', '').rsplit('.', 1)
53 return api + '.html#' + prefix + '-' + name
54
55 def _GetDescriptionFromManifest(self, path):
56 manifest = self._file_cache.get(path + '/manifest.json')
57 return json.loads(manifest).get('description')
58
59 def _MakeSamplesList(self, files):
60 samples_list = []
61 for filename in files:
62 if filename.rsplit('/')[-1] == 'manifest.json':
not at google - send to devlin 2012/07/12 00:22:48 if filename.rsplit('/')[-1] != 'manifest.json':
cduvall 2012/07/12 01:51:23 Done.
63 # This is a little hacky, but it makes a sample page.
64 sample_path = filename.rsplit('/', 1)[-2]
65 sample_files = filter(lambda x: x.startswith(sample_path), files)
66 api_items = []
67 for file_ in sample_files:
68 if file_.endswith('.js'):
69 api_items = self._InsertApiItems(api_items, file_)
70
71 api_calls = []
72 for item in api_items:
73 if len(item.split('.')) < 3:
74 continue
75 if item.endswith('.addListener'):
76 item = item.replace('.addListener', '')
77 api_calls.append({
78 'name': item.rsplit('.')[-1],
79 'link': self._MakeApiLink('event', item)
80 })
81 else:
82 api_calls.append({
83 'name': item.rsplit('.')[-1],
84 'link': self._MakeApiLink('method', item)
85 })
86
87 samples_list.append({
88 'name': filename.rsplit('/')[-2],
89 'description': self._GetDescriptionFromManifest(sample_path),
90 'path': sample_path.split('/', 1)[1],
91 'files': [f.replace(sample_path + '/', '') for f in sample_files],
92 'api_calls': api_calls
93 })
94 return samples_list
not at google - send to devlin 2012/07/12 00:22:48 wow badass. Definitely pull this stuff out of her
95
40 def _LoadTemplate(self, template): 96 def _LoadTemplate(self, template):
41 return Handlebar(template) 97 return Handlebar(template)
42 98
43 def Render(self, template_name): 99 def Render(self, template_name):
44 """This method will render a template named |template_name|, fetching all 100 """This method will render a template named |template_name|, fetching all
45 the partial templates needed from |self._cache|. Partials are retrieved 101 the partial templates needed from |self._cache|. Partials are retrieved
46 from the TemplateDataSource with the |get| method. 102 from the TemplateDataSource with the |get| method.
47 """ 103 """
48 template = self.get(template_name) 104 template = self.get(template_name)
49 if not template: 105 if not template:
50 return '' 106 return ''
51 # TODO error handling 107 # TODO error handling
52 return template.render({ 108 return template.render({
53 'apis': self._api_data_source, 109 'apis': self._api_data_source,
54 'branchInfo': self._branch_info, 110 'branchInfo': self._branch_info,
55 'intros': self._intro_data_source, 111 'intros': self._intro_data_source,
56 'partials': self, 112 'partials': self,
113 'samples': self._dir_cache.get(self._samples_path, True),
not at google - send to devlin 2012/07/12 00:22:48 Can we make this lazily populate somehow? Most tem
cduvall 2012/07/12 01:51:23 Done.
57 'static': self._static_resources 114 'static': self._static_resources
58 }).text 115 }).text
59 116
60 def __getitem__(self, key): 117 def __getitem__(self, key):
61 return self.get(key) 118 return self.get(key)
62 119
63 def get(self, key): 120 def get(self, key):
64 real_path = FormatKey(key) 121 real_path = FormatKey(key)
65 for base_path in self._base_paths: 122 for base_path in self._base_paths:
66 try: 123 try:
67 return self._cache.get(base_path + '/' + real_path) 124 return self._cache.get(base_path + '/' + real_path)
68 except: 125 except:
69 pass 126 pass
70 return None 127 return None
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698