OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 from collections import defaultdict, Mapping | 5 from collections import defaultdict, Mapping |
6 import traceback | 6 import traceback |
7 | 7 |
8 from third_party.json_schema_compiler import json_parse, idl_schema, idl_parser | 8 from third_party.json_schema_compiler import json_parse, idl_schema, idl_parser |
9 | 9 |
10 | 10 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 if '$ref' in node: | 47 if '$ref' in node: |
48 refcounts[node['$ref']] += 1 | 48 refcounts[node['$ref']] += 1 |
49 stack.extend(v for k, v in node.iteritems() if k not in ignore) | 49 stack.extend(v for k, v in node.iteritems() if k not in ignore) |
50 | 50 |
51 for type_ in schema['types']: | 51 for type_ in schema['types']: |
52 if not 'noinline_doc' in type_: | 52 if not 'noinline_doc' in type_: |
53 if refcounts[type_['id']] == 1: | 53 if refcounts[type_['id']] == 1: |
54 type_['inline_doc'] = True | 54 type_['inline_doc'] = True |
55 | 55 |
56 | 56 |
57 def InlineDocs(schema): | 57 def InlineDocs(schema, remove_nodes): |
not at google - send to devlin
2014/07/15 22:43:20
please update this variable as well
| |
58 '''Replace '$ref's that refer to inline_docs with the json for those docs. | 58 '''Replace '$ref's that refer to inline_docs with the json for those docs. |
59 If |remove_nodes| is true, then the inlined nodes are removed from the schema. | |
59 ''' | 60 ''' |
60 types = schema.get('types') | 61 types = schema.get('types') |
61 if types is None: | 62 if types is None: |
62 return | 63 return |
63 | 64 |
64 inline_docs = {} | 65 inline_docs = {} |
65 types_without_inline_doc = [] | 66 types_without_inline_doc = [] |
66 | 67 |
67 # Gather the types with inline_doc. | 68 # Gather the types with inline_doc. |
68 for type_ in types: | 69 for type_ in types: |
69 if type_.get('inline_doc'): | 70 if type_.get('inline_doc'): |
70 inline_docs[type_['id']] = type_ | 71 inline_docs[type_['id']] = type_ |
71 for k in ('description', 'id', 'inline_doc'): | 72 if remove_nodes: |
72 type_.pop(k, None) | 73 for k in ('description', 'id', 'inline_doc'): |
74 type_.pop(k, None) | |
73 else: | 75 else: |
74 types_without_inline_doc.append(type_) | 76 types_without_inline_doc.append(type_) |
75 schema['types'] = types_without_inline_doc | 77 if remove_nodes: |
78 schema['types'] = types_without_inline_doc | |
76 | 79 |
77 def apply_inline(node): | 80 def apply_inline(node): |
78 if isinstance(node, list): | 81 if isinstance(node, list): |
79 for i in node: | 82 for i in node: |
80 apply_inline(i) | 83 apply_inline(i) |
81 elif isinstance(node, Mapping): | 84 elif isinstance(node, Mapping): |
82 ref = node.get('$ref') | 85 ref = node.get('$ref') |
83 if ref and ref in inline_docs: | 86 if ref and ref in inline_docs: |
84 node.update(inline_docs[ref]) | 87 node.update(inline_docs[ref]) |
85 del node['$ref'] | 88 del node['$ref'] |
86 for k, v in node.iteritems(): | 89 for k, v in node.iteritems(): |
87 apply_inline(v) | 90 apply_inline(v) |
88 | 91 |
89 apply_inline(schema) | 92 apply_inline(schema) |
90 | 93 |
91 | 94 |
92 def ProcessSchema(path, file_data, inline=False): | 95 def ProcessSchema(path, file_data, remove_inlined_nodes=False): |
93 '''Parses |file_data| using a method determined by checking the extension of | 96 '''Parses |file_data| using a method determined by checking the |
94 the file at the given |path|. Then, trims 'nodoc' and if |inline| is given | 97 extension of the file at the given |path|. Then, trims 'nodoc' and if |
95 and True, handles inlineable types from the parsed schema data. | 98 |remove_inlined_nodes| is given and True, removes inlineable types from |
99 the parsed schema data. | |
96 ''' | 100 ''' |
97 def trim_and_inline(schema, is_idl=False): | 101 def trim_and_inline(schema, is_idl=False): |
98 '''Modifies an API schema in place by removing nodes that shouldn't be | 102 '''Modifies an API schema in place by removing nodes that shouldn't be |
99 documented and inlining schema types that are only referenced once. | 103 documented and inlining schema types that are only referenced once. |
100 ''' | 104 ''' |
101 if RemoveNoDocs(schema): | 105 if RemoveNoDocs(schema): |
102 # A return of True signifies that the entire schema should not be | 106 # A return of True signifies that the entire schema should not be |
103 # documented. Otherwise, only nodes that request 'nodoc' are removed. | 107 # documented. Otherwise, only nodes that request 'nodoc' are removed. |
104 return None | 108 return None |
105 if inline: | 109 if is_idl: |
106 if is_idl: | 110 DetectInlineableTypes(schema) |
107 DetectInlineableTypes(schema) | 111 InlineDocs(schema, remove_inlined_nodes) |
108 InlineDocs(schema) | |
109 return schema | 112 return schema |
110 | 113 |
111 if path.endswith('.idl'): | 114 if path.endswith('.idl'): |
112 idl = idl_schema.IDLSchema(idl_parser.IDLParser().ParseData(file_data)) | 115 idl = idl_schema.IDLSchema(idl_parser.IDLParser().ParseData(file_data)) |
113 # Wrap the result in a list so that it behaves like JSON API data. | 116 # Wrap the result in a list so that it behaves like JSON API data. |
114 return [trim_and_inline(idl.process()[0], is_idl=True)] | 117 return [trim_and_inline(idl.process()[0], is_idl=True)] |
115 | 118 |
116 try: | 119 try: |
117 schemas = json_parse.Parse(file_data) | 120 schemas = json_parse.Parse(file_data) |
118 except: | 121 except: |
119 raise ValueError('Cannot parse "%s" as JSON:\n%s' % | 122 raise ValueError('Cannot parse "%s" as JSON:\n%s' % |
120 (path, traceback.format_exc())) | 123 (path, traceback.format_exc())) |
121 for schema in schemas: | 124 for schema in schemas: |
122 # Schemas could consist of one API schema (data for a specific API file) | 125 # Schemas could consist of one API schema (data for a specific API file) |
123 # or multiple (data from extension_api.json). | 126 # or multiple (data from extension_api.json). |
124 trim_and_inline(schema) | 127 trim_and_inline(schema) |
125 return schemas | 128 return schemas |
OLD | NEW |