Chromium Code Reviews| 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, api_items, js_file): |
| 17 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', | 17 api_items.update(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 = self._fetcher.Read( |
| 25 [path + '/manifest.json'])[path + '/manifest.json'].Get() | |
|
not at google - send to devlin
2012/07/18 10:39:16
seems like you should hold onto path + '/manifest.
cduvall
2012/07/18 21:26:10
Done.
| |
| 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 js_files = filter(lambda x: x.endswith('.js'), sample_files) | |
| 38 js_contents = self._fetcher.Read(js_files) | |
| 37 api_items = set([]) | 39 api_items = set([]) |
|
not at google - send to devlin
2012/07/18 10:39:16
don't need the [], just set()
cduvall
2012/07/18 21:26:10
Done.
| |
| 38 for file_ in sample_files: | 40 [self._GetApiItems(api_items, js.Get()) for js in js_contents.values()] |
|
not at google - send to devlin
2012/07/18 10:39:16
I preferred the way it was... side-effectful list
cduvall
2012/07/18 21:26:10
Done.
| |
| 39 if file_.endswith('.js'): | |
| 40 api_items.update(self._GetApiItems(api_items, file_)) | |
| 41 | 41 |
| 42 api_calls = [] | 42 api_calls = [] |
| 43 for item in api_items: | 43 for item in api_items: |
| 44 if len(item.split('.')) < 3: | 44 if len(item.split('.')) < 3: |
| 45 continue | 45 continue |
| 46 if item.endswith('.addListener'): | 46 if item.endswith('.addListener'): |
| 47 item = item.replace('.addListener', '') | 47 item = item.replace('.addListener', '') |
| 48 api_calls.append({ | 48 api_calls.append({ |
| 49 'name': item, | 49 'name': item, |
| 50 'link': self._MakeApiLink('event', item) | 50 'link': self._MakeApiLink('event', item) |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 61 'path': sample_path.split('/', 1)[1], | 61 'path': sample_path.split('/', 1)[1], |
| 62 'files': [f.replace(sample_path + '/', '') for f in sample_files], | 62 'files': [f.replace(sample_path + '/', '') for f in sample_files], |
| 63 'api_calls': api_calls | 63 'api_calls': api_calls |
| 64 }) | 64 }) |
| 65 return samples_list | 65 return samples_list |
| 66 | 66 |
| 67 def __getitem__(self, key): | 67 def __getitem__(self, key): |
| 68 return self.get(key) | 68 return self.get(key) |
| 69 | 69 |
| 70 def get(self, key): | 70 def get(self, key): |
| 71 return self._cache.getFromFileListing('docs/' + self._samples_path, True) | 71 return self._cache.getFromFileListing(self._samples_path + '/') |
| OLD | NEW |