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