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 hashlib | 5 import hashlib |
| 6 import json | 6 import json |
| 7 import logging | 7 import logging |
| 8 import re | 8 import re |
| 9 | 9 |
| 10 import compiled_file_system as compiled_fs | 10 import compiled_file_system as compiled_fs |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 class Factory(object): | 21 class Factory(object): |
| 22 """A factory to create SamplesDataSource instances bound to individual | 22 """A factory to create SamplesDataSource instances bound to individual |
| 23 Requests. | 23 Requests. |
| 24 """ | 24 """ |
| 25 def __init__(self, | 25 def __init__(self, |
| 26 channel, | 26 channel, |
| 27 file_system, | 27 file_system, |
| 28 github_file_system, | 28 github_file_system, |
| 29 cache_factory, | 29 cache_factory, |
| 30 github_cache_factory, | 30 github_cache_factory, |
| 31 api_list_data_source_factory, | |
| 31 samples_path): | 32 samples_path): |
| 32 self._file_system = file_system | 33 self._file_system = file_system |
| 33 self._github_file_system = github_file_system | 34 self._github_file_system = github_file_system |
| 34 self._static_path = ((('/' + channel) if channel != 'local' else '') + | 35 self._static_path = ((('/' + channel) if channel != 'local' else '') + |
| 35 '/static') | 36 '/static') |
| 36 self._extensions_cache = cache_factory.Create(self._MakeSamplesList, | 37 self._extensions_cache = cache_factory.Create(self._MakeSamplesList, |
| 37 compiled_fs.EXTENSIONS) | 38 compiled_fs.EXTENSIONS) |
| 38 self._apps_cache = github_cache_factory.Create( | 39 self._apps_cache = github_cache_factory.Create( |
| 39 lambda x: self._MakeSamplesList(x, is_apps=True), | 40 lambda x: self._MakeSamplesList(x, is_apps=True), |
| 40 compiled_fs.APPS) | 41 compiled_fs.APPS) |
| 42 self._api_list_data_source = api_list_data_source_factory.Create() | |
| 41 self._samples_path = samples_path | 43 self._samples_path = samples_path |
| 42 | 44 |
| 43 def Create(self, request): | 45 def Create(self, request): |
| 44 """Returns a new SamplesDataSource bound to |request|. | 46 """Returns a new SamplesDataSource bound to |request|. |
| 45 """ | 47 """ |
| 46 return SamplesDataSource(self._extensions_cache, | 48 return SamplesDataSource(self._extensions_cache, |
| 47 self._apps_cache, | 49 self._apps_cache, |
| 48 self._samples_path, | 50 self._samples_path, |
| 49 request) | 51 request) |
| 50 | 52 |
| 51 def _GetApiItems(self, js_file): | 53 def _MakeAPIList(self): |
|
not at google - send to devlin
2012/09/11 23:40:00
_GetAllAPINames?
we're going to be calculating it
cduvall
2012/09/12 21:59:05
Done.
| |
| 54 apis = [] | |
| 55 for k1 in ['apps', 'extensions']: | |
| 56 for k2 in ['chrome', 'experimental']: | |
| 57 apis.extend( | |
| 58 [api['name'] for api in self._api_list_data_source[k1][k2]]) | |
| 59 return apis | |
| 60 | |
| 61 def _GetAPIItems(self, js_file): | |
| 52 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) | 62 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) |
| 53 | 63 |
| 54 def _MakeApiLink(self, prefix, item): | 64 def _MakeAPILink(self, prefix, item): |
| 55 api, name = item.replace('chrome.', '').split('.', 1) | 65 item = item.replace('chrome.', '') |
| 56 return api + '.html#' + prefix + '-' + name | 66 parts = item.split('.') |
| 67 api_name = [] | |
| 68 api_list = self._MakeAPIList() | |
| 69 for i in range(1, len(parts) + 1): | |
| 70 if '.'.join(parts[:i]) in api_list: | |
| 71 return '%s.html#%s-%s' % ('.'.join(parts[:i]), | |
| 72 prefix, | |
| 73 '.'.join(parts[i:])) | |
| 74 return None | |
| 57 | 75 |
| 58 def _GetDataFromManifest(self, path, file_system): | 76 def _GetDataFromManifest(self, path, file_system): |
| 59 manifest = file_system.ReadSingle(path + '/manifest.json') | 77 manifest = file_system.ReadSingle(path + '/manifest.json') |
| 60 try: | 78 try: |
| 61 manifest_json = json.loads(json_comment_eater.Nom(manifest)) | 79 manifest_json = json.loads(json_comment_eater.Nom(manifest)) |
| 62 except ValueError as e: | 80 except ValueError as e: |
| 63 logging.error('Error parsing manifest.json for %s: %s' % (path, e)) | 81 logging.error('Error parsing manifest.json for %s: %s' % (path, e)) |
| 64 return None | 82 return None |
| 65 l10n_data = { | 83 l10n_data = { |
| 66 'name': manifest_json.get('name', ''), | 84 'name': manifest_json.get('name', ''), |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 94 if filename.rsplit('/')[-1] != 'manifest.json': | 112 if filename.rsplit('/')[-1] != 'manifest.json': |
| 95 continue | 113 continue |
| 96 # This is a little hacky, but it makes a sample page. | 114 # This is a little hacky, but it makes a sample page. |
| 97 sample_path = filename.rsplit('/', 1)[-2] | 115 sample_path = filename.rsplit('/', 1)[-2] |
| 98 sample_files = [path for path in files | 116 sample_files = [path for path in files |
| 99 if path.startswith(sample_path + '/')] | 117 if path.startswith(sample_path + '/')] |
| 100 js_files = [path for path in sample_files if path.endswith('.js')] | 118 js_files = [path for path in sample_files if path.endswith('.js')] |
| 101 js_contents = file_system.Read(js_files).Get() | 119 js_contents = file_system.Read(js_files).Get() |
| 102 api_items = set() | 120 api_items = set() |
| 103 for js in js_contents.values(): | 121 for js in js_contents.values(): |
| 104 api_items.update(self._GetApiItems(js)) | 122 api_items.update(self._GetAPIItems(js)) |
| 105 | 123 |
| 106 api_calls = [] | 124 api_calls = [] |
| 107 for item in api_items: | 125 for item in api_items: |
| 108 if len(item.split('.')) < 3: | 126 if len(item.split('.')) < 3: |
| 109 continue | 127 continue |
| 128 if item.endswith('.removeListener'): | |
| 129 continue | |
|
not at google - send to devlin
2012/09/11 23:40:00
You might also want to add hasListener for complet
cduvall
2012/09/12 21:59:05
Done.
| |
| 110 if item.endswith('.addListener'): | 130 if item.endswith('.addListener'): |
| 111 item = item.replace('.addListener', '') | 131 item = item[:-len('.addListener')] |
| 132 link = self._MakeAPILink('event', item) | |
| 133 if link is None: | |
| 134 continue | |
| 112 api_calls.append({ | 135 api_calls.append({ |
| 113 'name': item, | 136 'name': item, |
| 114 'link': self._MakeApiLink('event', item) | 137 'link': link |
| 115 }) | 138 }) |
| 116 else: | 139 else: |
| 140 link = self._MakeAPILink('method', item) | |
|
not at google - send to devlin
2012/09/11 23:40:00
TODO(cduvall): this might be a property.
cduvall
2012/09/12 21:59:05
Done.
not at google - send to devlin
2012/09/14 00:47:19
or a type (sorry)
cduvall
2012/09/14 01:10:28
Done.
| |
| 141 if link is None: | |
| 142 continue | |
| 117 api_calls.append({ | 143 api_calls.append({ |
| 118 'name': item, | 144 'name': item, |
| 119 'link': self._MakeApiLink('method', item) | 145 'link': link |
| 120 }) | 146 }) |
| 121 manifest_data = self._GetDataFromManifest(sample_path, file_system) | 147 manifest_data = self._GetDataFromManifest(sample_path, file_system) |
| 122 if manifest_data is None: | 148 if manifest_data is None: |
| 123 continue | 149 continue |
| 124 | 150 |
| 125 sample_base_path = sample_path.split('/', 1)[1] | 151 sample_base_path = sample_path.split('/', 1)[1] |
| 126 if is_apps: | 152 if is_apps: |
| 127 url = url_constants.GITHUB_BASE + '/' + sample_base_path | 153 url = url_constants.GITHUB_BASE + '/' + sample_base_path |
| 128 icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_base_path | 154 icon_base = url_constants.RAW_GITHUB_BASE + '/' + sample_base_path |
| 129 download_url = url | 155 download_url = url |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 return return_list | 242 return return_list |
| 217 | 243 |
| 218 def __getitem__(self, key): | 244 def __getitem__(self, key): |
| 219 return self.get(key) | 245 return self.get(key) |
| 220 | 246 |
| 221 def get(self, key): | 247 def get(self, key): |
| 222 return { | 248 return { |
| 223 'apps': lambda: self._CreateSamplesDict('apps'), | 249 'apps': lambda: self._CreateSamplesDict('apps'), |
| 224 'extensions': lambda: self._CreateSamplesDict('extensions') | 250 'extensions': lambda: self._CreateSamplesDict('extensions') |
| 225 }.get(key, lambda: {})() | 251 }.get(key, lambda: {})() |
| OLD | NEW |