| 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 json | 5 import json |
| 6 import re | 6 import re |
| 7 | 7 |
| 8 class SamplesDataSource(object): | 8 class SamplesDataSource(object): |
| 9 """Constructs a list of samples and their respective files and api calls. | 9 """Constructs a list of samples and their respective files and api calls. |
| 10 """ | 10 """ |
| 11 def __init__(self, fetcher, cache_builder, samples_path): | 11 def __init__(self, fetcher, cache_builder, samples_path): |
| 12 self._fetcher = fetcher | 12 self._fetcher = fetcher |
| 13 self._cache = cache_builder.build(self._MakeSamplesList) | 13 self._cache = cache_builder.build(self._MakeSamplesList) |
| 14 self._samples_path = samples_path | 14 self._samples_path = samples_path |
| 15 | 15 |
| 16 def _GetApiItems(self, js_file): | 16 def _GetApiItems(self, js_file): |
| 17 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) | 17 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) |
| 18 | 18 |
| 19 def _MakeApiLink(self, prefix, item): | 19 def _MakeApiLink(self, prefix, item): |
| 20 api, name = item.replace('chrome.', '').split('.', 1) | 20 api, name = item.replace('chrome.', '').split('.', 1) |
| 21 return api + '.html#' + prefix + '-' + name | 21 return api + '.html#' + prefix + '-' + name |
| 22 | 22 |
| 23 def _GetDataFromManifest(self, path): | 23 def _GetDataFromManifest(self, path): |
| 24 manifest_path = path + '/manifest.json' | 24 manifest_path = path + '/manifest.json' |
| 25 manifest = self._fetcher.Read([manifest_path]).Get()[manifest_path] | 25 manifest = self._fetcher.Read([manifest_path]).Get()[manifest_path] |
| 26 manifest_json = json.loads(manifest) | 26 manifest_json = json.loads(manifest) |
| 27 return (manifest_json.get('name'), manifest_json.get('description')) | 27 return { |
| 28 'name': manifest_json.get('name'), |
| 29 'description': manifest_json.get('description'), |
| 30 'icon': manifest_json.get('icons', {}).get('128', None) |
| 31 } |
| 28 | 32 |
| 29 def _MakeSamplesList(self, files): | 33 def _MakeSamplesList(self, files): |
| 30 samples_list = [] | 34 samples_list = [] |
| 31 for filename in sorted(files): | 35 for filename in sorted(files): |
| 32 if filename.rsplit('/')[-1] != 'manifest.json': | 36 if filename.rsplit('/')[-1] != 'manifest.json': |
| 33 continue | 37 continue |
| 34 # This is a little hacky, but it makes a sample page. | 38 # This is a little hacky, but it makes a sample page. |
| 35 sample_path = filename.rsplit('/', 1)[-2] | 39 sample_path = filename.rsplit('/', 1)[-2] |
| 36 sample_files = filter(lambda x: x.startswith(sample_path + '/'), files) | 40 sample_files = filter(lambda x: x.startswith(sample_path + '/'), files) |
| 37 js_files = filter(lambda x: x.endswith('.js'), sample_files) | 41 js_files = filter(lambda x: x.endswith('.js'), sample_files) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 48 item = item.replace('.addListener', '') | 52 item = item.replace('.addListener', '') |
| 49 api_calls.append({ | 53 api_calls.append({ |
| 50 'name': item, | 54 'name': item, |
| 51 'link': self._MakeApiLink('event', item) | 55 'link': self._MakeApiLink('event', item) |
| 52 }) | 56 }) |
| 53 else: | 57 else: |
| 54 api_calls.append({ | 58 api_calls.append({ |
| 55 'name': item, | 59 'name': item, |
| 56 'link': self._MakeApiLink('method', item) | 60 'link': self._MakeApiLink('method', item) |
| 57 }) | 61 }) |
| 58 name, description = self._GetDataFromManifest(sample_path) | 62 samples_info = self._GetDataFromManifest(sample_path) |
| 59 samples_list.append({ | 63 samples_info.update({ |
| 60 'name': name, | |
| 61 'description': description, | |
| 62 'path': sample_path.split('/', 1)[1], | 64 'path': sample_path.split('/', 1)[1], |
| 63 'files': [f.replace(sample_path + '/', '') for f in sample_files], | 65 'files': [f.replace(sample_path + '/', '') for f in sample_files], |
| 64 'api_calls': api_calls | 66 'api_calls': api_calls |
| 65 }) | 67 }) |
| 68 samples_list.append(samples_info) |
| 66 return samples_list | 69 return samples_list |
| 67 | 70 |
| 68 def __getitem__(self, key): | 71 def __getitem__(self, key): |
| 69 return self.get(key) | 72 return self.get(key) |
| 70 | 73 |
| 71 def get(self, key): | 74 def get(self, key): |
| 72 return self._cache.GetFromFileListing(self._samples_path + '/') | 75 return self._cache.GetFromFileListing(self._samples_path + '/') |
| OLD | NEW |