Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Side by Side Diff: chrome/common/extensions/docs/server2/samples_data_source.py

Issue 11315018: Extensions Docs Server: Generalize $ref's to work for any schema node (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 from docs_server_utils import GetLinkToRefType
11 from file_system import FileNotFoundError 12 from file_system import FileNotFoundError
12 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater 13 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater
13 import third_party.json_schema_compiler.model as model 14 import third_party.json_schema_compiler.model as model
14 import url_constants 15 import url_constants
15 16
16 DEFAULT_ICON_PATH = '/images/sample-default-icon.png' 17 DEFAULT_ICON_PATH = '/images/sample-default-icon.png'
17 18
18 def _MakeAPILink(prefix, item, api_list):
19 item = item.replace('chrome.', '')
20 parts = item.split('.')
21 api_name = []
22 for i in range(1, len(parts) + 1):
23 if '.'.join(parts[:i]) in api_list:
24 return '%s.html#%s-%s' % ('.'.join(parts[:i]),
25 prefix,
26 '.'.join(parts[i:]))
27 return None
28
29 class SamplesDataSource(object): 19 class SamplesDataSource(object):
30 """Constructs a list of samples and their respective files and api calls. 20 """Constructs a list of samples and their respective files and api calls.
31 """ 21 """
32 22
33 class Factory(object): 23 class Factory(object):
34 """A factory to create SamplesDataSource instances bound to individual 24 """A factory to create SamplesDataSource instances bound to individual
35 Requests. 25 Requests.
36 """ 26 """
37 def __init__(self, 27 def __init__(self,
38 channel, 28 channel,
39 file_system, 29 file_system,
40 github_file_system, 30 github_file_system,
41 cache_factory, 31 cache_factory,
42 github_cache_factory, 32 github_cache_factory,
33 api_data_source_factory,
43 api_list_data_source_factory, 34 api_list_data_source_factory,
44 samples_path): 35 samples_path):
45 self._file_system = file_system 36 self._file_system = file_system
46 self._github_file_system = github_file_system 37 self._github_file_system = github_file_system
47 self._static_path = ((('/' + channel) if channel != 'local' else '') + 38 self._static_path = ((('/' + channel) if channel != 'local' else '') +
48 '/static') 39 '/static')
49 self._extensions_cache = cache_factory.Create(self._MakeSamplesList, 40 self._extensions_cache = cache_factory.Create(self._MakeSamplesList,
50 compiled_fs.EXTENSIONS) 41 compiled_fs.EXTENSIONS)
51 self._apps_cache = github_cache_factory.Create( 42 self._apps_cache = github_cache_factory.Create(
52 lambda x: self._MakeSamplesList(x, is_apps=True), 43 lambda x: self._MakeSamplesList(x, is_apps=True),
53 compiled_fs.APPS) 44 compiled_fs.APPS)
54 self._api_list_data_source = api_list_data_source_factory.Create() 45 self._api_list_data_source = api_list_data_source_factory.Create()
55 self._samples_path = samples_path 46 self._samples_path = samples_path
47 self._api_data_source = api_data_source_factory.Create(None,
not at google - send to devlin 2012/11/01 22:00:49 Could you pass in a fake request rather than None?
cduvall 2012/11/02 01:53:53 Done.
48 self.Create(None),
49 resolve_refs=False)
56 50
57 def Create(self, request): 51 def Create(self, request):
58 """Returns a new SamplesDataSource bound to |request|. 52 """Returns a new SamplesDataSource bound to |request|.
59 """ 53 """
60 return SamplesDataSource(self._extensions_cache, 54 return SamplesDataSource(self._extensions_cache,
61 self._apps_cache, 55 self._apps_cache,
62 self._samples_path, 56 self._samples_path,
63 request) 57 request)
64 58
65 def _GetAllAPINames(self):
66 apis = []
67 for k1 in ['apps', 'extensions']:
68 for k2 in ['chrome', 'experimental']:
69 apis.extend(
70 [api['name'] for api in self._api_list_data_source[k1][k2]])
71 return apis
72
73 def _GetAPIItems(self, js_file): 59 def _GetAPIItems(self, js_file):
74 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file)) 60 return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)', js_file))
75 61
76 def _GetDataFromManifest(self, path, file_system): 62 def _GetDataFromManifest(self, path, file_system):
77 manifest = file_system.ReadSingle(path + '/manifest.json') 63 manifest = file_system.ReadSingle(path + '/manifest.json')
78 try: 64 try:
79 manifest_json = json.loads(json_comment_eater.Nom(manifest)) 65 manifest_json = json.loads(json_comment_eater.Nom(manifest))
80 except ValueError as e: 66 except ValueError as e:
81 logging.error('Error parsing manifest.json for %s: %s' % (path, e)) 67 logging.error('Error parsing manifest.json for %s: %s' % (path, e))
82 return None 68 return None
(...skipping 18 matching lines...) Expand all
101 except ValueError as e: 87 except ValueError as e:
102 logging.error('Error parsing locales files for %s: %s' % (path, e)) 88 logging.error('Error parsing locales files for %s: %s' % (path, e))
103 else: 89 else:
104 for path, json_ in locales_json: 90 for path, json_ in locales_json:
105 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_ 91 l10n_data['locales'][path[len(locales_path):].split('/')[0]] = json_
106 return l10n_data 92 return l10n_data
107 93
108 def _MakeSamplesList(self, files, is_apps=False): 94 def _MakeSamplesList(self, files, is_apps=False):
109 file_system = self._github_file_system if is_apps else self._file_system 95 file_system = self._github_file_system if is_apps else self._file_system
110 samples_list = [] 96 samples_list = []
111 api_list = self._GetAllAPINames() 97 api_list = self._api_list_data_source.GetAllNames()
112 for filename in sorted(files): 98 for filename in sorted(files):
113 if filename.rsplit('/')[-1] != 'manifest.json': 99 if filename.rsplit('/')[-1] != 'manifest.json':
114 continue 100 continue
115 # This is a little hacky, but it makes a sample page. 101 # This is a little hacky, but it makes a sample page.
116 sample_path = filename.rsplit('/', 1)[-2] 102 sample_path = filename.rsplit('/', 1)[-2]
117 sample_files = [path for path in files 103 sample_files = [path for path in files
118 if path.startswith(sample_path + '/')] 104 if path.startswith(sample_path + '/')]
119 js_files = [path for path in sample_files if path.endswith('.js')] 105 js_files = [path for path in sample_files if path.endswith('.js')]
120 try: 106 try:
121 js_contents = file_system.Read(js_files).Get() 107 js_contents = file_system.Read(js_files).Get()
122 except FileNotFoundError as e: 108 except FileNotFoundError as e:
123 logging.warning('Error fetching samples files: %s. Was a file ' 109 logging.warning('Error fetching samples files: %s. Was a file '
124 'deleted from a sample? This warning should go away ' 110 'deleted from a sample? This warning should go away '
125 'in 5 minutes.' % e) 111 'in 5 minutes.' % e)
126 continue 112 continue
127 api_items = set() 113 api_items = set()
128 for js in js_contents.values(): 114 for js in js_contents.values():
129 api_items.update(self._GetAPIItems(js)) 115 api_items.update(self._GetAPIItems(js))
130 116
131 api_calls = [] 117 api_calls = []
132 for item in api_items: 118 for item in api_items:
133 if len(item.split('.')) < 3: 119 if len(item.split('.')) < 3:
134 continue 120 continue
135 if item.endswith('.removeListener') or item.endswith('.hasListener'): 121 if item.endswith('.removeListener') or item.endswith('.hasListener'):
136 continue 122 continue
137 if item.endswith('.addListener'): 123 if item.endswith('.addListener'):
138 item = item[:-len('.addListener')] 124 item = item[:-len('.addListener')]
139 link = _MakeAPILink('event', item, api_list) 125 if item.startswith('chrome.'):
140 if link is None: 126 item = item[len('chrome.'):]
141 continue 127 ref_data = GetLinkToRefType('samples',
142 api_calls.append({ 128 item,
143 'name': item, 129 self._api_data_source,
144 'link': link 130 api_list)
145 }) 131 if ref_data is None:
146 else: 132 continue
147 # TODO(cduvall): this might be a property or a type. 133 api_calls.append({
148 link = _MakeAPILink('method', item, api_list) 134 'name': ref_data['text'],
149 if link is None: 135 'link': ref_data['href']
150 continue 136 })
151 api_calls.append({
152 'name': item,
153 'link': link
154 })
155 try: 137 try:
156 manifest_data = self._GetDataFromManifest(sample_path, file_system) 138 manifest_data = self._GetDataFromManifest(sample_path, file_system)
157 except FileNotFoundError as e: 139 except FileNotFoundError as e:
158 logging.warning('Error getting data from samples manifest: %s. If ' 140 logging.warning('Error getting data from samples manifest: %s. If '
159 'this file was deleted from a sample this message ' 141 'this file was deleted from a sample this message '
160 'should go away in 5 minutes.' % e) 142 'should go away in 5 minutes.' % e)
161 continue 143 continue
162 if manifest_data is None: 144 if manifest_data is None:
163 continue 145 continue
164 146
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 return return_list 238 return return_list
257 239
258 def __getitem__(self, key): 240 def __getitem__(self, key):
259 return self.get(key) 241 return self.get(key)
260 242
261 def get(self, key): 243 def get(self, key):
262 return { 244 return {
263 'apps': lambda: self._CreateSamplesDict('apps'), 245 'apps': lambda: self._CreateSamplesDict('apps'),
264 'extensions': lambda: self._CreateSamplesDict('extensions') 246 'extensions': lambda: self._CreateSamplesDict('extensions')
265 }.get(key, lambda: {})() 247 }.get(key, lambda: {})()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698