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 cpp_namespace_pattern, | |
not at google - send to devlin
2014/08/25 22:22:47
There's a reason a lot of these namespace-level pr
lfg
2014/08/26 16:15:22
I thought this didn't feel right when I had to fix
| |
34 include_compiler_options=False): | |
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, |
39 cpp_namespace_pattern, | |
35 include_compiler_options=include_compiler_options) | 40 include_compiler_options=include_compiler_options) |
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 |
(...skipping 43 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 cpp_namespace_pattern='', | |
107 include_compiler_options=False): | |
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'] = '' |
114 self.cpp_namespace_pattern = cpp_namespace_pattern | |
105 self.description = json['description'] | 115 self.description = json['description'] |
106 self.deprecated = json.get('deprecated', None) | 116 self.deprecated = json.get('deprecated', None) |
107 self.unix_name = UnixName(self.name) | 117 self.unix_name = UnixName(self.name) |
108 self.source_file = source_file | 118 self.source_file = source_file |
109 self.source_file_dir, self.source_file_filename = os.path.split(source_file) | 119 self.source_file_dir, self.source_file_filename = os.path.split(source_file) |
110 self.short_filename = os.path.basename(source_file).split('.')[0] | 120 self.short_filename = os.path.basename(source_file).split('.')[0] |
111 self.parent = None | 121 self.parent = None |
112 self.platforms = _GetPlatforms(json) | 122 self.platforms = _GetPlatforms(json) |
113 toplevel_origin = Origin(from_client=True, from_json=True) | 123 toplevel_origin = Origin(from_client=True, from_json=True) |
114 self.types = _GetTypes(self, json, self, toplevel_origin) | 124 self.types = _GetTypes(self, json, self, toplevel_origin) |
(...skipping 471 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 |