| 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 re | 8 import re |
| 9 | 9 |
| 10 from compiled_file_system import CompiledFileSystem | 10 from compiled_file_system import CompiledFileSystem |
| 11 from file_system import FileNotFoundError | 11 from file_system import FileNotFoundError |
| 12 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater | 12 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
| 13 import third_party.json_schema_compiler.model as model | 13 import third_party.json_schema_compiler.model as model |
| 14 import url_constants | 14 import url_constants |
| 15 | 15 |
| 16 DEFAULT_ICON_PATH = '/images/sample-default-icon.png' | 16 DEFAULT_ICON_PATH = 'images/sample-default-icon.png' |
| 17 | 17 |
| 18 class SamplesDataSource(object): | 18 class SamplesDataSource(object): |
| 19 '''Constructs a list of samples and their respective files and api calls. | 19 '''Constructs a list of samples and their respective files and api calls. |
| 20 ''' | 20 ''' |
| 21 class Factory(object): | 21 class Factory(object): |
| 22 '''A factory to create SamplesDataSource instances bound to individual | 22 '''A factory to create SamplesDataSource instances bound to individual |
| 23 Requests. | 23 Requests. |
| 24 ''' | 24 ''' |
| 25 def __init__(self, | 25 def __init__(self, |
| 26 channel, | |
| 27 host_file_system, | 26 host_file_system, |
| 28 compiled_host_fs_factory, | 27 compiled_host_fs_factory, |
| 29 app_samples_file_system, | 28 app_samples_file_system, |
| 30 compiled_app_samples_fs_factory, | 29 compiled_app_samples_fs_factory, |
| 31 ref_resolver_factory, | 30 ref_resolver_factory, |
| 32 extension_samples_path): | 31 extension_samples_path, |
| 32 base_path): |
| 33 self._host_file_system = host_file_system | 33 self._host_file_system = host_file_system |
| 34 self._app_samples_file_system = app_samples_file_system | 34 self._app_samples_file_system = app_samples_file_system |
| 35 self._static_path = '/%s/static' % channel | |
| 36 self._ref_resolver = ref_resolver_factory.Create() | 35 self._ref_resolver = ref_resolver_factory.Create() |
| 37 self._extension_samples_path = extension_samples_path | 36 self._extension_samples_path = extension_samples_path |
| 37 self._base_path = base_path |
| 38 self._extensions_cache = compiled_host_fs_factory.Create( | 38 self._extensions_cache = compiled_host_fs_factory.Create( |
| 39 self._MakeSamplesList, | 39 self._MakeSamplesList, |
| 40 SamplesDataSource, | 40 SamplesDataSource, |
| 41 category='extensions') | 41 category='extensions') |
| 42 self._apps_cache = compiled_app_samples_fs_factory.Create( | 42 self._apps_cache = compiled_app_samples_fs_factory.Create( |
| 43 lambda *args: self._MakeSamplesList(*args, is_apps=True), | 43 lambda *args: self._MakeSamplesList(*args, is_apps=True), |
| 44 SamplesDataSource, | 44 SamplesDataSource, |
| 45 category='apps') | 45 category='apps') |
| 46 | 46 |
| 47 def Create(self, request): | 47 def Create(self, request): |
| 48 '''Returns a new SamplesDataSource bound to |request|. | 48 '''Returns a new SamplesDataSource bound to |request|. |
| 49 ''' | 49 ''' |
| 50 return SamplesDataSource(self._extensions_cache, | 50 return SamplesDataSource(self._extensions_cache, |
| 51 self._apps_cache, | 51 self._apps_cache, |
| 52 self._extension_samples_path, | 52 self._extension_samples_path, |
| 53 self._base_path, |
| 53 request) | 54 request) |
| 54 | 55 |
| 55 def _GetAPIItems(self, js_file): | 56 def _GetAPIItems(self, js_file): |
| 56 chrome_regex = '(chrome\.[a-zA-Z0-9\.]+)' | 57 chrome_regex = '(chrome\.[a-zA-Z0-9\.]+)' |
| 57 calls = set(re.findall(chrome_regex, js_file)) | 58 calls = set(re.findall(chrome_regex, js_file)) |
| 58 # Find APIs that have been assigned into variables. | 59 # Find APIs that have been assigned into variables. |
| 59 assigned_vars = dict(re.findall('var\s*([^\s]+)\s*=\s*%s;' % chrome_regex, | 60 assigned_vars = dict(re.findall('var\s*([^\s]+)\s*=\s*%s;' % chrome_regex, |
| 60 js_file)) | 61 js_file)) |
| 61 # Replace the variable name with the full API name. | 62 # Replace the variable name with the full API name. |
| 62 for var_name, value in assigned_vars.iteritems(): | 63 for var_name, value in assigned_vars.iteritems(): |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 if is_apps: | 153 if is_apps: |
| 153 url = url_constants.GITHUB_BASE + '/' + sample_base_path | 154 url = url_constants.GITHUB_BASE + '/' + sample_base_path |
| 154 icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_base_path | 155 icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_base_path |
| 155 download_url = url | 156 download_url = url |
| 156 else: | 157 else: |
| 157 url = sample_base_path | 158 url = sample_base_path |
| 158 icon_base = sample_base_path | 159 icon_base = sample_base_path |
| 159 download_url = sample_base_path + '.zip' | 160 download_url = sample_base_path + '.zip' |
| 160 | 161 |
| 161 if manifest_data['icon'] is None: | 162 if manifest_data['icon'] is None: |
| 162 icon_path = self._static_path + DEFAULT_ICON_PATH | 163 icon_path = '%s/static/%s' % (self._base_path, DEFAULT_ICON_PATH) |
| 163 else: | 164 else: |
| 164 icon_path = icon_base + '/' + manifest_data['icon'] | 165 icon_path = '%s/%s' % (icon_base, manifest_data['icon']) |
| 165 manifest_data.update({ | 166 manifest_data.update({ |
| 166 'icon': icon_path, | 167 'icon': icon_path, |
| 167 'id': hashlib.md5(url).hexdigest(), | 168 'id': hashlib.md5(url).hexdigest(), |
| 168 'download_url': download_url, | 169 'download_url': download_url, |
| 169 'url': url, | 170 'url': url, |
| 170 'files': [f.replace(sample_path + '/', '') for f in sample_files], | 171 'files': [f.replace(sample_path + '/', '') for f in sample_files], |
| 171 'api_calls': api_calls | 172 'api_calls': api_calls |
| 172 }) | 173 }) |
| 173 samples_list.append(manifest_data) | 174 samples_list.append(manifest_data) |
| 174 | 175 |
| 175 return samples_list | 176 return samples_list |
| 176 | 177 |
| 177 def __init__(self, | 178 def __init__(self, |
| 178 extensions_cache, | 179 extensions_cache, |
| 179 apps_cache, | 180 apps_cache, |
| 180 extension_samples_path, | 181 extension_samples_path, |
| 182 base_path, |
| 181 request): | 183 request): |
| 182 self._extensions_cache = extensions_cache | 184 self._extensions_cache = extensions_cache |
| 183 self._apps_cache = apps_cache | 185 self._apps_cache = apps_cache |
| 184 self._extension_samples_path = extension_samples_path | 186 self._extension_samples_path = extension_samples_path |
| 187 self._base_path = base_path |
| 185 self._request = request | 188 self._request = request |
| 186 | 189 |
| 187 def _GetAcceptedLanguages(self): | 190 def _GetAcceptedLanguages(self): |
| 188 accept_language = self._request.headers.get('Accept-Language', None) | 191 accept_language = self._request.headers.get('Accept-Language', None) |
| 189 if accept_language is None: | 192 if accept_language is None: |
| 190 return [] | 193 return [] |
| 191 return [lang_with_q.split(';')[0].strip() | 194 return [lang_with_q.split(';')[0].strip() |
| 192 for lang_with_q in accept_language.split(',')] | 195 for lang_with_q in accept_language.split(',')] |
| 193 | 196 |
| 194 def FilterSamples(self, key, api_name): | 197 def FilterSamples(self, key, api_name): |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 return_list.append(sample_data) | 248 return_list.append(sample_data) |
| 246 else: | 249 else: |
| 247 return_list.append(dict_) | 250 return_list.append(dict_) |
| 248 return return_list | 251 return return_list |
| 249 | 252 |
| 250 def get(self, key): | 253 def get(self, key): |
| 251 return { | 254 return { |
| 252 'apps': lambda: self._CreateSamplesDict('apps'), | 255 'apps': lambda: self._CreateSamplesDict('apps'), |
| 253 'extensions': lambda: self._CreateSamplesDict('extensions') | 256 'extensions': lambda: self._CreateSamplesDict('extensions') |
| 254 }.get(key, lambda: {})() | 257 }.get(key, lambda: {})() |
| OLD | NEW |