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 import collections |
8 | 9 |
9 import third_party.json_schema_compiler.json_parse as json_parse | 10 import third_party.json_schema_compiler.json_parse as json_parse |
10 import third_party.json_schema_compiler.model as model | 11 import third_party.json_schema_compiler.model as model |
11 import third_party.json_schema_compiler.idl_schema as idl_schema | 12 import third_party.json_schema_compiler.idl_schema as idl_schema |
12 import third_party.json_schema_compiler.idl_parser as idl_parser | 13 import third_party.json_schema_compiler.idl_parser as idl_parser |
13 | 14 |
14 def _RemoveNoDocs(item): | 15 def _RemoveNoDocs(item): |
15 if json_parse.IsDict(item): | 16 if json_parse.IsDict(item): |
16 if item.get('nodoc', False): | 17 if item.get('nodoc', False): |
17 return True | 18 return True |
18 for key, value in item.items(): | 19 for key, value in item.items(): |
19 if _RemoveNoDocs(value): | 20 if _RemoveNoDocs(value): |
20 del item[key] | 21 del item[key] |
21 elif type(item) == list: | 22 elif type(item) == list: |
22 to_remove = [] | 23 to_remove = [] |
23 for i in item: | 24 for i in item: |
24 if _RemoveNoDocs(i): | 25 if _RemoveNoDocs(i): |
25 to_remove.append(i) | 26 to_remove.append(i) |
26 for i in to_remove: | 27 for i in to_remove: |
27 item.remove(i) | 28 item.remove(i) |
28 return False | 29 return False |
29 | 30 |
| 31 |
| 32 def _InlineDocs(schema): |
| 33 """Replace '$ref's that refer to inline_docs with the json for those docs. |
| 34 """ |
| 35 types = schema.get('types') |
| 36 if types is None: |
| 37 return |
| 38 |
| 39 inline_docs = {} |
| 40 types_without_inline_doc = [] |
| 41 |
| 42 # Gather the types with inline_doc. |
| 43 for type_ in types: |
| 44 if type_.get('inline_doc'): |
| 45 inline_docs[type_['id']] = type_ |
| 46 if type_.get('description'): |
| 47 del type_['description'] |
| 48 del type_['inline_doc'] |
| 49 del type_['id'] |
| 50 else: |
| 51 types_without_inline_doc.append(type_) |
| 52 schema['types'] = types_without_inline_doc |
| 53 |
| 54 def apply_inline(node): |
| 55 if isinstance(node, list): |
| 56 for i in node: |
| 57 apply_inline(i) |
| 58 elif isinstance(node, collections.Mapping): |
| 59 ref = node.get('$ref') |
| 60 if ref and ref in inline_docs: |
| 61 node.update(inline_docs[ref]) |
| 62 del node['$ref'] |
| 63 for k, v in node.iteritems(): |
| 64 apply_inline(v) |
| 65 |
| 66 apply_inline(schema) |
| 67 |
30 def _CreateId(node, prefix): | 68 def _CreateId(node, prefix): |
31 if node.parent is not None and not isinstance(node.parent, model.Namespace): | 69 if node.parent is not None and not isinstance(node.parent, model.Namespace): |
32 return '-'.join([prefix, node.parent.simple_name, node.simple_name]) | 70 return '-'.join([prefix, node.parent.simple_name, node.simple_name]) |
33 return '-'.join([prefix, node.simple_name]) | 71 return '-'.join([prefix, node.simple_name]) |
34 | 72 |
35 def _FormatValue(value): | 73 def _FormatValue(value): |
36 """Inserts commas every three digits for integer values. It is magic. | 74 """Inserts commas every three digits for integer values. It is magic. |
37 """ | 75 """ |
38 s = str(value) | 76 s = str(value) |
39 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 77 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
40 | 78 |
41 class _JSCModel(object): | 79 class _JSCModel(object): |
42 """Uses a Model from the JSON Schema Compiler and generates a dict that | 80 """Uses a Model from the JSON Schema Compiler and generates a dict that |
43 a Handlebar template can use for a data source. | 81 a Handlebar template can use for a data source. |
44 """ | 82 """ |
45 def __init__(self, json, ref_resolver, disable_refs): | 83 def __init__(self, json, ref_resolver, disable_refs): |
46 self._ref_resolver = ref_resolver | 84 self._ref_resolver = ref_resolver |
47 self._disable_refs = disable_refs | 85 self._disable_refs = disable_refs |
48 clean_json = copy.deepcopy(json) | 86 clean_json = copy.deepcopy(json) |
49 if _RemoveNoDocs(clean_json): | 87 if _RemoveNoDocs(clean_json): |
50 self._namespace = None | 88 self._namespace = None |
51 else: | 89 else: |
| 90 _InlineDocs(clean_json) |
52 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 91 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
53 | 92 |
54 def _FormatDescription(self, description): | 93 def _FormatDescription(self, description): |
55 if self._disable_refs: | 94 if self._disable_refs: |
56 return description | 95 return description |
57 return self._ref_resolver.ResolveAllLinks(description, | 96 return self._ref_resolver.ResolveAllLinks(description, |
58 namespace=self._namespace.name) | 97 namespace=self._namespace.name) |
59 | 98 |
60 def _GetLink(self, link): | 99 def _GetLink(self, link): |
61 if self._disable_refs: | 100 if self._disable_refs: |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 if self._disable_refs: | 475 if self._disable_refs: |
437 cache, ext = ( | 476 cache, ext = ( |
438 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else | 477 (self._idl_cache_no_refs, '.idl') if (unix_name in idl_names) else |
439 (self._json_cache_no_refs, '.json')) | 478 (self._json_cache_no_refs, '.json')) |
440 else: | 479 else: |
441 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else | 480 cache, ext = ((self._idl_cache, '.idl') if (unix_name in idl_names) else |
442 (self._json_cache, '.json')) | 481 (self._json_cache, '.json')) |
443 return self._GenerateHandlebarContext( | 482 return self._GenerateHandlebarContext( |
444 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), | 483 cache.GetFromFile('%s/%s%s' % (self._base_path, unix_name, ext)), |
445 path) | 484 path) |
OLD | NEW |