Chromium Code Reviews| 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 os.path | 5 import os.path |
| 6 | 6 |
| 7 from json_parse import OrderedDict | 7 from json_parse import OrderedDict |
| 8 from memoize import memoize | 8 from memoize import memoize |
| 9 | 9 |
| 10 | 10 |
| 11 class ParseException(Exception): | 11 class ParseException(Exception): |
| 12 """Thrown when data in the model is invalid. | 12 """Thrown when data in the model is invalid. |
| 13 """ | 13 """ |
| 14 def __init__(self, parent, message): | 14 def __init__(self, parent, message): |
| 15 hierarchy = _GetModelHierarchy(parent) | 15 hierarchy = _GetModelHierarchy(parent) |
| 16 hierarchy.append(message) | 16 hierarchy.append(message) |
| 17 Exception.__init__( | 17 Exception.__init__( |
| 18 self, 'Model parse exception at:\n' + '\n'.join(hierarchy)) | 18 self, 'Model parse exception at:\n' + '\n'.join(hierarchy)) |
| 19 | 19 |
| 20 | 20 |
| 21 class Model(object): | 21 class Model(object): |
| 22 """Model of all namespaces that comprise an API. | 22 """Model of all namespaces that comprise an API. |
| 23 | 23 |
| 24 Properties: | 24 Properties: |
| 25 - |namespaces| a map of a namespace name to its model.Namespace | 25 - |namespaces| a map of a namespace name to its model.Namespace |
| 26 """ | 26 """ |
| 27 def __init__(self): | 27 def __init__(self, allow_inline_enums=True): |
| 28 self._allow_inline_enums = allow_inline_enums | |
| 28 self.namespaces = {} | 29 self.namespaces = {} |
| 29 | 30 |
| 30 def AddNamespace(self, | 31 def AddNamespace(self, |
| 31 json, | 32 json, |
| 32 source_file, | 33 source_file, |
| 33 include_compiler_options=False, | 34 include_compiler_options=False, |
| 34 environment=None): | 35 environment=None): |
| 35 """Add a namespace's json to the model and returns the namespace. | 36 """Add a namespace's json to the model and returns the namespace. |
| 36 """ | 37 """ |
| 37 namespace = Namespace(json, | 38 namespace = Namespace(json, |
| 38 source_file, | 39 source_file, |
| 39 include_compiler_options=include_compiler_options, | 40 include_compiler_options=include_compiler_options, |
| 40 environment=environment) | 41 environment=environment, |
| 42 allow_inline_enums=self._allow_inline_enums) | |
| 41 self.namespaces[namespace.name] = namespace | 43 self.namespaces[namespace.name] = namespace |
| 42 return namespace | 44 return namespace |
| 43 | 45 |
| 44 | 46 |
| 45 def CreateFeature(name, model): | 47 def CreateFeature(name, model): |
| 46 if isinstance(model, dict): | 48 if isinstance(model, dict): |
| 47 return SimpleFeature(name, model) | 49 return SimpleFeature(name, model) |
| 48 return ComplexFeature(name, [SimpleFeature(name, child) for child in model]) | 50 return ComplexFeature(name, [SimpleFeature(name, child) for child in model]) |
| 49 | 51 |
| 50 | 52 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 - |functions| a map of function names to their model.Function | 99 - |functions| a map of function names to their model.Function |
| 98 - |events| a map of event names to their model.Function | 100 - |events| a map of event names to their model.Function |
| 99 - |properties| a map of property names to their model.Property | 101 - |properties| a map of property names to their model.Property |
| 100 - |compiler_options| the compiler_options dict, only not empty if | 102 - |compiler_options| the compiler_options dict, only not empty if |
| 101 |include_compiler_options| is True | 103 |include_compiler_options| is True |
| 102 """ | 104 """ |
| 103 def __init__(self, | 105 def __init__(self, |
| 104 json, | 106 json, |
| 105 source_file, | 107 source_file, |
| 106 include_compiler_options=False, | 108 include_compiler_options=False, |
| 107 environment=None): | 109 environment=None, |
| 110 allow_inline_enums=True): | |
| 108 self.name = json['namespace'] | 111 self.name = json['namespace'] |
| 109 if 'description' not in json: | 112 if 'description' not in json: |
| 110 # TODO(kalman): Go back to throwing an error here. | 113 # TODO(kalman): Go back to throwing an error here. |
| 111 print('%s must have a "description" field. This will appear ' | 114 print('%s must have a "description" field. This will appear ' |
| 112 'on the API summary page.' % self.name) | 115 'on the API summary page.' % self.name) |
| 113 json['description'] = '' | 116 json['description'] = '' |
| 114 self.description = json['description'] | 117 self.description = json['description'] |
| 115 self.deprecated = json.get('deprecated', None) | 118 self.deprecated = json.get('deprecated', None) |
| 116 self.unix_name = UnixName(self.name) | 119 self.unix_name = UnixName(self.name) |
| 117 self.source_file = source_file | 120 self.source_file = source_file |
| 118 self.source_file_dir, self.source_file_filename = os.path.split(source_file) | 121 self.source_file_dir, self.source_file_filename = os.path.split(source_file) |
| 119 self.short_filename = os.path.basename(source_file).split('.')[0] | 122 self.short_filename = os.path.basename(source_file).split('.')[0] |
| 120 self.parent = None | 123 self.parent = None |
| 124 self.allow_inline_enums = allow_inline_enums | |
| 121 self.platforms = _GetPlatforms(json) | 125 self.platforms = _GetPlatforms(json) |
| 122 toplevel_origin = Origin(from_client=True, from_json=True) | 126 toplevel_origin = Origin(from_client=True, from_json=True) |
| 123 self.types = _GetTypes(self, json, self, toplevel_origin) | 127 self.types = _GetTypes(self, json, self, toplevel_origin) |
| 124 self.functions = _GetFunctions(self, json, self) | 128 self.functions = _GetFunctions(self, json, self) |
| 125 self.events = _GetEvents(self, json, self) | 129 self.events = _GetEvents(self, json, self) |
| 126 self.properties = _GetProperties(self, json, self, toplevel_origin) | 130 self.properties = _GetProperties(self, json, self, toplevel_origin) |
| 127 if include_compiler_options: | 131 if include_compiler_options: |
| 128 self.compiler_options = json.get('compiler_options', {}) | 132 self.compiler_options = json.get('compiler_options', {}) |
| 129 else: | 133 else: |
| 130 self.compiler_options = {} | 134 self.compiler_options = {} |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 | 195 |
| 192 json_type = json.get('type', None) | 196 json_type = json.get('type', None) |
| 193 if json_type == 'array': | 197 if json_type == 'array': |
| 194 self.property_type = PropertyType.ARRAY | 198 self.property_type = PropertyType.ARRAY |
| 195 self.item_type = Type( | 199 self.item_type = Type( |
| 196 self, '%sType' % name, json['items'], namespace, origin) | 200 self, '%sType' % name, json['items'], namespace, origin) |
| 197 elif '$ref' in json: | 201 elif '$ref' in json: |
| 198 self.property_type = PropertyType.REF | 202 self.property_type = PropertyType.REF |
| 199 self.ref_type = json['$ref'] | 203 self.ref_type = json['$ref'] |
| 200 elif 'enum' in json and json_type == 'string': | 204 elif 'enum' in json and json_type == 'string': |
| 205 if not namespace.allow_inline_enums and not isinstance(parent, Namespace): | |
|
not at google - send to devlin
2015/04/03 17:04:38
Is there a reason to make this a parameter rather
Devlin
2015/04/03 19:38:51
The docserver inlines types, and then feeds them b
| |
| 206 raise ParseException( | |
| 207 self, | |
| 208 'Inline enum "%s" found in namespace "%s". These are not allowed. ' | |
| 209 'See crbug.com/472279' % (name, namespace.name)) | |
| 201 self.property_type = PropertyType.ENUM | 210 self.property_type = PropertyType.ENUM |
| 202 self.enum_values = [EnumValue(value) for value in json['enum']] | 211 self.enum_values = [EnumValue(value) for value in json['enum']] |
| 203 self.cpp_enum_prefix_override = json.get('cpp_enum_prefix_override', None) | 212 self.cpp_enum_prefix_override = json.get('cpp_enum_prefix_override', None) |
| 204 elif json_type == 'any': | 213 elif json_type == 'any': |
| 205 self.property_type = PropertyType.ANY | 214 self.property_type = PropertyType.ANY |
| 206 elif json_type == 'binary': | 215 elif json_type == 'binary': |
| 207 self.property_type = PropertyType.BINARY | 216 self.property_type = PropertyType.BINARY |
| 208 elif json_type == 'boolean': | 217 elif json_type == 'boolean': |
| 209 self.property_type = PropertyType.BOOLEAN | 218 self.property_type = PropertyType.BOOLEAN |
| 210 elif json_type == 'integer': | 219 elif json_type == 'integer': |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 598 # Sanity check: platforms should not be an empty list. | 607 # Sanity check: platforms should not be an empty list. |
| 599 if not json['platforms']: | 608 if not json['platforms']: |
| 600 raise ValueError('"platforms" cannot be an empty list') | 609 raise ValueError('"platforms" cannot be an empty list') |
| 601 platforms = [] | 610 platforms = [] |
| 602 for platform_name in json['platforms']: | 611 for platform_name in json['platforms']: |
| 603 for platform_enum in _Enum.GetAll(Platforms): | 612 for platform_enum in _Enum.GetAll(Platforms): |
| 604 if platform_name == platform_enum.name: | 613 if platform_name == platform_enum.name: |
| 605 platforms.append(platform_enum) | 614 platforms.append(platform_enum) |
| 606 break | 615 break |
| 607 return platforms | 616 return platforms |
| OLD | NEW |