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, retain_inlined_types): |
| 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 |retain_inlined_types| is False, then the inlined nodes are removed | |
| 60 from the schema. | |
| 59 ''' | 61 ''' |
| 60 types = schema.get('types') | 62 types = schema.get('types') |
| 61 if types is None: | 63 if types is None: |
| 62 return | 64 return |
| 63 | 65 |
| 64 inline_docs = {} | 66 inline_docs = {} |
| 65 types_without_inline_doc = [] | 67 types_without_inline_doc = [] |
| 66 | 68 |
| 67 # Gather the types with inline_doc. | 69 # Gather the types with inline_doc. |
| 68 for type_ in types: | 70 for type_ in types: |
| 69 if type_.get('inline_doc'): | 71 if type_.get('inline_doc'): |
| 70 inline_docs[type_['id']] = type_ | 72 inline_docs[type_['id']] = type_ |
| 71 for k in ('description', 'id', 'inline_doc'): | 73 if not retain_inlined_types: |
| 72 type_.pop(k, None) | 74 for k in ('description', 'id', 'inline_doc'): |
| 75 type_.pop(k, None) | |
| 73 else: | 76 else: |
| 74 types_without_inline_doc.append(type_) | 77 types_without_inline_doc.append(type_) |
| 75 schema['types'] = types_without_inline_doc | 78 if not retain_inlined_types: |
| 79 schema['types'] = types_without_inline_doc | |
| 76 | 80 |
| 77 def apply_inline(node): | 81 def apply_inline(node): |
| 78 if isinstance(node, list): | 82 if isinstance(node, list): |
| 79 for i in node: | 83 for i in node: |
| 80 apply_inline(i) | 84 apply_inline(i) |
| 81 elif isinstance(node, Mapping): | 85 elif isinstance(node, Mapping): |
| 82 ref = node.get('$ref') | 86 ref = node.get('$ref') |
| 83 if ref and ref in inline_docs: | 87 if ref and ref in inline_docs: |
| 84 node.update(inline_docs[ref]) | 88 node.update(inline_docs[ref]) |
| 85 del node['$ref'] | 89 del node['$ref'] |
| 86 for k, v in node.iteritems(): | 90 for k, v in node.iteritems(): |
| 87 apply_inline(v) | 91 apply_inline(v) |
| 88 | 92 |
| 89 apply_inline(schema) | 93 apply_inline(schema) |
| 90 | 94 |
| 91 | 95 |
| 92 def ProcessSchema(path, file_data, inline=False): | 96 def ProcessSchema(path, file_data, retain_inlined_types=True): |
|
not at google - send to devlin
2014/07/16 16:17:15
Can this be default-False?
It looks like you'd ju
| |
| 93 '''Parses |file_data| using a method determined by checking the extension of | 97 '''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 | 98 extension of the file at the given |path|. Then, trims 'nodoc' and if |
| 95 and True, handles inlineable types from the parsed schema data. | 99 |retain_inlined_types| is given and False, removes inlineable types from |
| 100 the parsed schema data. | |
| 96 ''' | 101 ''' |
| 97 def trim_and_inline(schema, is_idl=False): | 102 def trim_and_inline(schema, is_idl=False): |
| 98 '''Modifies an API schema in place by removing nodes that shouldn't be | 103 '''Modifies an API schema in place by removing nodes that shouldn't be |
| 99 documented and inlining schema types that are only referenced once. | 104 documented and inlining schema types that are only referenced once. |
| 100 ''' | 105 ''' |
| 101 if RemoveNoDocs(schema): | 106 if RemoveNoDocs(schema): |
| 102 # A return of True signifies that the entire schema should not be | 107 # A return of True signifies that the entire schema should not be |
| 103 # documented. Otherwise, only nodes that request 'nodoc' are removed. | 108 # documented. Otherwise, only nodes that request 'nodoc' are removed. |
| 104 return None | 109 return None |
| 105 if inline: | 110 if is_idl: |
| 106 if is_idl: | 111 DetectInlineableTypes(schema) |
| 107 DetectInlineableTypes(schema) | 112 InlineDocs(schema, retain_inlined_types) |
| 108 InlineDocs(schema) | |
| 109 return schema | 113 return schema |
| 110 | 114 |
| 111 if path.endswith('.idl'): | 115 if path.endswith('.idl'): |
| 112 idl = idl_schema.IDLSchema(idl_parser.IDLParser().ParseData(file_data)) | 116 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. | 117 # 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)] | 118 return [trim_and_inline(idl.process()[0], is_idl=True)] |
| 115 | 119 |
| 116 try: | 120 try: |
| 117 schemas = json_parse.Parse(file_data) | 121 schemas = json_parse.Parse(file_data) |
| 118 except: | 122 except: |
| 119 raise ValueError('Cannot parse "%s" as JSON:\n%s' % | 123 raise ValueError('Cannot parse "%s" as JSON:\n%s' % |
| 120 (path, traceback.format_exc())) | 124 (path, traceback.format_exc())) |
| 121 for schema in schemas: | 125 for schema in schemas: |
| 122 # Schemas could consist of one API schema (data for a specific API file) | 126 # Schemas could consist of one API schema (data for a specific API file) |
| 123 # or multiple (data from extension_api.json). | 127 # or multiple (data from extension_api.json). |
| 124 trim_and_inline(schema) | 128 trim_and_inline(schema) |
| 125 return schemas | 129 return schemas |
| OLD | NEW |