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 copy | 5 import copy |
6 import os.path | 6 import os.path |
7 import re | 7 import re |
8 | 8 |
9 class ParseException(Exception): | 9 class ParseException(Exception): |
10 """Thrown when data in the model is invalid. | 10 """Thrown when data in the model is invalid. |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 class Property(object): | 143 class Property(object): |
144 """A property of a type OR a parameter to a function. | 144 """A property of a type OR a parameter to a function. |
145 | 145 |
146 Properties: | 146 Properties: |
147 - |name| name of the property as in the json. This shouldn't change since | 147 - |name| name of the property as in the json. This shouldn't change since |
148 it is the key used to access DictionaryValues | 148 it is the key used to access DictionaryValues |
149 - |unix_name| the unix_style_name of the property. Used as variable name | 149 - |unix_name| the unix_style_name of the property. Used as variable name |
150 - |optional| a boolean representing whether the property is optional | 150 - |optional| a boolean representing whether the property is optional |
151 - |description| a description of the property (if provided) | 151 - |description| a description of the property (if provided) |
152 - |type_| the model.PropertyType of this property | 152 - |type_| the model.PropertyType of this property |
| 153 - |compiled_type| the model.PropertyType that this property should be |
| 154 compiled to from the JSON. Defaults to type_. |
153 - |ref_type| the type that the REF property is referencing. Can be used to | 155 - |ref_type| the type that the REF property is referencing. Can be used to |
154 map to its model.Type | 156 map to its model.Type |
155 - |item_type| a model.Property representing the type of each element in an | 157 - |item_type| a model.Property representing the type of each element in an |
156 ARRAY | 158 ARRAY |
157 - |properties| the properties of an OBJECT parameter | 159 - |properties| the properties of an OBJECT parameter |
158 - |from_client| indicates that instances of the Type can originate from the | 160 - |from_client| indicates that instances of the Type can originate from the |
159 users of generated code, such as top-level types and function results | 161 users of generated code, such as top-level types and function results |
160 - |from_json| indicates that instances of the Type can originate from the | 162 - |from_json| indicates that instances of the Type can originate from the |
161 JSON (as described by the schema), such as top-level types and function | 163 JSON (as described by the schema), such as top-level types and function |
162 parameters | 164 parameters |
163 """ | 165 """ |
164 | 166 |
165 def __init__(self, parent, name, json, is_additional_properties=False, | 167 def __init__(self, parent, name, json, is_additional_properties=False, |
166 from_json=False, from_client=False): | 168 from_json=False, from_client=False): |
167 self.name = name | 169 self.name = name |
168 self._unix_name = UnixName(self.name) | 170 self._unix_name = UnixName(self.name) |
169 self._unix_name_used = False | 171 self._unix_name_used = False |
170 self.optional = json.get('optional', False) | 172 self.optional = json.get('optional', False) |
171 self.functions = {} | 173 self.functions = {} |
172 self.has_value = False | 174 self.has_value = False |
173 self.description = json.get('description') | 175 self.description = json.get('description') |
174 self.parent = parent | 176 self.parent = parent |
175 self.from_json = from_json | 177 self.from_json = from_json |
176 self.from_client = from_client | 178 self.from_client = from_client |
177 self.instance_of = json.get('isInstanceOf', None) | 179 self.instance_of = json.get('isInstanceOf', None) |
178 _AddProperties(self, json) | 180 _AddProperties(self, json) |
179 if is_additional_properties: | 181 if is_additional_properties: |
180 self.type_ = PropertyType.ADDITIONAL_PROPERTIES | 182 self.type_ = PropertyType.ADDITIONAL_PROPERTIES |
| 183 self.compiled_type = self.type_ |
181 elif '$ref' in json: | 184 elif '$ref' in json: |
182 self.ref_type = json['$ref'] | 185 self.ref_type = json['$ref'] |
183 self.type_ = PropertyType.REF | 186 self.type_ = PropertyType.REF |
| 187 self.compiled_type = self.type_ |
184 elif 'enum' in json and json.get('type') == 'string': | 188 elif 'enum' in json and json.get('type') == 'string': |
185 # Non-string enums (as in the case of [legalValues=(1,2)]) should fall | 189 # Non-string enums (as in the case of [legalValues=(1,2)]) should fall |
186 # through to the next elif. | 190 # through to the next elif. |
187 self.enum_values = [] | 191 self.enum_values = [] |
188 for value in json['enum']: | 192 for value in json['enum']: |
189 self.enum_values.append(value) | 193 self.enum_values.append(value) |
190 self.type_ = PropertyType.ENUM | 194 self.type_ = PropertyType.ENUM |
| 195 self.compiled_type = self.type_ |
191 elif 'type' in json: | 196 elif 'type' in json: |
192 json_type = json['type'] | 197 json_type = json['type'] |
193 if json_type == 'string': | 198 if json_type == 'string': |
194 self.type_ = PropertyType.STRING | 199 self.type_ = PropertyType.STRING |
195 elif json_type == 'any': | 200 elif json_type == 'any': |
196 self.type_ = PropertyType.ANY | 201 self.type_ = PropertyType.ANY |
197 elif json_type == 'boolean': | 202 elif json_type == 'boolean': |
198 self.type_ = PropertyType.BOOLEAN | 203 self.type_ = PropertyType.BOOLEAN |
199 elif json_type == 'integer': | 204 elif json_type == 'integer': |
200 self.type_ = PropertyType.INTEGER | 205 self.type_ = PropertyType.INTEGER |
(...skipping 10 matching lines...) Expand all Loading... |
211 type_ = Type(self, self.name, json) | 216 type_ = Type(self, self.name, json) |
212 # self.properties will already have some value from |_AddProperties|. | 217 # self.properties will already have some value from |_AddProperties|. |
213 self.properties.update(type_.properties) | 218 self.properties.update(type_.properties) |
214 self.functions = type_.functions | 219 self.functions = type_.functions |
215 elif json_type == 'function': | 220 elif json_type == 'function': |
216 self.type_ = PropertyType.FUNCTION | 221 self.type_ = PropertyType.FUNCTION |
217 elif json_type == 'binary': | 222 elif json_type == 'binary': |
218 self.type_ = PropertyType.BINARY | 223 self.type_ = PropertyType.BINARY |
219 else: | 224 else: |
220 raise ParseException(self, 'type ' + json_type + ' not recognized') | 225 raise ParseException(self, 'type ' + json_type + ' not recognized') |
| 226 if 'compiled_type' in json: |
| 227 json_compiled_type = json['compiled_type'] |
| 228 if json_compiled_type == 'int64': |
| 229 self.compiled_type = PropertyType.INT64 |
| 230 else: |
| 231 # TODO(mwrosen): Support more types as necessary. |
| 232 raise ParseException(self, 'type ' + json_compiled_type + |
| 233 ' not recognized') |
| 234 else: |
| 235 self.compiled_type = self.type_ |
221 elif 'choices' in json: | 236 elif 'choices' in json: |
222 if not json['choices'] or len(json['choices']) == 0: | 237 if not json['choices'] or len(json['choices']) == 0: |
223 raise ParseException(self, 'Choices has no choices') | 238 raise ParseException(self, 'Choices has no choices') |
224 self.choices = {} | 239 self.choices = {} |
225 self.type_ = PropertyType.CHOICES | 240 self.type_ = PropertyType.CHOICES |
| 241 self.compiled_type = self.type_ |
226 for choice_json in json['choices']: | 242 for choice_json in json['choices']: |
227 choice = Property(self, self.name, choice_json, | 243 choice = Property(self, self.name, choice_json, |
228 from_json=from_json, | 244 from_json=from_json, |
229 from_client=from_client) | 245 from_client=from_client) |
230 choice.unix_name = UnixName(self.name + choice.type_.name) | 246 choice.unix_name = UnixName(self.name + choice.type_.name) |
231 # The existence of any single choice is optional | 247 # The existence of any single choice is optional |
232 choice.optional = True | 248 choice.optional = True |
233 self.choices[choice.type_] = choice | 249 self.choices[choice.type_] = choice |
234 elif 'value' in json: | 250 elif 'value' in json: |
235 self.has_value = True | 251 self.has_value = True |
236 self.value = json['value'] | 252 self.value = json['value'] |
237 if type(self.value) == int: | 253 if type(self.value) == int: |
238 self.type_ = PropertyType.INTEGER | 254 self.type_ = PropertyType.INTEGER |
| 255 self.compiled_type = self.type_ |
239 else: | 256 else: |
240 # TODO(kalman): support more types as necessary. | 257 # TODO(kalman): support more types as necessary. |
241 raise ParseException( | 258 raise ParseException( |
242 self, '"%s" is not a supported type' % type(self.value)) | 259 self, '"%s" is not a supported type' % type(self.value)) |
243 else: | 260 else: |
244 raise ParseException( | 261 raise ParseException( |
245 self, 'Property has no type, $ref, choices, or value') | 262 self, 'Property has no type, $ref, choices, or value') |
246 | 263 |
247 def GetUnixName(self): | 264 def GetUnixName(self): |
248 """Gets the property's unix_name. Raises AttributeError if not set. | 265 """Gets the property's unix_name. Raises AttributeError if not set. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 """ | 297 """ |
281 class _Info(object): | 298 class _Info(object): |
282 def __init__(self, is_fundamental, name): | 299 def __init__(self, is_fundamental, name): |
283 self.is_fundamental = is_fundamental | 300 self.is_fundamental = is_fundamental |
284 self.name = name | 301 self.name = name |
285 | 302 |
286 def __repr__(self): | 303 def __repr__(self): |
287 return self.name | 304 return self.name |
288 | 305 |
289 INTEGER = _Info(True, "INTEGER") | 306 INTEGER = _Info(True, "INTEGER") |
| 307 INT64 = _Info(True, "INT64") |
290 DOUBLE = _Info(True, "DOUBLE") | 308 DOUBLE = _Info(True, "DOUBLE") |
291 BOOLEAN = _Info(True, "BOOLEAN") | 309 BOOLEAN = _Info(True, "BOOLEAN") |
292 STRING = _Info(True, "STRING") | 310 STRING = _Info(True, "STRING") |
293 ENUM = _Info(False, "ENUM") | 311 ENUM = _Info(False, "ENUM") |
294 ARRAY = _Info(False, "ARRAY") | 312 ARRAY = _Info(False, "ARRAY") |
295 REF = _Info(False, "REF") | 313 REF = _Info(False, "REF") |
296 CHOICES = _Info(False, "CHOICES") | 314 CHOICES = _Info(False, "CHOICES") |
297 OBJECT = _Info(False, "OBJECT") | 315 OBJECT = _Info(False, "OBJECT") |
298 FUNCTION = _Info(False, "FUNCTION") | 316 FUNCTION = _Info(False, "FUNCTION") |
299 BINARY = _Info(False, "BINARY") | 317 BINARY = _Info(False, "BINARY") |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 of |json|. | 370 of |json|. |
353 """ | 371 """ |
354 model.properties = {} | 372 model.properties = {} |
355 for name, property_json in json.get('properties', {}).items(): | 373 for name, property_json in json.get('properties', {}).items(): |
356 model.properties[name] = Property( | 374 model.properties[name] = Property( |
357 model, | 375 model, |
358 name, | 376 name, |
359 property_json, | 377 property_json, |
360 from_json=from_json, | 378 from_json=from_json, |
361 from_client=from_client) | 379 from_client=from_client) |
OLD | NEW |