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 from json_parse import OrderedDict |
| 10 |
9 class ParseException(Exception): | 11 class ParseException(Exception): |
10 """Thrown when data in the model is invalid. | 12 """Thrown when data in the model is invalid. |
11 """ | 13 """ |
12 def __init__(self, parent, message): | 14 def __init__(self, parent, message): |
13 hierarchy = _GetModelHierarchy(parent) | 15 hierarchy = _GetModelHierarchy(parent) |
14 hierarchy.append(message) | 16 hierarchy.append(message) |
15 Exception.__init__( | 17 Exception.__init__( |
16 self, 'Model parse exception at:\n' + '\n'.join(hierarchy)) | 18 self, 'Model parse exception at:\n' + '\n'.join(hierarchy)) |
17 | 19 |
18 class Model(object): | 20 class Model(object): |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 json, | 217 json, |
216 namespace, | 218 namespace, |
217 is_additional_properties=False, | 219 is_additional_properties=False, |
218 from_json=False, | 220 from_json=False, |
219 from_client=False): | 221 from_client=False): |
220 self.name = name | 222 self.name = name |
221 self.simple_name = _StripNamespace(self.name, namespace) | 223 self.simple_name = _StripNamespace(self.name, namespace) |
222 self._unix_name = UnixName(self.name) | 224 self._unix_name = UnixName(self.name) |
223 self._unix_name_used = False | 225 self._unix_name_used = False |
224 self.optional = json.get('optional', False) | 226 self.optional = json.get('optional', False) |
225 self.functions = {} | 227 self.functions = OrderedDict() |
226 self.has_value = False | 228 self.has_value = False |
227 self.description = json.get('description') | 229 self.description = json.get('description') |
228 self.parent = parent | 230 self.parent = parent |
229 self.from_json = from_json | 231 self.from_json = from_json |
230 self.from_client = from_client | 232 self.from_client = from_client |
231 self.instance_of = json.get('isInstanceOf', None) | 233 self.instance_of = json.get('isInstanceOf', None) |
232 _AddProperties(self, json, namespace) | 234 _AddProperties(self, json, namespace) |
233 if is_additional_properties: | 235 if is_additional_properties: |
234 self.type_ = PropertyType.ADDITIONAL_PROPERTIES | 236 self.type_ = PropertyType.ADDITIONAL_PROPERTIES |
235 elif '$ref' in json: | 237 elif '$ref' in json: |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 hierarchy.append(entity.name) | 403 hierarchy.append(entity.name) |
402 except AttributeError: | 404 except AttributeError: |
403 hierarchy.append(repr(entity)) | 405 hierarchy.append(repr(entity)) |
404 entity = entity.parent | 406 entity = entity.parent |
405 hierarchy.reverse() | 407 hierarchy.reverse() |
406 return hierarchy | 408 return hierarchy |
407 | 409 |
408 def _AddTypes(model, json, namespace): | 410 def _AddTypes(model, json, namespace): |
409 """Adds Type objects to |model| contained in the 'types' field of |json|. | 411 """Adds Type objects to |model| contained in the 'types' field of |json|. |
410 """ | 412 """ |
411 model.types = {} | 413 model.types = OrderedDict() |
412 for type_json in json.get('types', []): | 414 for type_json in json.get('types', []): |
413 type_ = Type(model, type_json['id'], type_json, namespace) | 415 type_ = Type(model, type_json['id'], type_json, namespace) |
414 model.types[type_.name] = type_ | 416 model.types[type_.name] = type_ |
415 | 417 |
416 def _AddFunctions(model, json, namespace): | 418 def _AddFunctions(model, json, namespace): |
417 """Adds Function objects to |model| contained in the 'functions' field of | 419 """Adds Function objects to |model| contained in the 'functions' field of |
418 |json|. | 420 |json|. |
419 """ | 421 """ |
420 model.functions = {} | 422 model.functions = OrderedDict() |
421 for function_json in json.get('functions', []): | 423 for function_json in json.get('functions', []): |
422 function = Function(model, function_json, namespace, from_json=True) | 424 function = Function(model, function_json, namespace, from_json=True) |
423 model.functions[function.name] = function | 425 model.functions[function.name] = function |
424 | 426 |
425 def _AddEvents(model, json, namespace): | 427 def _AddEvents(model, json, namespace): |
426 """Adds Function objects to |model| contained in the 'events' field of |json|. | 428 """Adds Function objects to |model| contained in the 'events' field of |json|. |
427 """ | 429 """ |
428 model.events = {} | 430 model.events = OrderedDict() |
429 for event_json in json.get('events', []): | 431 for event_json in json.get('events', []): |
430 event = Function(model, event_json, namespace, from_client=True) | 432 event = Function(model, event_json, namespace, from_client=True) |
431 model.events[event.name] = event | 433 model.events[event.name] = event |
432 | 434 |
433 def _AddProperties(model, | 435 def _AddProperties(model, |
434 json, | 436 json, |
435 namespace, | 437 namespace, |
436 from_json=False, | 438 from_json=False, |
437 from_client=False): | 439 from_client=False): |
438 """Adds model.Property objects to |model| contained in the 'properties' field | 440 """Adds model.Property objects to |model| contained in the 'properties' field |
439 of |json|. | 441 of |json|. |
440 """ | 442 """ |
441 model.properties = {} | 443 model.properties = OrderedDict() |
442 for name, property_json in json.get('properties', {}).items(): | 444 for name, property_json in json.get('properties', {}).items(): |
443 model.properties[name] = Property( | 445 model.properties[name] = Property( |
444 model, | 446 model, |
445 name, | 447 name, |
446 property_json, | 448 property_json, |
447 namespace, | 449 namespace, |
448 from_json=from_json, | 450 from_json=from_json, |
449 from_client=from_client) | 451 from_client=from_client) |
OLD | NEW |