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 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 allows the cache to be invalidated | 17 # the caches used by APIDataSource. This allows the cache to be invalidated |
18 # without having to flush memcache on the production server. | 18 # without having to flush memcache on the production server. |
19 _VERSION = 8 | 19 _VERSION = 9 |
20 | 20 |
21 def _RemoveNoDocs(item): | 21 def _RemoveNoDocs(item): |
22 if json_parse.IsDict(item): | 22 if json_parse.IsDict(item): |
23 if item.get('nodoc', False): | 23 if item.get('nodoc', False): |
24 return True | 24 return True |
25 to_remove = [] | 25 to_remove = [] |
26 for key, value in item.items(): | 26 for key, value in item.items(): |
27 if _RemoveNoDocs(value): | 27 if _RemoveNoDocs(value): |
28 to_remove.append(key) | 28 to_remove.append(key) |
29 for k in to_remove: | 29 for k in to_remove: |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 if self._disable_refs: | 70 if self._disable_refs: |
71 type_name = link.split('.', 1)[-1] | 71 type_name = link.split('.', 1)[-1] |
72 return { 'href': '#type-%s' % type_name, 'text': link, 'name': link } | 72 return { 'href': '#type-%s' % type_name, 'text': link, 'name': link } |
73 return self._ref_resolver.SafeGetLink(link, self._namespace.name) | 73 return self._ref_resolver.SafeGetLink(link, self._namespace.name) |
74 | 74 |
75 def ToDict(self): | 75 def ToDict(self): |
76 if self._namespace is None: | 76 if self._namespace is None: |
77 return {} | 77 return {} |
78 return { | 78 return { |
79 'name': self._namespace.name, | 79 'name': self._namespace.name, |
80 'types': [self._GenerateType(t) for t in self._namespace.types.values() | 80 'types': self._GenerateTypes(self._namespace.types.values()), |
81 if t.type_ != model.PropertyType.ADDITIONAL_PROPERTIES], | |
82 'functions': self._GenerateFunctions(self._namespace.functions), | 81 'functions': self._GenerateFunctions(self._namespace.functions), |
83 'events': self._GenerateEvents(self._namespace.events), | 82 'events': self._GenerateEvents(self._namespace.events), |
84 'properties': self._GenerateProperties(self._namespace.properties) | 83 'properties': self._GenerateProperties(self._namespace.properties) |
85 } | 84 } |
86 | 85 |
| 86 def _GenerateTypes(self, types): |
| 87 return [self._GenerateType(t) for t in types] |
| 88 |
87 def _GenerateType(self, type_): | 89 def _GenerateType(self, type_): |
88 type_dict = { | 90 type_dict = { |
89 'name': type_.simple_name, | 91 'name': type_.simple_name, |
90 'description': self._FormatDescription(type_.description), | 92 'description': self._FormatDescription(type_.description), |
91 'properties': self._GenerateProperties(type_.properties), | 93 'properties': self._GenerateProperties(type_.properties), |
92 'functions': self._GenerateFunctions(type_.functions), | 94 'functions': self._GenerateFunctions(type_.functions), |
93 'events': self._GenerateEvents(type_.events), | 95 'events': self._GenerateEvents(type_.events), |
94 'id': _CreateId(type_, 'type') | 96 'id': _CreateId(type_, 'type') |
95 } | 97 } |
96 self._RenderTypeInformation(type_, type_dict) | 98 self._RenderTypeInformation(type_, type_dict) |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 'optional': callback.optional, | 158 'optional': callback.optional, |
157 'parameters': [] | 159 'parameters': [] |
158 } | 160 } |
159 for param in callback.params: | 161 for param in callback.params: |
160 callback_dict['parameters'].append(self._GenerateProperty(param)) | 162 callback_dict['parameters'].append(self._GenerateProperty(param)) |
161 if (len(callback_dict['parameters']) > 0): | 163 if (len(callback_dict['parameters']) > 0): |
162 callback_dict['parameters'][-1]['last'] = True | 164 callback_dict['parameters'][-1]['last'] = True |
163 return callback_dict | 165 return callback_dict |
164 | 166 |
165 def _GenerateProperties(self, properties): | 167 def _GenerateProperties(self, properties): |
166 return [self._GenerateProperty(v) for v in properties.values() | 168 return [self._GenerateProperty(v) for v in properties.values()] |
167 if v.type_ != model.PropertyType.ADDITIONAL_PROPERTIES] | |
168 | 169 |
169 def _GenerateProperty(self, property_): | 170 def _GenerateProperty(self, property_): |
| 171 type_ = property_.type_ |
170 property_dict = { | 172 property_dict = { |
171 'name': property_.simple_name, | 173 'name': property_.simple_name, |
172 'optional': property_.optional, | 174 'optional': property_.optional, |
173 'description': self._FormatDescription(property_.description), | 175 'description': self._FormatDescription(property_.description), |
174 'properties': self._GenerateProperties(property_.properties), | 176 'properties': self._GenerateProperties(type_.properties), |
175 'functions': self._GenerateFunctions(property_.functions), | 177 'functions': self._GenerateFunctions(type_.functions), |
176 'parameters': [], | 178 'parameters': [], |
177 'returns': None, | 179 'returns': None, |
178 'id': _CreateId(property_, 'property') | 180 'id': _CreateId(property_, 'property') |
179 } | 181 } |
180 for param in property_.params: | 182 if type_.property_type == model.PropertyType.FUNCTION: |
181 property_dict['parameters'].append(self._GenerateProperty(param)) | 183 function = type_.function |
182 if property_.returns: | 184 for param in function.params: |
183 property_dict['returns'] = self._GenerateProperty(property_.returns) | 185 property_dict['parameters'].append(self._GenerateProperty(param)) |
| 186 if function.returns: |
| 187 property_dict['returns'] = self._GenerateProperty(function.returns) |
184 if (property_.parent is not None and | 188 if (property_.parent is not None and |
185 not isinstance(property_.parent, model.Namespace)): | 189 not isinstance(property_.parent, model.Namespace)): |
186 property_dict['parent_name'] = property_.parent.simple_name | 190 property_dict['parent_name'] = property_.parent.simple_name |
187 else: | 191 else: |
188 property_dict['parent_name'] = None | 192 property_dict['parent_name'] = None |
189 if property_.has_value: | 193 value = property_.value |
190 if isinstance(property_.value, int): | 194 if value is not None: |
191 property_dict['value'] = _FormatValue(property_.value) | 195 if isinstance(value, int): |
| 196 property_dict['value'] = _FormatValue(value) |
192 else: | 197 else: |
193 property_dict['value'] = property_.value | 198 property_dict['value'] = value |
194 else: | 199 else: |
195 self._RenderTypeInformation(property_, property_dict) | 200 self._RenderTypeInformation(type_, property_dict) |
196 return property_dict | 201 return property_dict |
197 | 202 |
198 def _RenderTypeInformation(self, property_, dst_dict): | 203 def _RenderTypeInformation(self, type_, dst_dict): |
199 if property_.type_ == model.PropertyType.CHOICES: | 204 if type_.property_type == model.PropertyType.CHOICES: |
200 dst_dict['choices'] = [self._GenerateProperty(c) | 205 dst_dict['choices'] = self._GenerateTypes(type_.choices) |
201 for c in property_.choices.values()] | |
202 # We keep track of which is last for knowing when to add "or" between | 206 # We keep track of which is last for knowing when to add "or" between |
203 # choices in templates. | 207 # choices in templates. |
204 if len(dst_dict['choices']) > 0: | 208 if len(dst_dict['choices']) > 0: |
205 dst_dict['choices'][-1]['last'] = True | 209 dst_dict['choices'][-1]['last'] = True |
206 elif property_.type_ == model.PropertyType.REF: | 210 elif type_.property_type == model.PropertyType.REF: |
207 dst_dict['link'] = self._GetLink(property_.ref_type) | 211 dst_dict['link'] = self._GetLink(type_.ref_type) |
208 elif property_.type_ == model.PropertyType.ARRAY: | 212 elif type_.property_type == model.PropertyType.ARRAY: |
209 dst_dict['array'] = self._GenerateProperty(property_.item_type) | 213 dst_dict['array'] = self._GenerateType(type_.item_type) |
210 elif property_.type_ == model.PropertyType.ENUM: | 214 elif type_.property_type == model.PropertyType.ENUM: |
211 dst_dict['enum_values'] = [] | 215 dst_dict['enum_values'] = [] |
212 for enum_value in property_.enum_values: | 216 for enum_value in type_.enum_values: |
213 dst_dict['enum_values'].append({'name': enum_value}) | 217 dst_dict['enum_values'].append({'name': enum_value}) |
214 if len(dst_dict['enum_values']) > 0: | 218 if len(dst_dict['enum_values']) > 0: |
215 dst_dict['enum_values'][-1]['last'] = True | 219 dst_dict['enum_values'][-1]['last'] = True |
216 elif property_.instance_of is not None: | 220 elif type_.instance_of is not None: |
217 dst_dict['simple_type'] = property_.instance_of.lower() | 221 dst_dict['simple_type'] = type_.instance_of.lower() |
218 else: | 222 else: |
219 dst_dict['simple_type'] = property_.type_.name.lower() | 223 dst_dict['simple_type'] = type_.property_type.name |
220 | 224 |
221 class _LazySamplesGetter(object): | 225 class _LazySamplesGetter(object): |
222 """This class is needed so that an extensions API page does not have to fetch | 226 """This class is needed so that an extensions API page does not have to fetch |
223 the apps samples page and vice versa. | 227 the apps samples page and vice versa. |
224 """ | 228 """ |
225 def __init__(self, api_name, samples): | 229 def __init__(self, api_name, samples): |
226 self._api_name = api_name | 230 self._api_name = api_name |
227 self._samples = samples | 231 self._samples = samples |
228 | 232 |
229 def get(self, key): | 233 def get(self, key): |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 if self._disable_refs: | 424 if self._disable_refs: |
421 cache, ext = ( | 425 cache, ext = ( |
422 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else | 426 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else |
423 (self._json_cache_no_refs, '.json')) | 427 (self._json_cache_no_refs, '.json')) |
424 else: | 428 else: |
425 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else | 429 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else |
426 (self._json_cache, '.json')) | 430 (self._json_cache, '.json')) |
427 return self._GenerateHandlebarContext( | 431 return self._GenerateHandlebarContext( |
428 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), | 432 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), |
429 path) | 433 path) |
OLD | NEW |