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 import re | 6 import re |
7 import copy | 7 import copy |
8 | 8 |
9 class Model(object): | 9 class Model(object): |
10 """Model of all namespaces that comprise an API. | 10 """Model of all namespaces that comprise an API. |
(...skipping 22 matching lines...) Expand all Loading... | |
33 - |name| the name of the namespace | 33 - |name| the name of the namespace |
34 - |unix_name| the unix_name of the namespace | 34 - |unix_name| the unix_name of the namespace |
35 - |source_file| the file that contained the namespace definition | 35 - |source_file| the file that contained the namespace definition |
36 - |source_file_dir| the directory component of |source_file| | 36 - |source_file_dir| the directory component of |source_file| |
37 - |source_file_filename| the filename component of |source_file| | 37 - |source_file_filename| the filename component of |source_file| |
38 - |types| a map of type names to their model.Type | 38 - |types| a map of type names to their model.Type |
39 - |functions| a map of function names to their model.Function | 39 - |functions| a map of function names to their model.Function |
40 """ | 40 """ |
41 def __init__(self, json, source_file): | 41 def __init__(self, json, source_file): |
42 self.name = json['namespace'] | 42 self.name = json['namespace'] |
43 self.unix_name = _UnixName(self.name) | 43 self.unix_name = UnixName(self.name) |
44 self.source_file = source_file | 44 self.source_file = source_file |
45 self.source_file_dir, self.source_file_filename = os.path.split(source_file) | 45 self.source_file_dir, self.source_file_filename = os.path.split(source_file) |
46 self.types = {} | 46 self.types = {} |
47 self.functions = {} | 47 self.functions = {} |
48 # TODO(calamity): Implement this business. | |
not at google - send to devlin
2012/02/28 22:41:17
What is "this business"? Implementation of proper
calamity
2012/03/01 04:47:09
Leaving this TODO as discussed (for shared structu
| |
49 for property_json in json.get('properties', []): | |
50 pass | |
48 for type_json in json.get('types', []): | 51 for type_json in json.get('types', []): |
49 type_ = Type(type_json) | 52 type_ = Type(type_json['id'], type_json) |
50 self.types[type_.name] = type_ | 53 self.types[type_.name] = type_ |
51 for function_json in json.get('functions', []): | 54 for function_json in json.get('functions', []): |
52 if not function_json.get('nocompile', False): | 55 if not function_json.get('nocompile', False): |
53 function = Function(function_json) | 56 function = Function(function_json) |
54 self.functions[function.name] = function | 57 self.functions[function.name] = function |
55 | 58 |
56 class Type(object): | 59 class Type(object): |
57 """A Type defined in the json. | 60 """A Type defined in the json. |
58 | 61 |
59 Properties: | 62 Properties: |
60 - |name| the type name | 63 - |name| the type name |
61 - |description| the description of the type (if provided) | 64 - |description| the description of the type (if provided) |
62 - |properties| a map of property names to their model.Property | 65 - |properties| a map of property names to their model.Property |
Yoyo Zhou
2012/02/28 22:23:41
Add |functions| to the doc.
calamity
2012/03/01 04:47:09
Done.
| |
63 - |from_client| indicates that instances of the Type can originate from the | 66 - |from_client| indicates that instances of the Type can originate from the |
64 users of generated code, such as top-level types and function results | 67 users of generated code, such as top-level types and function results |
65 - |from_json| indicates that instances of the Type can originate from the | 68 - |from_json| indicates that instances of the Type can originate from the |
66 JSON (as described by the schema), such as top-level types and function | 69 JSON (as described by the schema), such as top-level types and function |
67 parameters | 70 parameters |
68 """ | 71 """ |
69 def __init__(self, json): | 72 def __init__(self, name, json): |
70 self.name = json['id'] | 73 assert ('properties' in json or 'additionalProperties' in json or |
74 'functions' in json), name + " has no properties or functions" | |
Yoyo Zhou
2012/02/28 22:23:41
nit: indent to paren
not at google - send to devlin
2012/02/28 22:41:17
nit... I like arranging multiple linked conditiona
calamity
2012/03/01 04:47:09
Types as in OBJECT properties. Should I have a fla
not at google - send to devlin
2012/03/01 07:09:37
Eh, the more you support the better I suppose. Mig
calamity
2012/03/01 23:58:48
Done.
| |
75 self.name = name | |
71 self.description = json.get('description') | 76 self.description = json.get('description') |
72 self.from_json = True | 77 self.from_json = True |
73 self.from_client = True | 78 self.from_client = True |
74 self.properties = {} | 79 self.properties = {} |
75 for prop_name, prop_json in json['properties'].items(): | 80 self.functions = {} |
81 for function_json in json.get('functions', []): | |
82 if not function_json.get('nocompile', False): | |
83 function = Function(function_json) | |
84 self.functions[function.name] = function | |
Yoyo Zhou
2012/02/28 22:23:41
This could fit on one line (same with 56-57).
calamity
2012/03/01 04:47:09
like
self.functions[function_json['name']] = Funct
Yoyo Zhou
2012/03/01 19:02:38
Yes, exactly.
calamity
2012/03/01 23:58:48
Done.
| |
85 for prop_name, prop_json in json.get('properties', {}).items(): | |
86 # Ignore callbacks in objects because they're handled renderer-side | |
not at google - send to devlin
2012/02/28 22:41:17
Reference to "renderer-side" doesn't make much sen
calamity
2012/03/01 04:47:09
Done.
| |
87 if prop_json.get('type') == 'function': | |
88 continue | |
76 self.properties[prop_name] = Property(prop_name, prop_json, | 89 self.properties[prop_name] = Property(prop_name, prop_json, |
77 from_json=True, | 90 from_json=True, |
78 from_client=True) | 91 from_client=True) |
92 additional_properties = json.get('additionalProperties') | |
93 if additional_properties: | |
94 prop = Property('additionalProperties', additional_properties) | |
95 prop.type_ = PropertyType.ADDITIONAL_PROPERTIES | |
Yoyo Zhou
2012/02/28 22:23:41
Seems like an abstraction violation to have to set
calamity
2012/03/01 04:47:09
Made a flag in Property.__init__.
| |
96 self.properties['additionalProperties'] = prop | |
not at google - send to devlin
2012/02/28 22:41:17
So... additionalProperties is stored as a property
calamity
2012/03/01 04:47:09
Raised an exception if any 2 properties have the s
| |
79 | 97 |
80 class Callback(object): | 98 class Callback(object): |
81 """A callback parameter to a Function. | 99 """A callback parameter to a Function. |
82 | 100 |
83 Properties: | 101 Properties: |
84 - |params| the parameters to this callback. | 102 - |params| the parameters to this callback. |
85 """ | 103 """ |
86 def __init__(self, json): | 104 def __init__(self, json): |
87 params = json['parameters'] | 105 params = json['parameters'] |
88 self.params = [] | 106 self.params = [] |
(...skipping 13 matching lines...) Expand all Loading... | |
102 - |name| the function name | 120 - |name| the function name |
103 - |params| a list of parameters to the function (order matters). A separate | 121 - |params| a list of parameters to the function (order matters). A separate |
104 parameter is used for each choice of a 'choices' parameter. | 122 parameter is used for each choice of a 'choices' parameter. |
105 - |description| a description of the function (if provided) | 123 - |description| a description of the function (if provided) |
106 - |callback| the callback parameter to the function. There should be exactly | 124 - |callback| the callback parameter to the function. There should be exactly |
107 one | 125 one |
108 """ | 126 """ |
109 def __init__(self, json): | 127 def __init__(self, json): |
110 self.name = json['name'] | 128 self.name = json['name'] |
111 self.params = [] | 129 self.params = [] |
112 self.description = json['description'] | 130 self.description = json.get('description') |
113 self.callback = None | 131 self.callback = None |
114 for param in json['parameters']: | 132 for param in json['parameters']: |
115 if param.get('type') == 'function': | 133 if param.get('type') == 'function': |
116 assert (not self.callback), self.name + " has more than one callback" | 134 assert (not self.callback), self.name + " has more than one callback" |
117 self.callback = Callback(param) | 135 self.callback = Callback(param) |
118 else: | 136 else: |
119 self.params.append(Property(param['name'], param, | 137 self.params.append(Property(param['name'], param, |
120 from_json=True)) | 138 from_json=True)) |
121 | 139 |
122 class Property(object): | 140 class Property(object): |
(...skipping 17 matching lines...) Expand all Loading... | |
140 from_client=False): | 158 from_client=False): |
141 """ | 159 """ |
142 Parameters: | 160 Parameters: |
143 - |from_json| indicates that instances of the Type can originate from the | 161 - |from_json| indicates that instances of the Type can originate from the |
144 JSON (as described by the schema), such as top-level types and function | 162 JSON (as described by the schema), such as top-level types and function |
145 parameters | 163 parameters |
146 - |from_client| indicates that instances of the Type can originate from the | 164 - |from_client| indicates that instances of the Type can originate from the |
147 users of generated code, such as top-level types and function results | 165 users of generated code, such as top-level types and function results |
148 """ | 166 """ |
149 self.name = name | 167 self.name = name |
150 self._unix_name = _UnixName(self.name) | 168 self._unix_name = UnixName(self.name) |
151 self._unix_name_used = False | 169 self._unix_name_used = False |
152 self.optional = json.get('optional', False) | 170 self.optional = json.get('optional', False) |
153 self.description = json.get('description') | 171 self.description = json.get('description') |
154 if '$ref' in json: | 172 if '$ref' in json: |
155 self.ref_type = json['$ref'] | 173 self.ref_type = json['$ref'] |
156 self.type_ = PropertyType.REF | 174 self.type_ = PropertyType.REF |
157 elif 'enum' in json: | 175 elif 'enum' in json: |
158 self.enum_values = [] | 176 self.enum_values = [] |
159 for value in json['enum']: | 177 for value in json['enum']: |
160 self.enum_values.append(value) | 178 self.enum_values.append(value) |
(...skipping 14 matching lines...) Expand all Loading... | |
175 self.item_type = Property(name + "Element", json['items'], | 193 self.item_type = Property(name + "Element", json['items'], |
176 from_json, | 194 from_json, |
177 from_client) | 195 from_client) |
178 self.type_ = PropertyType.ARRAY | 196 self.type_ = PropertyType.ARRAY |
179 elif json_type == 'object': | 197 elif json_type == 'object': |
180 self.type_ = PropertyType.OBJECT | 198 self.type_ = PropertyType.OBJECT |
181 # These members are read when this OBJECT Property is used as a Type | 199 # These members are read when this OBJECT Property is used as a Type |
182 self.properties = {} | 200 self.properties = {} |
183 self.from_json = from_json | 201 self.from_json = from_json |
184 self.from_client = from_client | 202 self.from_client = from_client |
185 for key, val in json.get('properties', {}).items(): | 203 type_ = Type(self.name, json) |
186 self.properties[key] = Property(key, val, | 204 self.properties = type_.properties |
187 from_json, | 205 self.functions = type_.functions |
188 from_client) | 206 self.from_client = from_client |
207 self.from_json = from_json | |
not at google - send to devlin
2012/02/28 22:41:17
you have repeated assignments here.
calamity
2012/03/01 04:47:09
Done.
| |
189 else: | 208 else: |
190 raise NotImplementedError(json_type) | 209 raise NotImplementedError(json_type) |
191 elif 'choices' in json: | 210 elif 'choices' in json: |
192 assert len(json['choices']), 'Choices has no choices\n%s' % json | 211 assert len(json['choices']), 'Choices has no choices\n%s' % json |
193 self.choices = {} | 212 self.choices = {} |
194 self.type_ = PropertyType.CHOICES | 213 self.type_ = PropertyType.CHOICES |
195 for choice_json in json['choices']: | 214 for choice_json in json['choices']: |
196 choice = Property(self.name, choice_json, | 215 choice = Property(self.name, choice_json, |
197 from_json, | 216 from_json, |
198 from_client) | 217 from_client) |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 INTEGER = _Info(True, "INTEGER") | 269 INTEGER = _Info(True, "INTEGER") |
251 DOUBLE = _Info(True, "DOUBLE") | 270 DOUBLE = _Info(True, "DOUBLE") |
252 BOOLEAN = _Info(True, "BOOLEAN") | 271 BOOLEAN = _Info(True, "BOOLEAN") |
253 STRING = _Info(True, "STRING") | 272 STRING = _Info(True, "STRING") |
254 ENUM = _Info(False, "ENUM") | 273 ENUM = _Info(False, "ENUM") |
255 ARRAY = _Info(False, "ARRAY") | 274 ARRAY = _Info(False, "ARRAY") |
256 REF = _Info(False, "REF") | 275 REF = _Info(False, "REF") |
257 CHOICES = _Info(False, "CHOICES") | 276 CHOICES = _Info(False, "CHOICES") |
258 OBJECT = _Info(False, "OBJECT") | 277 OBJECT = _Info(False, "OBJECT") |
259 ANY = _Info(False, "ANY") | 278 ANY = _Info(False, "ANY") |
279 ADDITIONAL_PROPERTIES = _Info(False, "ADDITIONAL_PROPERTIES") | |
260 | 280 |
261 def _UnixName(name): | 281 def UnixName(name): |
262 """Returns the unix_style name for a given lowerCamelCase string. | 282 """Returns the unix_style name for a given lowerCamelCase string. |
263 """ | 283 """ |
264 return '_'.join([x.lower() | 284 return '_'.join([x.lower() |
265 for x in re.findall('[A-Z][a-z_]*', name[0].upper() + name[1:])]) | 285 for x in re.findall('[A-Z][a-z_]*', name[0].upper() + name[1:])]) |
266 | 286 |
OLD | NEW |