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): |
28 self.namespaces = {} | 28 self.namespaces = {} |
29 | 29 |
30 def AddNamespace(self, json, source_file, include_compiler_options=False): | 30 def AddNamespace(self, |
| 31 json, |
| 32 source_file, |
| 33 include_compiler_options=False, |
| 34 environment=None): |
31 """Add a namespace's json to the model and returns the namespace. | 35 """Add a namespace's json to the model and returns the namespace. |
32 """ | 36 """ |
33 namespace = Namespace(json, | 37 namespace = Namespace(json, |
34 source_file, | 38 source_file, |
35 include_compiler_options=include_compiler_options) | 39 include_compiler_options=include_compiler_options, |
| 40 environment=environment) |
36 self.namespaces[namespace.name] = namespace | 41 self.namespaces[namespace.name] = namespace |
37 return namespace | 42 return namespace |
38 | 43 |
39 | 44 |
40 def CreateFeature(name, model): | 45 def CreateFeature(name, model): |
41 if isinstance(model, dict): | 46 if isinstance(model, dict): |
42 return SimpleFeature(name, model) | 47 return SimpleFeature(name, model) |
43 return ComplexFeature(name, [SimpleFeature(name, child) for child in model]) | 48 return ComplexFeature(name, [SimpleFeature(name, child) for child in model]) |
44 | 49 |
45 | 50 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 - |source_file_filename| the filename component of |source_file| | 93 - |source_file_filename| the filename component of |source_file| |
89 - |platforms| if not None, the list of platforms that the namespace is | 94 - |platforms| if not None, the list of platforms that the namespace is |
90 available to | 95 available to |
91 - |types| a map of type names to their model.Type | 96 - |types| a map of type names to their model.Type |
92 - |functions| a map of function names to their model.Function | 97 - |functions| a map of function names to their model.Function |
93 - |events| a map of event names to their model.Function | 98 - |events| a map of event names to their model.Function |
94 - |properties| a map of property names to their model.Property | 99 - |properties| a map of property names to their model.Property |
95 - |compiler_options| the compiler_options dict, only not empty if | 100 - |compiler_options| the compiler_options dict, only not empty if |
96 |include_compiler_options| is True | 101 |include_compiler_options| is True |
97 """ | 102 """ |
98 def __init__(self, json, source_file, include_compiler_options=False): | 103 def __init__(self, |
| 104 json, |
| 105 source_file, |
| 106 include_compiler_options=False, |
| 107 environment=None): |
99 self.name = json['namespace'] | 108 self.name = json['namespace'] |
100 if 'description' not in json: | 109 if 'description' not in json: |
101 # TODO(kalman): Go back to throwing an error here. | 110 # TODO(kalman): Go back to throwing an error here. |
102 print('%s must have a "description" field. This will appear ' | 111 print('%s must have a "description" field. This will appear ' |
103 'on the API summary page.' % self.name) | 112 'on the API summary page.' % self.name) |
104 json['description'] = '' | 113 json['description'] = '' |
105 self.description = json['description'] | 114 self.description = json['description'] |
106 self.deprecated = json.get('deprecated', None) | 115 self.deprecated = json.get('deprecated', None) |
107 self.unix_name = UnixName(self.name) | 116 self.unix_name = UnixName(self.name) |
108 self.source_file = source_file | 117 self.source_file = source_file |
109 self.source_file_dir, self.source_file_filename = os.path.split(source_file) | 118 self.source_file_dir, self.source_file_filename = os.path.split(source_file) |
110 self.short_filename = os.path.basename(source_file).split('.')[0] | 119 self.short_filename = os.path.basename(source_file).split('.')[0] |
111 self.parent = None | 120 self.parent = None |
112 self.platforms = _GetPlatforms(json) | 121 self.platforms = _GetPlatforms(json) |
113 toplevel_origin = Origin(from_client=True, from_json=True) | 122 toplevel_origin = Origin(from_client=True, from_json=True) |
114 self.types = _GetTypes(self, json, self, toplevel_origin) | 123 self.types = _GetTypes(self, json, self, toplevel_origin) |
115 self.functions = _GetFunctions(self, json, self) | 124 self.functions = _GetFunctions(self, json, self) |
116 self.events = _GetEvents(self, json, self) | 125 self.events = _GetEvents(self, json, self) |
117 self.properties = _GetProperties(self, json, self, toplevel_origin) | 126 self.properties = _GetProperties(self, json, self, toplevel_origin) |
118 if include_compiler_options: | 127 if include_compiler_options: |
119 self.compiler_options = json.get('compiler_options', {}) | 128 self.compiler_options = json.get('compiler_options', {}) |
120 else: | 129 else: |
121 self.compiler_options = {} | 130 self.compiler_options = {} |
| 131 self.environment = environment |
122 self.documentation_options = json.get('documentation_options', {}) | 132 self.documentation_options = json.get('documentation_options', {}) |
123 | 133 |
124 | 134 |
125 class Origin(object): | 135 class Origin(object): |
126 """Stores the possible origin of model object as a pair of bools. These are: | 136 """Stores the possible origin of model object as a pair of bools. These are: |
127 | 137 |
128 |from_client| indicating that instances can originate from users of | 138 |from_client| indicating that instances can originate from users of |
129 generated code (for example, function results), or | 139 generated code (for example, function results), or |
130 |from_json| indicating that instances can originate from the JSON (for | 140 |from_json| indicating that instances can originate from the JSON (for |
131 example, function parameters) | 141 example, function parameters) |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 # Sanity check: platforms should not be an empty list. | 596 # Sanity check: platforms should not be an empty list. |
587 if not json['platforms']: | 597 if not json['platforms']: |
588 raise ValueError('"platforms" cannot be an empty list') | 598 raise ValueError('"platforms" cannot be an empty list') |
589 platforms = [] | 599 platforms = [] |
590 for platform_name in json['platforms']: | 600 for platform_name in json['platforms']: |
591 for platform_enum in _Enum.GetAll(Platforms): | 601 for platform_enum in _Enum.GetAll(Platforms): |
592 if platform_name == platform_enum.name: | 602 if platform_name == platform_enum.name: |
593 platforms.append(platform_enum) | 603 platforms.append(platform_enum) |
594 break | 604 break |
595 return platforms | 605 return platforms |
OLD | NEW |