| 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 class ParseException(Exception): | 10 class ParseException(Exception): |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 json_type = json.get('type', None) | 134 json_type = json.get('type', None) |
| 135 if json_type == 'array': | 135 if json_type == 'array': |
| 136 self.property_type = PropertyType.ARRAY | 136 self.property_type = PropertyType.ARRAY |
| 137 self.item_type = Type( | 137 self.item_type = Type( |
| 138 self, '%sType' % name, json['items'], namespace, origin) | 138 self, '%sType' % name, json['items'], namespace, origin) |
| 139 elif '$ref' in json: | 139 elif '$ref' in json: |
| 140 self.property_type = PropertyType.REF | 140 self.property_type = PropertyType.REF |
| 141 self.ref_type = json['$ref'] | 141 self.ref_type = json['$ref'] |
| 142 elif 'enum' in json and json_type == 'string': | 142 elif 'enum' in json and json_type == 'string': |
| 143 self.property_type = PropertyType.ENUM | 143 self.property_type = PropertyType.ENUM |
| 144 self.enum_values = [EnumValue(value) for value in json['enum']] | 144 self.enum_values = [value for value in json['enum']] |
| 145 elif json_type == 'any': | 145 elif json_type == 'any': |
| 146 self.property_type = PropertyType.ANY | 146 self.property_type = PropertyType.ANY |
| 147 elif json_type == 'binary': | 147 elif json_type == 'binary': |
| 148 self.property_type = PropertyType.BINARY | 148 self.property_type = PropertyType.BINARY |
| 149 elif json_type == 'boolean': | 149 elif json_type == 'boolean': |
| 150 self.property_type = PropertyType.BOOLEAN | 150 self.property_type = PropertyType.BOOLEAN |
| 151 elif json_type == 'integer': | 151 elif json_type == 'integer': |
| 152 self.property_type = PropertyType.INTEGER | 152 self.property_type = PropertyType.INTEGER |
| 153 elif (json_type == 'double' or | 153 elif (json_type == 'double' or |
| 154 json_type == 'number'): | 154 json_type == 'number'): |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 return | 336 return |
| 337 if self._unix_name_used: | 337 if self._unix_name_used: |
| 338 raise AttributeError( | 338 raise AttributeError( |
| 339 'Cannot set the unix_name on %s; ' | 339 'Cannot set the unix_name on %s; ' |
| 340 'it is already used elsewhere as %s' % | 340 'it is already used elsewhere as %s' % |
| 341 (self.name, self._unix_name)) | 341 (self.name, self._unix_name)) |
| 342 self._unix_name = unix_name | 342 self._unix_name = unix_name |
| 343 | 343 |
| 344 unix_name = property(GetUnixName, SetUnixName) | 344 unix_name = property(GetUnixName, SetUnixName) |
| 345 | 345 |
| 346 class EnumValue(object): | |
| 347 """A single value from an enum. | |
| 348 Properties: | |
| 349 - |name| name of the property as in the json. | |
| 350 - |description| a description of the property (if provided) | |
| 351 """ | |
| 352 def __init__(self, json): | |
| 353 if isinstance(json, dict): | |
| 354 self.name = json['name'] | |
| 355 self.description = json.get('description') | |
| 356 else: | |
| 357 self.name = json | |
| 358 self.description = None | |
| 359 | |
| 360 class _Enum(object): | 346 class _Enum(object): |
| 361 """Superclass for enum types with a "name" field, setting up repr/eq/ne. | 347 """Superclass for enum types with a "name" field, setting up repr/eq/ne. |
| 362 Enums need to do this so that equality/non-equality work over pickling. | 348 Enums need to do this so that equality/non-equality work over pickling. |
| 363 """ | 349 """ |
| 364 @staticmethod | 350 @staticmethod |
| 365 def GetAll(cls): | 351 def GetAll(cls): |
| 366 """Yields all _Enum objects declared in |cls|. | 352 """Yields all _Enum objects declared in |cls|. |
| 367 """ | 353 """ |
| 368 for prop_key in dir(cls): | 354 for prop_key in dir(cls): |
| 369 prop_value = getattr(cls, prop_key) | 355 prop_value = getattr(cls, prop_key) |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 # Sanity check: platforms should not be an empty list. | 491 # Sanity check: platforms should not be an empty list. |
| 506 if not json['platforms']: | 492 if not json['platforms']: |
| 507 raise ValueError('"platforms" cannot be an empty list') | 493 raise ValueError('"platforms" cannot be an empty list') |
| 508 platforms = [] | 494 platforms = [] |
| 509 for platform_name in json['platforms']: | 495 for platform_name in json['platforms']: |
| 510 for platform_enum in _Enum.GetAll(Platforms): | 496 for platform_enum in _Enum.GetAll(Platforms): |
| 511 if platform_name == platform_enum.name: | 497 if platform_name == platform_enum.name: |
| 512 platforms.append(platform_enum) | 498 platforms.append(platform_enum) |
| 513 break | 499 break |
| 514 return platforms | 500 return platforms |
| OLD | NEW |