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

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

Issue 11827026: Overhaul JSON Schema Compiler to support a number of features required to (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 11 months 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 copy 5 import copy
6 import logging 6 import logging
7 import os 7 import os
8 8
9 import compiled_file_system as compiled_fs 9 import compiled_file_system as compiled_fs
10 from file_system import FileNotFoundError 10 from file_system import FileNotFoundError
11 import third_party.json_schema_compiler.json_parse as json_parse 11 import third_party.json_schema_compiler.json_parse as json_parse
12 import third_party.json_schema_compiler.model as model 12 import third_party.json_schema_compiler.model as model
13 import third_party.json_schema_compiler.idl_schema as idl_schema 13 import third_party.json_schema_compiler.idl_schema as idl_schema
14 import third_party.json_schema_compiler.idl_parser as idl_parser 14 import third_party.json_schema_compiler.idl_parser as idl_parser
15 15
16 # Increment this version when there are changes to the data stored in any of 16 # Increment this version when there are changes to the data stored in any of
17 # the caches used by APIDataSource. This would include changes to model.py in 17 # the caches used by APIDataSource. This would include changes to model.py in
18 # JSON schema compiler! This allows the cache to be invalidated without having 18 # JSON schema compiler! This allows the cache to be invalidated without having
19 # to flush memcache on the production server. 19 # to flush memcache on the production server.
20 _VERSION = 12 20 _VERSION = 13
21 21
22 def _RemoveNoDocs(item): 22 def _RemoveNoDocs(item):
23 if json_parse.IsDict(item): 23 if json_parse.IsDict(item):
24 if item.get('nodoc', False): 24 if item.get('nodoc', False):
25 return True 25 return True
26 to_remove = [] 26 to_remove = []
27 for key, value in item.items(): 27 for key, value in item.items():
28 if _RemoveNoDocs(value): 28 if _RemoveNoDocs(value):
29 to_remove.append(key) 29 to_remove.append(key)
30 for k in to_remove: 30 for k in to_remove:
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 if self._disable_refs: 72 if self._disable_refs:
73 type_name = link.split('.', 1)[-1] 73 type_name = link.split('.', 1)[-1]
74 return { 'href': '#type-%s' % type_name, 'text': link, 'name': link } 74 return { 'href': '#type-%s' % type_name, 'text': link, 'name': link }
75 return self._ref_resolver.SafeGetLink(link, namespace=self._namespace.name) 75 return self._ref_resolver.SafeGetLink(link, namespace=self._namespace.name)
76 76
77 def ToDict(self): 77 def ToDict(self):
78 if self._namespace is None: 78 if self._namespace is None:
79 return {} 79 return {}
80 return { 80 return {
81 'name': self._namespace.name, 81 'name': self._namespace.name,
82 'types': [self._GenerateType(t) for t in self._namespace.types.values() 82 'types': self._GenerateTypes(self._namespace.types.values()),
83 if t.type_ != model.PropertyType.ADDITIONAL_PROPERTIES],
84 'functions': self._GenerateFunctions(self._namespace.functions), 83 'functions': self._GenerateFunctions(self._namespace.functions),
85 'events': self._GenerateEvents(self._namespace.events), 84 'events': self._GenerateEvents(self._namespace.events),
86 'properties': self._GenerateProperties(self._namespace.properties) 85 'properties': self._GenerateProperties(self._namespace.properties)
87 } 86 }
88 87
88 def _GenerateTypes(self, types):
89 return [self._GenerateType(t) for t in types]
90
89 def _GenerateType(self, type_): 91 def _GenerateType(self, type_):
90 type_dict = { 92 type_dict = {
91 'name': type_.simple_name, 93 'name': type_.simple_name,
92 'description': self._FormatDescription(type_.description), 94 'description': self._FormatDescription(type_.description),
93 'properties': self._GenerateProperties(type_.properties), 95 'properties': self._GenerateProperties(type_.properties),
94 'functions': self._GenerateFunctions(type_.functions), 96 'functions': self._GenerateFunctions(type_.functions),
95 'events': self._GenerateEvents(type_.events), 97 'events': self._GenerateEvents(type_.events),
96 'id': _CreateId(type_, 'type') 98 'id': _CreateId(type_, 'type')
97 } 99 }
98 self._RenderTypeInformation(type_, type_dict) 100 self._RenderTypeInformation(type_, type_dict)
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 'optional': callback.optional, 163 'optional': callback.optional,
162 'parameters': [] 164 'parameters': []
163 } 165 }
164 for param in callback.params: 166 for param in callback.params:
165 callback_dict['parameters'].append(self._GenerateProperty(param)) 167 callback_dict['parameters'].append(self._GenerateProperty(param))
166 if (len(callback_dict['parameters']) > 0): 168 if (len(callback_dict['parameters']) > 0):
167 callback_dict['parameters'][-1]['last'] = True 169 callback_dict['parameters'][-1]['last'] = True
168 return callback_dict 170 return callback_dict
169 171
170 def _GenerateProperties(self, properties): 172 def _GenerateProperties(self, properties):
171 return [self._GenerateProperty(v) for v in properties.values() 173 return [self._GenerateProperty(v) for v in properties.values()]
172 if v.type_ != model.PropertyType.ADDITIONAL_PROPERTIES]
173 174
174 def _GenerateProperty(self, property_): 175 def _GenerateProperty(self, property_):
176 type_ = property_.type_
177
175 # Make sure we generate property info for arrays, too. 178 # Make sure we generate property info for arrays, too.
176 # TODO(kalman): what about choices? 179 # TODO(kalman): what about choices?
177 if property_.type_ == model.PropertyType.ARRAY: 180 if type_.property_type == model.PropertyType.ARRAY:
178 properties = property_.item_type.properties 181 properties = type_.item_type.properties
179 else: 182 else:
180 properties = property_.properties 183 properties = type_.properties
181 184
182 property_dict = { 185 property_dict = {
183 'name': property_.simple_name, 186 'name': property_.simple_name,
184 'optional': property_.optional, 187 'optional': property_.optional,
185 'description': self._FormatDescription(property_.description), 188 'description': self._FormatDescription(property_.description),
186 'properties': self._GenerateProperties(properties), 189 'properties': self._GenerateProperties(type_.properties),
187 'functions': self._GenerateFunctions(property_.functions), 190 'functions': self._GenerateFunctions(type_.functions),
188 'parameters': [], 191 'parameters': [],
189 'returns': None, 192 'returns': None,
190 'id': _CreateId(property_, 'property') 193 'id': _CreateId(property_, 'property')
191 } 194 }
192 for param in property_.params: 195
193 property_dict['parameters'].append(self._GenerateProperty(param)) 196 if type_.property_type == model.PropertyType.FUNCTION:
194 if property_.returns: 197 function = type_.function
195 property_dict['returns'] = self._GenerateProperty(property_.returns) 198 for param in function.params:
199 property_dict['parameters'].append(self._GenerateProperty(param))
200 if function.returns:
201 property_dict['returns'] = self._GenerateProperty(function.returns)
202
196 if (property_.parent is not None and 203 if (property_.parent is not None and
197 not isinstance(property_.parent, model.Namespace)): 204 not isinstance(property_.parent, model.Namespace)):
198 property_dict['parent_name'] = property_.parent.simple_name 205 property_dict['parent_name'] = property_.parent.simple_name
199 if property_.has_value: 206
200 if isinstance(property_.value, int): 207 value = property_.value
201 property_dict['value'] = _FormatValue(property_.value) 208 if value is not None:
209 if isinstance(value, int):
210 property_dict['value'] = _FormatValue(value)
202 else: 211 else:
203 property_dict['value'] = property_.value 212 property_dict['value'] = value
204 else: 213 else:
205 self._RenderTypeInformation(property_, property_dict) 214 self._RenderTypeInformation(type_, property_dict)
215
206 return property_dict 216 return property_dict
207 217
208 def _GenerateCallbackProperty(self, callback): 218 def _GenerateCallbackProperty(self, callback):
209 property_dict = { 219 property_dict = {
210 'name': callback.simple_name, 220 'name': callback.simple_name,
211 'description': self._FormatDescription(callback.description), 221 'description': self._FormatDescription(callback.description),
212 'optional': callback.optional, 222 'optional': callback.optional,
213 'id': _CreateId(callback, 'property'), 223 'id': _CreateId(callback, 'property'),
214 'simple_type': 'function', 224 'simple_type': 'function',
215 } 225 }
216 if (callback.parent is not None and 226 if (callback.parent is not None and
217 not isinstance(callback.parent, model.Namespace)): 227 not isinstance(callback.parent, model.Namespace)):
218 property_dict['parent_name'] = callback.parent.simple_name 228 property_dict['parent_name'] = callback.parent.simple_name
219 return property_dict 229 return property_dict
220 230
221 def _RenderTypeInformation(self, property_, dst_dict): 231 def _RenderTypeInformation(self, type_, dst_dict):
222 if property_.type_ == model.PropertyType.CHOICES: 232 if type_.property_type == model.PropertyType.CHOICES:
223 dst_dict['choices'] = [self._GenerateProperty(c) 233 dst_dict['choices'] = self._GenerateTypes(type_.choices)
224 for c in property_.choices.values()]
225 # We keep track of which is last for knowing when to add "or" between 234 # We keep track of which is last for knowing when to add "or" between
226 # choices in templates. 235 # choices in templates.
227 if len(dst_dict['choices']) > 0: 236 if len(dst_dict['choices']) > 0:
228 dst_dict['choices'][-1]['last'] = True 237 dst_dict['choices'][-1]['last'] = True
229 elif property_.type_ == model.PropertyType.REF: 238 elif type_.property_type == model.PropertyType.REF:
230 dst_dict['link'] = self._GetLink(property_.ref_type) 239 dst_dict['link'] = self._GetLink(type_.ref_type)
231 elif property_.type_ == model.PropertyType.ARRAY: 240 elif type_.property_type == model.PropertyType.ARRAY:
232 dst_dict['array'] = self._GenerateProperty(property_.item_type) 241 dst_dict['array'] = self._GenerateType(type_.item_type)
233 elif property_.type_ == model.PropertyType.ENUM: 242 elif type_.property_type == model.PropertyType.ENUM:
234 dst_dict['enum_values'] = [] 243 dst_dict['enum_values'] = []
235 for enum_value in property_.enum_values: 244 for enum_value in type_.enum_values:
236 dst_dict['enum_values'].append({'name': enum_value}) 245 dst_dict['enum_values'].append({'name': enum_value})
237 if len(dst_dict['enum_values']) > 0: 246 if len(dst_dict['enum_values']) > 0:
238 dst_dict['enum_values'][-1]['last'] = True 247 dst_dict['enum_values'][-1]['last'] = True
239 elif property_.instance_of is not None: 248 elif type_.instance_of is not None:
240 dst_dict['simple_type'] = property_.instance_of.lower() 249 dst_dict['simple_type'] = type_.instance_of.lower()
241 else: 250 else:
242 dst_dict['simple_type'] = property_.type_.name.lower() 251 dst_dict['simple_type'] = type_.property_type.name.lower()
243 252
244 class _LazySamplesGetter(object): 253 class _LazySamplesGetter(object):
245 """This class is needed so that an extensions API page does not have to fetch 254 """This class is needed so that an extensions API page does not have to fetch
246 the apps samples page and vice versa. 255 the apps samples page and vice versa.
247 """ 256 """
248 def __init__(self, api_name, samples): 257 def __init__(self, api_name, samples):
249 self._api_name = api_name 258 self._api_name = api_name
250 self._samples = samples 259 self._samples = samples
251 260
252 def get(self, key): 261 def get(self, key):
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 if self._disable_refs: 452 if self._disable_refs:
444 cache, ext = ( 453 cache, ext = (
445 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else 454 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else
446 (self._json_cache_no_refs, '.json')) 455 (self._json_cache_no_refs, '.json'))
447 else: 456 else:
448 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else 457 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else
449 (self._json_cache, '.json')) 458 (self._json_cache, '.json'))
450 return self._GenerateHandlebarContext( 459 return self._GenerateHandlebarContext(
451 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), 460 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)),
452 path) 461 path)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698