| OLD | NEW |
| 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 hashlib | 5 import hashlib |
| 6 import json | 6 import json |
| 7 import logging | 7 import logging |
| 8 import posixpath | 8 import posixpath |
| 9 import re | 9 import re |
| 10 import traceback | 10 import traceback |
| 11 | 11 |
| 12 from compiled_file_system import CompiledFileSystem | 12 from compiled_file_system import CompiledFileSystem |
| 13 from extensions_paths import EXAMPLES |
| 13 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater | 14 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
| 14 import third_party.json_schema_compiler.model as model | 15 import third_party.json_schema_compiler.model as model |
| 15 import url_constants | 16 import url_constants |
| 16 | 17 |
| 17 DEFAULT_ICON_PATH = 'images/sample-default-icon.png' | 18 |
| 19 _DEFAULT_ICON_PATH = 'images/sample-default-icon.png' |
| 20 |
| 18 | 21 |
| 19 class SamplesDataSource(object): | 22 class SamplesDataSource(object): |
| 20 '''Constructs a list of samples and their respective files and api calls. | 23 '''Constructs a list of samples and their respective files and api calls. |
| 21 ''' | 24 ''' |
| 22 class Factory(object): | 25 class Factory(object): |
| 23 '''A factory to create SamplesDataSource instances bound to individual | 26 '''A factory to create SamplesDataSource instances bound to individual |
| 24 Requests. | 27 Requests. |
| 25 ''' | 28 ''' |
| 26 def __init__(self, | 29 def __init__(self, |
| 27 host_file_system, | 30 host_file_system, |
| 28 app_samples_file_system, | 31 app_samples_file_system, |
| 29 compiled_fs_factory, | 32 compiled_fs_factory, |
| 30 ref_resolver_factory, | 33 ref_resolver_factory, |
| 31 extension_samples_path, | |
| 32 base_path): | 34 base_path): |
| 33 self._host_file_system = host_file_system | 35 self._host_file_system = host_file_system |
| 34 self._app_samples_file_system = app_samples_file_system | 36 self._app_samples_file_system = app_samples_file_system |
| 35 self._ref_resolver = ref_resolver_factory.Create() | 37 self._ref_resolver = ref_resolver_factory.Create() |
| 36 self._extension_samples_path = extension_samples_path | |
| 37 self._base_path = base_path | 38 self._base_path = base_path |
| 38 self._extensions_cache = compiled_fs_factory.Create( | 39 self._extensions_cache = compiled_fs_factory.Create( |
| 39 host_file_system, | 40 host_file_system, |
| 40 self._MakeSamplesList, | 41 self._MakeSamplesList, |
| 41 SamplesDataSource, | 42 SamplesDataSource, |
| 42 category='extensions') | 43 category='extensions') |
| 43 self._apps_cache = compiled_fs_factory.Create( | 44 self._apps_cache = compiled_fs_factory.Create( |
| 44 app_samples_file_system, | 45 app_samples_file_system, |
| 45 lambda *args: self._MakeSamplesList(*args, is_apps=True), | 46 lambda *args: self._MakeSamplesList(*args, is_apps=True), |
| 46 SamplesDataSource, | 47 SamplesDataSource, |
| 47 category='apps') | 48 category='apps') |
| 48 | 49 |
| 49 def Create(self, request): | 50 def Create(self, request): |
| 50 '''Returns a new SamplesDataSource bound to |request|. | 51 '''Returns a new SamplesDataSource bound to |request|. |
| 51 ''' | 52 ''' |
| 52 return SamplesDataSource(self._extensions_cache, | 53 return SamplesDataSource(self._extensions_cache, |
| 53 self._apps_cache, | 54 self._apps_cache, |
| 54 self._extension_samples_path, | |
| 55 self._base_path, | 55 self._base_path, |
| 56 request) | 56 request) |
| 57 | 57 |
| 58 def _GetAPIItems(self, js_file): | 58 def _GetAPIItems(self, js_file): |
| 59 chrome_pattern = r'chrome[\w.]+' | 59 chrome_pattern = r'chrome[\w.]+' |
| 60 # Add API calls that appear normally, like "chrome.runtime.connect". | 60 # Add API calls that appear normally, like "chrome.runtime.connect". |
| 61 calls = set(re.findall(chrome_pattern, js_file)) | 61 calls = set(re.findall(chrome_pattern, js_file)) |
| 62 # Add API calls that have been assigned into variables, like | 62 # Add API calls that have been assigned into variables, like |
| 63 # "var storageArea = chrome.storage.sync; storageArea.get", which should | 63 # "var storageArea = chrome.storage.sync; storageArea.get", which should |
| 64 # be expanded like "chrome.storage.sync.get". | 64 # be expanded like "chrome.storage.sync.get". |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_base_path | 154 icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_base_path |
| 155 download_url = url | 155 download_url = url |
| 156 else: | 156 else: |
| 157 url = sample_base_path | 157 url = sample_base_path |
| 158 icon_base = sample_base_path | 158 icon_base = sample_base_path |
| 159 download_url = sample_base_path + '.zip' | 159 download_url = sample_base_path + '.zip' |
| 160 | 160 |
| 161 manifest_data = self._GetDataFromManifest(sample_path, file_system) | 161 manifest_data = self._GetDataFromManifest(sample_path, file_system) |
| 162 if manifest_data['icon'] is None: | 162 if manifest_data['icon'] is None: |
| 163 icon_path = posixpath.join( | 163 icon_path = posixpath.join( |
| 164 self._base_path, 'static', DEFAULT_ICON_PATH) | 164 self._base_path, 'static', _DEFAULT_ICON_PATH) |
| 165 else: | 165 else: |
| 166 icon_path = '%s/%s' % (icon_base, manifest_data['icon']) | 166 icon_path = '%s/%s' % (icon_base, manifest_data['icon']) |
| 167 manifest_data.update({ | 167 manifest_data.update({ |
| 168 'icon': icon_path, | 168 'icon': icon_path, |
| 169 'download_url': download_url, | 169 'download_url': download_url, |
| 170 'url': url, | 170 'url': url, |
| 171 'files': [f.replace(sample_path + '/', '') for f in sample_files], | 171 'files': [f.replace(sample_path + '/', '') for f in sample_files], |
| 172 'api_calls': api_calls | 172 'api_calls': api_calls |
| 173 }) | 173 }) |
| 174 samples_list.append(manifest_data) | 174 samples_list.append(manifest_data) |
| 175 | 175 |
| 176 return samples_list | 176 return samples_list |
| 177 | 177 |
| 178 def __init__(self, | 178 def __init__(self, |
| 179 extensions_cache, | 179 extensions_cache, |
| 180 apps_cache, | 180 apps_cache, |
| 181 extension_samples_path, | |
| 182 base_path, | 181 base_path, |
| 183 request): | 182 request): |
| 184 self._extensions_cache = extensions_cache | 183 self._extensions_cache = extensions_cache |
| 185 self._apps_cache = apps_cache | 184 self._apps_cache = apps_cache |
| 186 self._extension_samples_path = extension_samples_path | |
| 187 self._base_path = base_path | 185 self._base_path = base_path |
| 188 self._request = request | 186 self._request = request |
| 189 | 187 |
| 190 def _GetSampleId(self, sample_name): | 188 def _GetSampleId(self, sample_name): |
| 191 return sample_name.lower().replace(' ', '-') | 189 return sample_name.lower().replace(' ', '-') |
| 192 | 190 |
| 193 def _GetAcceptedLanguages(self): | 191 def _GetAcceptedLanguages(self): |
| 194 accept_language = self._request.headers.get('Accept-Language', None) | 192 accept_language = self._request.headers.get('Accept-Language', None) |
| 195 if accept_language is None: | 193 if accept_language is None: |
| 196 return [] | 194 return [] |
| 197 return [lang_with_q.split(';')[0].strip() | 195 return [lang_with_q.split(';')[0].strip() |
| 198 for lang_with_q in accept_language.split(',')] | 196 for lang_with_q in accept_language.split(',')] |
| 199 | 197 |
| 200 def FilterSamples(self, key, api_name): | 198 def FilterSamples(self, key, api_name): |
| 201 '''Fetches and filters the list of samples specified by |key|, returning | 199 '''Fetches and filters the list of samples specified by |key|, returning |
| 202 only the samples that use the API |api_name|. |key| is either 'apps' or | 200 only the samples that use the API |api_name|. |key| is either 'apps' or |
| 203 'extensions'. | 201 'extensions'. |
| 204 ''' | 202 ''' |
| 205 return [sample for sample in self.get(key) if any( | 203 return [sample for sample in self.get(key) if any( |
| 206 call['name'].startswith(api_name + '.') | 204 call['name'].startswith(api_name + '.') |
| 207 for call in sample['api_calls'])] | 205 for call in sample['api_calls'])] |
| 208 | 206 |
| 209 def _CreateSamplesDict(self, key): | 207 def _CreateSamplesDict(self, key): |
| 210 if key == 'apps': | 208 if key == 'apps': |
| 211 samples_list = self._apps_cache.GetFromFileListing('/').Get() | 209 samples_list = self._apps_cache.GetFromFileListing('/').Get() |
| 212 else: | 210 else: |
| 213 samples_list = self._extensions_cache.GetFromFileListing( | 211 samples_list = self._extensions_cache.GetFromFileListing(EXAMPLES).Get() |
| 214 self._extension_samples_path + '/').Get() | |
| 215 return_list = [] | 212 return_list = [] |
| 216 for dict_ in samples_list: | 213 for dict_ in samples_list: |
| 217 name = dict_['name'] | 214 name = dict_['name'] |
| 218 description = dict_['description'] | 215 description = dict_['description'] |
| 219 if description is None: | 216 if description is None: |
| 220 description = '' | 217 description = '' |
| 221 if name.startswith('__MSG_') or description.startswith('__MSG_'): | 218 if name.startswith('__MSG_') or description.startswith('__MSG_'): |
| 222 try: | 219 try: |
| 223 # Copy the sample dict so we don't change the dict in the cache. | 220 # Copy the sample dict so we don't change the dict in the cache. |
| 224 sample_data = dict_.copy() | 221 sample_data = dict_.copy() |
| (...skipping 16 matching lines...) Expand all Loading... |
| 241 else: | 238 else: |
| 242 dict_['id'] = self._GetSampleId(name) | 239 dict_['id'] = self._GetSampleId(name) |
| 243 return_list.append(dict_) | 240 return_list.append(dict_) |
| 244 return return_list | 241 return return_list |
| 245 | 242 |
| 246 def get(self, key): | 243 def get(self, key): |
| 247 return { | 244 return { |
| 248 'apps': lambda: self._CreateSamplesDict('apps'), | 245 'apps': lambda: self._CreateSamplesDict('apps'), |
| 249 'extensions': lambda: self._CreateSamplesDict('extensions') | 246 'extensions': lambda: self._CreateSamplesDict('extensions') |
| 250 }.get(key, lambda: {})() | 247 }.get(key, lambda: {})() |
| OLD | NEW |