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