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 self.params = [] | 234 self.params = [] |
233 self.returns = None | 235 self.returns = None |
234 _AddProperties(self, json, namespace) | 236 _AddProperties(self, json, namespace) |
235 if is_additional_properties: | 237 if is_additional_properties: |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 hierarchy.append(entity.name) | 415 hierarchy.append(entity.name) |
414 except AttributeError: | 416 except AttributeError: |
415 hierarchy.append(repr(entity)) | 417 hierarchy.append(repr(entity)) |
416 entity = entity.parent | 418 entity = entity.parent |
417 hierarchy.reverse() | 419 hierarchy.reverse() |
418 return hierarchy | 420 return hierarchy |
419 | 421 |
420 def _AddTypes(model, json, namespace): | 422 def _AddTypes(model, json, namespace): |
421 """Adds Type objects to |model| contained in the 'types' field of |json|. | 423 """Adds Type objects to |model| contained in the 'types' field of |json|. |
422 """ | 424 """ |
423 model.types = {} | 425 model.types = OrderedDict() |
424 for type_json in json.get('types', []): | 426 for type_json in json.get('types', []): |
425 type_ = Type(model, type_json['id'], type_json, namespace) | 427 type_ = Type(model, type_json['id'], type_json, namespace) |
426 model.types[type_.name] = type_ | 428 model.types[type_.name] = type_ |
427 | 429 |
428 def _AddFunctions(model, json, namespace): | 430 def _AddFunctions(model, json, namespace): |
429 """Adds Function objects to |model| contained in the 'functions' field of | 431 """Adds Function objects to |model| contained in the 'functions' field of |
430 |json|. | 432 |json|. |
431 """ | 433 """ |
432 model.functions = {} | 434 model.functions = OrderedDict() |
433 for function_json in json.get('functions', []): | 435 for function_json in json.get('functions', []): |
434 function = Function(model, function_json, namespace, from_json=True) | 436 function = Function(model, function_json, namespace, from_json=True) |
435 model.functions[function.name] = function | 437 model.functions[function.name] = function |
436 | 438 |
437 def _AddEvents(model, json, namespace): | 439 def _AddEvents(model, json, namespace): |
438 """Adds Function objects to |model| contained in the 'events' field of |json|. | 440 """Adds Function objects to |model| contained in the 'events' field of |json|. |
439 """ | 441 """ |
440 model.events = {} | 442 model.events = OrderedDict() |
441 for event_json in json.get('events', []): | 443 for event_json in json.get('events', []): |
442 event = Function(model, event_json, namespace, from_client=True) | 444 event = Function(model, event_json, namespace, from_client=True) |
443 model.events[event.name] = event | 445 model.events[event.name] = event |
444 | 446 |
445 def _AddProperties(model, | 447 def _AddProperties(model, |
446 json, | 448 json, |
447 namespace, | 449 namespace, |
448 from_json=False, | 450 from_json=False, |
449 from_client=False): | 451 from_client=False): |
450 """Adds model.Property objects to |model| contained in the 'properties' field | 452 """Adds model.Property objects to |model| contained in the 'properties' field |
451 of |json|. | 453 of |json|. |
452 """ | 454 """ |
453 model.properties = {} | 455 model.properties = OrderedDict() |
454 for name, property_json in json.get('properties', {}).items(): | 456 for name, property_json in json.get('properties', {}).items(): |
455 model.properties[name] = Property( | 457 model.properties[name] = Property( |
456 model, | 458 model, |
457 name, | 459 name, |
458 property_json, | 460 property_json, |
459 namespace, | 461 namespace, |
460 from_json=from_json, | 462 from_json=from_json, |
461 from_client=from_client) | 463 from_client=from_client) |
OLD | NEW |