Chromium Code Reviews| 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): |
| 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, full_inline=False): |
|
not at google - send to devlin
2014/07/15 21:12:04
can you make this actually be explicit? "full_inli
not at google - send to devlin
2014/07/15 22:43:20
I would prefer retain_inlined_nodes actually (reve
ahernandez
2014/07/15 22:49:51
I believe this line: https://code.google.com/p/chr
| |
| 93 '''Parses |file_data| using a method determined by checking the extension of | 96 '''Parses |file_data| using a method determined by checking the extension of |
| 94 the file at the given |path|. Then, trims 'nodoc' and if |inline| is given | 97 the file at the given |path|. Then, trims 'nodoc' and if |full_inline| is |
| 95 and True, handles inlineable types from the parsed schema data. | 98 given and True, removes inlineable types from the parsed schema data. |
| 96 ''' | 99 ''' |
| 97 def trim_and_inline(schema, is_idl=False): | 100 def trim_and_inline(schema, is_idl=False): |
| 98 '''Modifies an API schema in place by removing nodes that shouldn't be | 101 '''Modifies an API schema in place by removing nodes that shouldn't be |
| 99 documented and inlining schema types that are only referenced once. | 102 documented and inlining schema types that are only referenced once. |
| 100 ''' | 103 ''' |
| 101 if RemoveNoDocs(schema): | 104 if RemoveNoDocs(schema): |
| 102 # A return of True signifies that the entire schema should not be | 105 # A return of True signifies that the entire schema should not be |
| 103 # documented. Otherwise, only nodes that request 'nodoc' are removed. | 106 # documented. Otherwise, only nodes that request 'nodoc' are removed. |
| 104 return None | 107 return None |
| 105 if inline: | 108 if is_idl: |
| 106 if is_idl: | 109 DetectInlineableTypes(schema) |
| 107 DetectInlineableTypes(schema) | 110 InlineDocs(schema, full_inline) |
| 108 InlineDocs(schema) | |
| 109 return schema | 111 return schema |
| 110 | 112 |
| 111 if path.endswith('.idl'): | 113 if path.endswith('.idl'): |
| 112 idl = idl_schema.IDLSchema(idl_parser.IDLParser().ParseData(file_data)) | 114 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. | 115 # 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)] | 116 return [trim_and_inline(idl.process()[0], is_idl=True)] |
| 115 | 117 |
| 116 try: | 118 try: |
| 117 schemas = json_parse.Parse(file_data) | 119 schemas = json_parse.Parse(file_data) |
| 118 except: | 120 except: |
| 119 raise ValueError('Cannot parse "%s" as JSON:\n%s' % | 121 raise ValueError('Cannot parse "%s" as JSON:\n%s' % |
| 120 (path, traceback.format_exc())) | 122 (path, traceback.format_exc())) |
| 121 for schema in schemas: | 123 for schema in schemas: |
| 122 # Schemas could consist of one API schema (data for a specific API file) | 124 # Schemas could consist of one API schema (data for a specific API file) |
| 123 # or multiple (data from extension_api.json). | 125 # or multiple (data from extension_api.json). |
| 124 trim_and_inline(schema) | 126 trim_and_inline(schema) |
| 125 return schemas | 127 return schemas |
| OLD | NEW |