OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 json | 5 import json |
6 import logging | 6 import logging |
7 import posixpath | 7 import posixpath |
8 import re | 8 import re |
9 | 9 |
10 from extensions_paths import EXAMPLES | 10 from extensions_paths import EXAMPLES |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 self._text_cache = compiled_fs_factory.ForUnicode(self._samples_fs) | 51 self._text_cache = compiled_fs_factory.ForUnicode(self._samples_fs) |
52 self._reference_resolver = reference_resolver | 52 self._reference_resolver = reference_resolver |
53 self._base_path = base_path | 53 self._base_path = base_path |
54 self._platform = platform | 54 self._platform = platform |
55 | 55 |
56 def GetCache(self): | 56 def GetCache(self): |
57 return self._samples_cache | 57 return self._samples_cache |
58 | 58 |
59 def FilterSamples(self, api_name): | 59 def FilterSamples(self, api_name): |
60 '''Fetches and filters the list of samples for this platform, returning | 60 '''Fetches and filters the list of samples for this platform, returning |
61 only the samples that use the API |api_name|. | 61 a Future to the only the samples that use the API |api_name|. |
62 ''' | 62 ''' |
63 try: | 63 def filter_samples(samples_list): |
| 64 return [sample for sample in samples_list |
| 65 if any(call['name'].startswith(api_name + '.') |
| 66 for call in sample['api_calls'])] |
| 67 def handle_error(_): |
64 # TODO(rockot): This cache is probably not working as intended, since | 68 # TODO(rockot): This cache is probably not working as intended, since |
65 # it can still lead to underlying filesystem (e.g. gitiles) access | 69 # it can still lead to underlying filesystem (e.g. gitiles) access |
66 # while processing live requests. Because this can fail, we at least | 70 # while processing live requests. Because this can fail, we at least |
67 # trap and log exceptions to prevent 500s from being thrown. | 71 # trap and log exceptions to prevent 500s from being thrown. |
68 samples_list = self._samples_cache.GetFromFileListing( | |
69 '' if self._platform == 'apps' else EXAMPLES).Get() | |
70 except Exception as e: | |
71 logging.warning('Unable to get samples listing. Skipping.') | 72 logging.warning('Unable to get samples listing. Skipping.') |
72 samples_list = [] | 73 return [] |
73 | 74 platform_for_samples = '' if self._platform == 'apps' else EXAMPLES |
74 return [sample for sample in samples_list if any( | 75 return (self._samples_cache.GetFromFileListing(platform_for_samples) |
75 call['name'].startswith(api_name + '.') | 76 .Then(filter_samples, error_handler=handle_error)) |
76 for call in sample['api_calls'])] | |
77 | 77 |
78 def _GetDataFromManifest(self, path, file_system): | 78 def _GetDataFromManifest(self, path, file_system): |
79 manifest = self._text_cache.GetFromFile(path + '/manifest.json').Get() | 79 manifest = self._text_cache.GetFromFile(path + '/manifest.json').Get() |
80 try: | 80 try: |
81 manifest_json = json.loads(json_comment_eater.Nom(manifest)) | 81 manifest_json = json.loads(json_comment_eater.Nom(manifest)) |
82 except ValueError as e: | 82 except ValueError as e: |
83 logging.error('Error parsing manifest.json for %s: %s' % (path, e)) | 83 logging.error('Error parsing manifest.json for %s: %s' % (path, e)) |
84 return None | 84 return None |
85 l10n_data = { | 85 l10n_data = { |
86 'name': manifest_json.get('name', ''), | 86 'name': manifest_json.get('name', ''), |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 manifest_data.update({ | 170 manifest_data.update({ |
171 'icon': icon_path, | 171 'icon': icon_path, |
172 'download_url': download_url, | 172 'download_url': download_url, |
173 'url': url, | 173 'url': url, |
174 'files': [f.replace(sample_path + '/', '') for f in sample_files], | 174 'files': [f.replace(sample_path + '/', '') for f in sample_files], |
175 'api_calls': api_calls | 175 'api_calls': api_calls |
176 }) | 176 }) |
177 samples_list.append(manifest_data) | 177 samples_list.append(manifest_data) |
178 | 178 |
179 return samples_list | 179 return samples_list |
OLD | NEW |