| 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, | 26 channel, |
| 27 extensions_file_system, | 27 host_file_system, |
| 28 apps_file_system, | 28 compiled_host_fs_factory, |
| 29 app_samples_file_system, |
| 30 compiled_app_samples_fs_factory, |
| 29 ref_resolver_factory, | 31 ref_resolver_factory, |
| 30 object_store_creator_factory, | |
| 31 extension_samples_path): | 32 extension_samples_path): |
| 32 self._svn_file_system = extensions_file_system | 33 self._host_file_system = host_file_system |
| 33 self._github_file_system = apps_file_system | 34 self._app_samples_file_system = app_samples_file_system |
| 34 self._static_path = '/%s/static' % channel | 35 self._static_path = '/%s/static' % channel |
| 35 self._ref_resolver = ref_resolver_factory.Create() | 36 self._ref_resolver = ref_resolver_factory.Create() |
| 36 self._extension_samples_path = extension_samples_path | 37 self._extension_samples_path = extension_samples_path |
| 37 def create_compiled_fs(fs, fn, category): | 38 self._extensions_cache = compiled_host_fs_factory.Create( |
| 38 return CompiledFileSystem.Factory( | 39 self._MakeSamplesList, |
| 39 fs, | 40 SamplesDataSource, |
| 40 object_store_creator_factory).Create(fn, | 41 category='extensions') |
| 41 SamplesDataSource, | 42 self._apps_cache = compiled_app_samples_fs_factory.Create( |
| 42 category=category) | 43 lambda *args: self._MakeSamplesList(*args, is_apps=True), |
| 43 self._extensions_cache = create_compiled_fs(extensions_file_system, | 44 SamplesDataSource, |
| 44 self._MakeSamplesList, | 45 category='apps') |
| 45 'extensions') | |
| 46 self._apps_cache = create_compiled_fs(apps_file_system, | |
| 47 lambda *args: self._MakeSamplesList( | |
| 48 *args, is_apps=True), | |
| 49 'apps') | |
| 50 | 46 |
| 51 def Create(self, request): | 47 def Create(self, request): |
| 52 """Returns a new SamplesDataSource bound to |request|. | 48 '''Returns a new SamplesDataSource bound to |request|. |
| 53 """ | 49 ''' |
| 54 return SamplesDataSource(self._extensions_cache, | 50 return SamplesDataSource(self._extensions_cache, |
| 55 self._apps_cache, | 51 self._apps_cache, |
| 56 self._extension_samples_path, | 52 self._extension_samples_path, |
| 57 request) | 53 request) |
| 58 | 54 |
| 59 def _GetAPIItems(self, js_file): | 55 def _GetAPIItems(self, js_file): |
| 60 chrome_regex = '(chrome\.[a-zA-Z0-9\.]+)' | 56 chrome_regex = '(chrome\.[a-zA-Z0-9\.]+)' |
| 61 calls = set(re.findall(chrome_regex, js_file)) | 57 calls = set(re.findall(chrome_regex, js_file)) |
| 62 # Find APIs that have been assigned into variables. | 58 # Find APIs that have been assigned into variables. |
| 63 assigned_vars = dict(re.findall('var\s*([^\s]+)\s*=\s*%s;' % chrome_regex, | 59 assigned_vars = dict(re.findall('var\s*([^\s]+)\s*=\s*%s;' % chrome_regex, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 logging.error('Error parsing locales files for %s: %s' % (path, e)) | 92 logging.error('Error parsing locales files for %s: %s' % (path, e)) |
| 97 else: | 93 else: |
| 98 for path, json_ in locales_json: | 94 for path, json_ in locales_json: |
| 99 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_ | 95 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_ |
| 100 return l10n_data | 96 return l10n_data |
| 101 | 97 |
| 102 def _MakeSamplesList(self, base_dir, files, is_apps=False): | 98 def _MakeSamplesList(self, base_dir, files, is_apps=False): |
| 103 # HACK(kalman): The code here (for legacy reasons) assumes that |files| is | 99 # HACK(kalman): The code here (for legacy reasons) assumes that |files| is |
| 104 # prefixed by |base_dir|, so make it true. | 100 # prefixed by |base_dir|, so make it true. |
| 105 files = ['%s%s' % (base_dir, f) for f in files] | 101 files = ['%s%s' % (base_dir, f) for f in files] |
| 106 file_system = (self._github_file_system if is_apps else | 102 file_system = (self._app_samples_file_system if is_apps else |
| 107 self._svn_file_system) | 103 self._host_file_system) |
| 108 samples_list = [] | 104 samples_list = [] |
| 109 for filename in sorted(files): | 105 for filename in sorted(files): |
| 110 if filename.rsplit('/')[-1] != 'manifest.json': | 106 if filename.rsplit('/')[-1] != 'manifest.json': |
| 111 continue | 107 continue |
| 112 # This is a little hacky, but it makes a sample page. | 108 # This is a little hacky, but it makes a sample page. |
| 113 sample_path = filename.rsplit('/', 1)[-2] | 109 sample_path = filename.rsplit('/', 1)[-2] |
| 114 sample_files = [path for path in files | 110 sample_files = [path for path in files |
| 115 if path.startswith(sample_path + '/')] | 111 if path.startswith(sample_path + '/')] |
| 116 js_files = [path for path in sample_files if path.endswith('.js')] | 112 js_files = [path for path in sample_files if path.endswith('.js')] |
| 117 try: | 113 try: |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 self._request = request | 185 self._request = request |
| 190 | 186 |
| 191 def _GetAcceptedLanguages(self): | 187 def _GetAcceptedLanguages(self): |
| 192 accept_language = self._request.headers.get('Accept-Language', None) | 188 accept_language = self._request.headers.get('Accept-Language', None) |
| 193 if accept_language is None: | 189 if accept_language is None: |
| 194 return [] | 190 return [] |
| 195 return [lang_with_q.split(';')[0].strip() | 191 return [lang_with_q.split(';')[0].strip() |
| 196 for lang_with_q in accept_language.split(',')] | 192 for lang_with_q in accept_language.split(',')] |
| 197 | 193 |
| 198 def FilterSamples(self, key, api_name): | 194 def FilterSamples(self, key, api_name): |
| 199 """Fetches and filters the list of samples specified by |key|, returning | 195 '''Fetches and filters the list of samples specified by |key|, returning |
| 200 only the samples that use the API |api_name|. |key| is either 'apps' or | 196 only the samples that use the API |api_name|. |key| is either 'apps' or |
| 201 'extensions'. | 197 'extensions'. |
| 202 """ | 198 ''' |
| 203 api_search = api_name + '_' | 199 api_search = api_name + '_' |
| 204 samples_list = [] | 200 samples_list = [] |
| 205 try: | 201 try: |
| 206 for sample in self.get(key): | 202 for sample in self.get(key): |
| 207 api_calls_unix = [model.UnixName(call['name']) | 203 api_calls_unix = [model.UnixName(call['name']) |
| 208 for call in sample['api_calls']] | 204 for call in sample['api_calls']] |
| 209 for call in api_calls_unix: | 205 for call in api_calls_unix: |
| 210 if call.startswith(api_search): | 206 if call.startswith(api_search): |
| 211 samples_list.append(sample) | 207 samples_list.append(sample) |
| 212 break | 208 break |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 return_list.append(sample_data) | 245 return_list.append(sample_data) |
| 250 else: | 246 else: |
| 251 return_list.append(dict_) | 247 return_list.append(dict_) |
| 252 return return_list | 248 return return_list |
| 253 | 249 |
| 254 def get(self, key): | 250 def get(self, key): |
| 255 return { | 251 return { |
| 256 'apps': lambda: self._CreateSamplesDict('apps'), | 252 'apps': lambda: self._CreateSamplesDict('apps'), |
| 257 'extensions': lambda: self._CreateSamplesDict('extensions') | 253 'extensions': lambda: self._CreateSamplesDict('extensions') |
| 258 }.get(key, lambda: {})() | 254 }.get(key, lambda: {})() |
| OLD | NEW |