| 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, api_items, js_file): | 16 def _GetApiItems(self, js_file): |
| 17 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', | 17 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) |
| 18 self._fetcher.FetchResource(js_file).content)) | |
| 19 | 18 |
| 20 def _MakeApiLink(self, prefix, item): | 19 def _MakeApiLink(self, prefix, item): |
| 21 api, name = item.replace('chrome.', '').split('.', 1) | 20 api, name = item.replace('chrome.', '').split('.', 1) |
| 22 return api + '.html#' + prefix + '-' + name | 21 return api + '.html#' + prefix + '-' + name |
| 23 | 22 |
| 24 def _GetDataFromManifest(self, path): | 23 def _GetDataFromManifest(self, path): |
| 25 manifest = self._fetcher.FetchResource(path + '/manifest.json').content | 24 manifest_path = path + '/manifest.json' |
| 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 (manifest_json.get('name'), manifest_json.get('description')) |
| 28 | 28 |
| 29 def _MakeSamplesList(self, files): | 29 def _MakeSamplesList(self, files): |
| 30 samples_list = [] | 30 samples_list = [] |
| 31 for filename in files: | 31 for filename in sorted(files): |
| 32 if filename.rsplit('/')[-1] != 'manifest.json': | 32 if filename.rsplit('/')[-1] != 'manifest.json': |
| 33 continue | 33 continue |
| 34 # This is a little hacky, but it makes a sample page. | 34 # This is a little hacky, but it makes a sample page. |
| 35 sample_path = filename.rsplit('/', 1)[-2] | 35 sample_path = filename.rsplit('/', 1)[-2] |
| 36 sample_files = filter(lambda x: x.startswith(sample_path + '/'), files) | 36 sample_files = filter(lambda x: x.startswith(sample_path + '/'), files) |
| 37 api_items = set([]) | 37 js_files = filter(lambda x: x.endswith('.js'), sample_files) |
| 38 for file_ in sample_files: | 38 js_contents = self._fetcher.Read(js_files).Get() |
| 39 if file_.endswith('.js'): | 39 api_items = set() |
| 40 api_items.update(self._GetApiItems(api_items, file_)) | 40 for js in js_contents.values(): |
| 41 api_items.update(self._GetApiItems(js)) |
| 41 | 42 |
| 42 api_calls = [] | 43 api_calls = [] |
| 43 for item in api_items: | 44 for item in api_items: |
| 44 if len(item.split('.')) < 3: | 45 if len(item.split('.')) < 3: |
| 45 continue | 46 continue |
| 46 if item.endswith('.addListener'): | 47 if item.endswith('.addListener'): |
| 47 item = item.replace('.addListener', '') | 48 item = item.replace('.addListener', '') |
| 48 api_calls.append({ | 49 api_calls.append({ |
| 49 'name': item, | 50 'name': item, |
| 50 'link': self._MakeApiLink('event', item) | 51 'link': self._MakeApiLink('event', item) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 61 'path': sample_path.split('/', 1)[1], | 62 'path': sample_path.split('/', 1)[1], |
| 62 'files': [f.replace(sample_path + '/', '') for f in sample_files], | 63 'files': [f.replace(sample_path + '/', '') for f in sample_files], |
| 63 'api_calls': api_calls | 64 'api_calls': api_calls |
| 64 }) | 65 }) |
| 65 return samples_list | 66 return samples_list |
| 66 | 67 |
| 67 def __getitem__(self, key): | 68 def __getitem__(self, key): |
| 68 return self.get(key) | 69 return self.get(key) |
| 69 | 70 |
| 70 def get(self, key): | 71 def get(self, key): |
| 71 return self._cache.getFromFileListing('docs/' + self._samples_path, True) | 72 return self._cache.GetFromFileListing(self._samples_path + '/') |
| OLD | NEW |