Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(871)

Side by Side Diff: tools/json_schema_compiler/model.py

Issue 273323002: Convert snakecase enum names to camelcase when stringified. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: One rename missed. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 10
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 if json_type == 'array': 183 if json_type == 'array':
184 self.property_type = PropertyType.ARRAY 184 self.property_type = PropertyType.ARRAY
185 self.item_type = Type( 185 self.item_type = Type(
186 self, '%sType' % name, json['items'], namespace, origin) 186 self, '%sType' % name, json['items'], namespace, origin)
187 elif '$ref' in json: 187 elif '$ref' in json:
188 self.property_type = PropertyType.REF 188 self.property_type = PropertyType.REF
189 self.ref_type = json['$ref'] 189 self.ref_type = json['$ref']
190 elif 'enum' in json and json_type == 'string': 190 elif 'enum' in json and json_type == 'string':
191 self.property_type = PropertyType.ENUM 191 self.property_type = PropertyType.ENUM
192 self.enum_values = [EnumValue(value) for value in json['enum']] 192 self.enum_values = [EnumValue(value) for value in json['enum']]
193 self.cpp_omit_enum_type = 'cpp_omit_enum_type' in json 193 self.cpp_enum_prefix_override = json.get('cpp_enum_prefix_override', None)
194 elif json_type == 'any': 194 elif json_type == 'any':
195 self.property_type = PropertyType.ANY 195 self.property_type = PropertyType.ANY
196 elif json_type == 'binary': 196 elif json_type == 'binary':
197 self.property_type = PropertyType.BINARY 197 self.property_type = PropertyType.BINARY
198 elif json_type == 'boolean': 198 elif json_type == 'boolean':
199 self.property_type = PropertyType.BOOLEAN 199 self.property_type = PropertyType.BOOLEAN
200 elif json_type == 'integer': 200 elif json_type == 'integer':
201 self.property_type = PropertyType.INTEGER 201 self.property_type = PropertyType.INTEGER
202 elif (json_type == 'double' or 202 elif (json_type == 'double' or
203 json_type == 'number'): 203 json_type == 'number'):
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 - |description| a description of the property (if provided) 403 - |description| a description of the property (if provided)
404 """ 404 """
405 def __init__(self, json): 405 def __init__(self, json):
406 if isinstance(json, dict): 406 if isinstance(json, dict):
407 self.name = json['name'] 407 self.name = json['name']
408 self.description = json.get('description') 408 self.description = json.get('description')
409 else: 409 else:
410 self.name = json 410 self.name = json
411 self.description = None 411 self.description = None
412 412
413 def CamelName(self):
414 return CamelName(self.name)
415
413 class _Enum(object): 416 class _Enum(object):
414 """Superclass for enum types with a "name" field, setting up repr/eq/ne. 417 """Superclass for enum types with a "name" field, setting up repr/eq/ne.
415 Enums need to do this so that equality/non-equality work over pickling. 418 Enums need to do this so that equality/non-equality work over pickling.
416 """ 419 """
417 @staticmethod 420 @staticmethod
418 def GetAll(cls): 421 def GetAll(cls):
419 """Yields all _Enum objects declared in |cls|. 422 """Yields all _Enum objects declared in |cls|.
420 """ 423 """
421 for prop_key in dir(cls): 424 for prop_key in dir(cls):
422 prop_value = getattr(cls, prop_key) 425 prop_value = getattr(cls, prop_key)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 unix_name.append('_') 480 unix_name.append('_')
478 if c == '.': 481 if c == '.':
479 # Replace hello.world with hello_world. 482 # Replace hello.world with hello_world.
480 unix_name.append('_') 483 unix_name.append('_')
481 else: 484 else:
482 # Everything is lowercase. 485 # Everything is lowercase.
483 unix_name.append(c.lower()) 486 unix_name.append(c.lower())
484 return ''.join(unix_name) 487 return ''.join(unix_name)
485 488
486 489
490 @memoize
491 def CamelName(snake):
492 ''' Converts a snake_cased_string to a camelCasedOne. '''
493 pieces = snake.split('_')
494 camel = []
495 for i, piece in enumerate(pieces):
496 if i == 0:
497 camel.append(piece)
498 else:
499 camel.append(piece.capitalize())
500 return ''.join(camel)
501
502
487 def _StripNamespace(name, namespace): 503 def _StripNamespace(name, namespace):
488 if name.startswith(namespace.name + '.'): 504 if name.startswith(namespace.name + '.'):
489 return name[len(namespace.name + '.'):] 505 return name[len(namespace.name + '.'):]
490 return name 506 return name
491 507
492 508
493 def _GetModelHierarchy(entity): 509 def _GetModelHierarchy(entity):
494 """Returns the hierarchy of the given model entity.""" 510 """Returns the hierarchy of the given model entity."""
495 hierarchy = [] 511 hierarchy = []
496 while entity is not None: 512 while entity is not None:
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 # Sanity check: platforms should not be an empty list. 586 # Sanity check: platforms should not be an empty list.
571 if not json['platforms']: 587 if not json['platforms']:
572 raise ValueError('"platforms" cannot be an empty list') 588 raise ValueError('"platforms" cannot be an empty list')
573 platforms = [] 589 platforms = []
574 for platform_name in json['platforms']: 590 for platform_name in json['platforms']:
575 for platform_enum in _Enum.GetAll(Platforms): 591 for platform_enum in _Enum.GetAll(Platforms):
576 if platform_name == platform_enum.name: 592 if platform_name == platform_enum.name:
577 platforms.append(platform_enum) 593 platforms.append(platform_enum)
578 break 594 break
579 return platforms 595 return platforms
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698