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

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

Issue 437323003: Docserver: Factor SamplesModel out of SamplesDataSource (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 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
OLDNEW
(Empty)
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
3 # found in the LICENSE file.
4
5 import json
6 import logging
7 import posixpath
8 import re
9
10 from extensions_paths import EXAMPLES
11 from samples_data_source import SamplesDataSource
12 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater
13 import url_constants
14
15
16 _DEFAULT_ICON_PATH = 'images/sample-default-icon.png'
17
18
19 def _GetAPIItems(js_file):
20 chrome_pattern = r'chrome[\w.]+'
21 # Add API calls that appear normally, like "chrome.runtime.connect".
22 calls = set(re.findall(chrome_pattern, js_file))
23 # Add API calls that have been assigned into variables, like
24 # "var storageArea = chrome.storage.sync; storageArea.get", which should
25 # be expanded like "chrome.storage.sync.get".
26 for match in re.finditer(r'var\s+(\w+)\s*=\s*(%s);' % chrome_pattern,
27 js_file):
28 var_name, api_prefix = match.groups()
29 for var_match in re.finditer(r'\b%s\.([\w.]+)\b' % re.escape(var_name),
30 js_file):
31 api_suffix, = var_match.groups()
32 calls.add('%s.%s' % (api_prefix, api_suffix))
33 return calls
34
35
36 class SamplesModel(object):
37 def __init__(self,
38 extension_samples_file_system,
39 app_samples_file_system,
40 compiled_fs_factory,
41 reference_resolver,
42 base_path,
43 platform):
44 self._samples_fs = (extension_samples_file_system if
45 platform == 'extensions' else app_samples_file_system)
46 self._samples_cache = compiled_fs_factory.Create(
47 self._samples_fs,
48 self._MakeSamplesList,
49 SamplesDataSource,
50 category=platform)
51 self._text_cache = compiled_fs_factory.ForUnicode(self._samples_fs)
52 self._reference_resolver = reference_resolver
53 self._base_path = base_path
54 self._platform = platform
55
56 def GetCache(self):
57 return self._samples_cache
58
59 def FilterSamples(self, api_name):
60 '''Fetches and filters the list of samples for this platform, returning
61 only the samples that use the API |api_name|.
62 '''
63 samples_list = self._samples_cache.GetFromFileListing(
64 '' if self._platform == 'apps' else EXAMPLES).Get()
65 return [sample for sample in samples_list if any(
66 call['name'].startswith(api_name + '.')
67 for call in sample['api_calls'])]
68
69 def _GetDataFromManifest(self, path, file_system):
70 manifest = self._text_cache.GetFromFile(path + '/manifest.json').Get()
71 try:
72 manifest_json = json.loads(json_comment_eater.Nom(manifest))
73 except ValueError as e:
74 logging.error('Error parsing manifest.json for %s: %s' % (path, e))
75 return None
76 l10n_data = {
77 'name': manifest_json.get('name', ''),
78 'description': manifest_json.get('description', None),
79 'icon': manifest_json.get('icons', {}).get('128', None),
80 'default_locale': manifest_json.get('default_locale', None),
81 'locales': {}
82 }
83 if not l10n_data['default_locale']:
84 return l10n_data
85 locales_path = path + '/_locales/'
86 locales_dir = file_system.ReadSingle(locales_path).Get()
87 if locales_dir:
88 def load_locale_json(path):
89 return (path, json.loads(self._text_cache.GetFromFile(path).Get()))
90
91 try:
92 locales_json = [load_locale_json(locales_path + f + 'messages.json')
93 for f in locales_dir]
94 except ValueError as e:
95 logging.error('Error parsing locales files for %s: %s' % (path, e))
96 else:
97 for path, json_ in locales_json:
98 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_
99 return l10n_data
100
101 def _MakeSamplesList(self, base_path, files):
102 samples_list = []
103 for filename in sorted(files):
104 if filename.rsplit('/')[-1] != 'manifest.json':
105 continue
106
107 # This is a little hacky, but it makes a sample page.
108 sample_path = filename.rsplit('/', 1)[-2]
109 sample_files = [path for path in files
110 if path.startswith(sample_path + '/')]
111 js_files = [path for path in sample_files if path.endswith('.js')]
112 js_contents = [self._text_cache.GetFromFile(
113 posixpath.join(base_path, js_file)).Get()
114 for js_file in js_files]
115 api_items = set()
116 for js in js_contents:
117 api_items.update(_GetAPIItems(js))
118
119 api_calls = []
120 for item in sorted(api_items):
121 if len(item.split('.')) < 3:
122 continue
123 if item.endswith('.removeListener') or item.endswith('.hasListener'):
124 continue
125 if item.endswith('.addListener'):
126 item = item[:-len('.addListener')]
127 if item.startswith('chrome.'):
128 item = item[len('chrome.'):]
129 ref_data = self._reference_resolver.GetLink(item)
130 # TODO(kalman): What about references like chrome.storage.sync.get?
131 # That should link to either chrome.storage.sync or
132 # chrome.storage.StorageArea.get (or probably both).
133 # TODO(kalman): Filter out API-only references? This can happen when
134 # the API namespace is assigned to a variable, but it's very hard to
135 # to disambiguate.
136 if ref_data is None:
137 continue
138 api_calls.append({
139 'name': ref_data['text'],
140 'link': ref_data['href']
141 })
142
143 if self._platform == 'apps':
144 url = url_constants.GITHUB_BASE + '/' + sample_path
145 icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_path
146 download_url = url
147 else:
148 extension_sample_path = posixpath.join('examples', sample_path)
149 url = extension_sample_path
150 icon_base = extension_sample_path
151 download_url = extension_sample_path + '.zip'
152
153 manifest_data = self._GetDataFromManifest(
154 posixpath.join(base_path, sample_path),
155 self._samples_fs)
156 if manifest_data['icon'] is None:
157 icon_path = posixpath.join(
158 self._base_path, 'static', _DEFAULT_ICON_PATH)
159 else:
160 icon_path = '%s/%s' % (icon_base, manifest_data['icon'])
161 manifest_data.update({
162 'icon': icon_path,
163 'download_url': download_url,
164 'url': url,
165 'files': [f.replace(sample_path + '/', '') for f in sample_files],
166 'api_calls': api_calls
167 })
168 samples_list.append(manifest_data)
169
170 return samples_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698