| 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 import traceback |
| 9 | 10 |
| 10 from compiled_file_system import CompiledFileSystem | 11 from compiled_file_system import CompiledFileSystem |
| 11 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 |
| 12 import third_party.json_schema_compiler.model as model | 13 import third_party.json_schema_compiler.model as model |
| 13 import url_constants | 14 import url_constants |
| 14 | 15 |
| 15 DEFAULT_ICON_PATH = 'images/sample-default-icon.png' | 16 DEFAULT_ICON_PATH = 'images/sample-default-icon.png' |
| 16 | 17 |
| 17 class SamplesDataSource(object): | 18 class SamplesDataSource(object): |
| 18 '''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. |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 icon_base = sample_base_path | 145 icon_base = sample_base_path |
| 145 download_url = sample_base_path + '.zip' | 146 download_url = sample_base_path + '.zip' |
| 146 | 147 |
| 147 manifest_data = self._GetDataFromManifest(sample_path, file_system) | 148 manifest_data = self._GetDataFromManifest(sample_path, file_system) |
| 148 if manifest_data['icon'] is None: | 149 if manifest_data['icon'] is None: |
| 149 icon_path = '%s/static/%s' % (self._base_path, DEFAULT_ICON_PATH) | 150 icon_path = '%s/static/%s' % (self._base_path, DEFAULT_ICON_PATH) |
| 150 else: | 151 else: |
| 151 icon_path = '%s/%s' % (icon_base, manifest_data['icon']) | 152 icon_path = '%s/%s' % (icon_base, manifest_data['icon']) |
| 152 manifest_data.update({ | 153 manifest_data.update({ |
| 153 'icon': icon_path, | 154 'icon': icon_path, |
| 154 'id': hashlib.md5(url).hexdigest(), | |
| 155 'download_url': download_url, | 155 'download_url': download_url, |
| 156 'url': url, | 156 'url': url, |
| 157 'files': [f.replace(sample_path + '/', '') for f in sample_files], | 157 'files': [f.replace(sample_path + '/', '') for f in sample_files], |
| 158 'api_calls': api_calls | 158 'api_calls': api_calls |
| 159 }) | 159 }) |
| 160 samples_list.append(manifest_data) | 160 samples_list.append(manifest_data) |
| 161 | 161 |
| 162 return samples_list | 162 return samples_list |
| 163 | 163 |
| 164 def __init__(self, | 164 def __init__(self, |
| 165 extensions_cache, | 165 extensions_cache, |
| 166 apps_cache, | 166 apps_cache, |
| 167 extension_samples_path, | 167 extension_samples_path, |
| 168 base_path, | 168 base_path, |
| 169 request): | 169 request): |
| 170 self._extensions_cache = extensions_cache | 170 self._extensions_cache = extensions_cache |
| 171 self._apps_cache = apps_cache | 171 self._apps_cache = apps_cache |
| 172 self._extension_samples_path = extension_samples_path | 172 self._extension_samples_path = extension_samples_path |
| 173 self._base_path = base_path | 173 self._base_path = base_path |
| 174 self._request = request | 174 self._request = request |
| 175 | 175 |
| 176 def _GetSampleId(self, sample_name): |
| 177 return sample_name.lower().replace(' ', '-') |
| 178 |
| 176 def _GetAcceptedLanguages(self): | 179 def _GetAcceptedLanguages(self): |
| 177 accept_language = self._request.headers.get('Accept-Language', None) | 180 accept_language = self._request.headers.get('Accept-Language', None) |
| 178 if accept_language is None: | 181 if accept_language is None: |
| 179 return [] | 182 return [] |
| 180 return [lang_with_q.split(';')[0].strip() | 183 return [lang_with_q.split(';')[0].strip() |
| 181 for lang_with_q in accept_language.split(',')] | 184 for lang_with_q in accept_language.split(',')] |
| 182 | 185 |
| 183 def FilterSamples(self, key, api_name): | 186 def FilterSamples(self, key, api_name): |
| 184 '''Fetches and filters the list of samples specified by |key|, returning | 187 '''Fetches and filters the list of samples specified by |key|, returning |
| 185 only the samples that use the API |api_name|. |key| is either 'apps' or | 188 only the samples that use the API |api_name|. |key| is either 'apps' or |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 name_key = name[len('__MSG_'):-len('__')] | 223 name_key = name[len('__MSG_'):-len('__')] |
| 221 description_key = description[len('__MSG_'):-len('__')] | 224 description_key = description[len('__MSG_'):-len('__')] |
| 222 locale = sample_data['default_locale'] | 225 locale = sample_data['default_locale'] |
| 223 for lang in self._GetAcceptedLanguages(): | 226 for lang in self._GetAcceptedLanguages(): |
| 224 if lang in sample_data['locales']: | 227 if lang in sample_data['locales']: |
| 225 locale = lang | 228 locale = lang |
| 226 break | 229 break |
| 227 locale_data = sample_data['locales'][locale] | 230 locale_data = sample_data['locales'][locale] |
| 228 sample_data['name'] = locale_data[name_key]['message'] | 231 sample_data['name'] = locale_data[name_key]['message'] |
| 229 sample_data['description'] = locale_data[description_key]['message'] | 232 sample_data['description'] = locale_data[description_key]['message'] |
| 233 sample_data['id'] = self._GetSampleId(sample_data['name']) |
| 230 except Exception as e: | 234 except Exception as e: |
| 231 logging.error(e) | 235 logging.error(traceback.format_exc()) |
| 232 # Revert the sample to the original dict. | 236 # Revert the sample to the original dict. |
| 233 sample_data = dict_ | 237 sample_data = dict_ |
| 234 return_list.append(sample_data) | 238 return_list.append(sample_data) |
| 235 else: | 239 else: |
| 240 dict_['id'] = self._GetSampleId(name) |
| 236 return_list.append(dict_) | 241 return_list.append(dict_) |
| 237 return return_list | 242 return return_list |
| 238 | 243 |
| 239 def get(self, key): | 244 def get(self, key): |
| 240 return { | 245 return { |
| 241 'apps': lambda: self._CreateSamplesDict('apps'), | 246 'apps': lambda: self._CreateSamplesDict('apps'), |
| 242 'extensions': lambda: self._CreateSamplesDict('extensions') | 247 'extensions': lambda: self._CreateSamplesDict('extensions') |
| 243 }.get(key, lambda: {})() | 248 }.get(key, lambda: {})() |
| OLD | NEW |