| 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 = [value for value in json['enum']] | 144 self.enum_values = [EnumValue(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 |
| 346 class _Enum(object): | 360 class _Enum(object): |
| 347 """Superclass for enum types with a "name" field, setting up repr/eq/ne. | 361 """Superclass for enum types with a "name" field, setting up repr/eq/ne. |
| 348 Enums need to do this so that equality/non-equality work over pickling. | 362 Enums need to do this so that equality/non-equality work over pickling. |
| 349 """ | 363 """ |
| 350 @staticmethod | 364 @staticmethod |
| 351 def GetAll(cls): | 365 def GetAll(cls): |
| 352 """Yields all _Enum objects declared in |cls|. | 366 """Yields all _Enum objects declared in |cls|. |
| 353 """ | 367 """ |
| 354 for prop_key in dir(cls): | 368 for prop_key in dir(cls): |
| 355 prop_value = getattr(cls, prop_key) | 369 prop_value = getattr(cls, prop_key) |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 def _GetPlatforms(json): | 502 def _GetPlatforms(json): |
| 489 if 'platforms' not in json: | 503 if 'platforms' not in json: |
| 490 return None | 504 return None |
| 491 platforms = [] | 505 platforms = [] |
| 492 for platform_name in json['platforms']: | 506 for platform_name in json['platforms']: |
| 493 for platform_enum in _Enum.GetAll(Platforms): | 507 for platform_enum in _Enum.GetAll(Platforms): |
| 494 if platform_name == platform_enum.name: | 508 if platform_name == platform_enum.name: |
| 495 platforms.append(platform_enum) | 509 platforms.append(platform_enum) |
| 496 break | 510 break |
| 497 return platforms | 511 return platforms |
| OLD | NEW |