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

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: Cap funciton names 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 392 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
not at google - send to devlin 2014/05/13 23:45:47 only 1 blank line
David Tseng 2014/05/14 02:16:14 Done.
414 def CamelName(self):
415 return SnakeToCamel(self.name)
416
413 class _Enum(object): 417 class _Enum(object):
414 """Superclass for enum types with a "name" field, setting up repr/eq/ne. 418 """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. 419 Enums need to do this so that equality/non-equality work over pickling.
416 """ 420 """
417 @staticmethod 421 @staticmethod
418 def GetAll(cls): 422 def GetAll(cls):
419 """Yields all _Enum objects declared in |cls|. 423 """Yields all _Enum objects declared in |cls|.
420 """ 424 """
421 for prop_key in dir(cls): 425 for prop_key in dir(cls):
422 prop_value = getattr(cls, prop_key) 426 prop_value = getattr(cls, prop_key)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 unix_name.append('_') 481 unix_name.append('_')
478 if c == '.': 482 if c == '.':
479 # Replace hello.world with hello_world. 483 # Replace hello.world with hello_world.
480 unix_name.append('_') 484 unix_name.append('_')
481 else: 485 else:
482 # Everything is lowercase. 486 # Everything is lowercase.
483 unix_name.append(c.lower()) 487 unix_name.append(c.lower())
484 return ''.join(unix_name) 488 return ''.join(unix_name)
485 489
486 490
491 def SnakeToCamel(snake):
not at google - send to devlin 2014/05/13 23:45:47 could you write some unit tests? see model_test.py
David Tseng 2014/05/14 02:16:14 Done.
492 ''' Converts a snake_cased_string to a camelCasedOne. '''
493 camel = []
494 i = 0
495 while i < len(snake):
496 if snake[i] == "_" and (i + 1) < len(snake):
not at google - send to devlin 2014/05/13 23:45:47 single quotes
David Tseng 2014/05/14 02:16:14 Done.
497 camel.append(snake[i + 1].upper())
498 i = i + 2
not at google - send to devlin 2014/05/13 23:45:47 += 2
David Tseng 2014/05/14 02:16:14 Done.
499 else:
500 camel.append(snake[i])
501 i = i + 1
not at google - send to devlin 2014/05/13 23:45:47 += 1
David Tseng 2014/05/14 02:16:14 Done.
502 return ''.join(camel)
not at google - send to devlin 2014/05/13 23:45:47 the above said, it might be simpler (and more effi
David Tseng 2014/05/14 02:16:14 Sounds like you'd still end up with some kind of l
not at google - send to devlin 2014/05/14 16:18:14 Actually split is likely to be much faster. Iterat
David Tseng 2014/05/14 18:11:31 Thanks; good to know.
503
504
487 def _StripNamespace(name, namespace): 505 def _StripNamespace(name, namespace):
488 if name.startswith(namespace.name + '.'): 506 if name.startswith(namespace.name + '.'):
489 return name[len(namespace.name + '.'):] 507 return name[len(namespace.name + '.'):]
490 return name 508 return name
491 509
492 510
493 def _GetModelHierarchy(entity): 511 def _GetModelHierarchy(entity):
494 """Returns the hierarchy of the given model entity.""" 512 """Returns the hierarchy of the given model entity."""
495 hierarchy = [] 513 hierarchy = []
496 while entity is not None: 514 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. 588 # Sanity check: platforms should not be an empty list.
571 if not json['platforms']: 589 if not json['platforms']:
572 raise ValueError('"platforms" cannot be an empty list') 590 raise ValueError('"platforms" cannot be an empty list')
573 platforms = [] 591 platforms = []
574 for platform_name in json['platforms']: 592 for platform_name in json['platforms']:
575 for platform_enum in _Enum.GetAll(Platforms): 593 for platform_enum in _Enum.GetAll(Platforms):
576 if platform_name == platform_enum.name: 594 if platform_name == platform_enum.name:
577 platforms.append(platform_enum) 595 platforms.append(platform_enum)
578 break 596 break
579 return platforms 597 return platforms
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698