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