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 | |
6 import os.path | 5 import os.path |
7 import re | |
8 | 6 |
9 from json_parse import OrderedDict | 7 from json_parse import OrderedDict |
| 8 from memoize import memoize |
10 | 9 |
11 class ParseException(Exception): | 10 class ParseException(Exception): |
12 """Thrown when data in the model is invalid. | 11 """Thrown when data in the model is invalid. |
13 """ | 12 """ |
14 def __init__(self, parent, message): | 13 def __init__(self, parent, message): |
15 hierarchy = _GetModelHierarchy(parent) | 14 hierarchy = _GetModelHierarchy(parent) |
16 hierarchy.append(message) | 15 hierarchy.append(message) |
17 Exception.__init__( | 16 Exception.__init__( |
18 self, 'Model parse exception at:\n' + '\n'.join(hierarchy)) | 17 self, 'Model parse exception at:\n' + '\n'.join(hierarchy)) |
19 | 18 |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 STRING = _PropertyTypeInfo(True, "string") | 365 STRING = _PropertyTypeInfo(True, "string") |
367 ENUM = _PropertyTypeInfo(False, "enum") | 366 ENUM = _PropertyTypeInfo(False, "enum") |
368 ARRAY = _PropertyTypeInfo(False, "array") | 367 ARRAY = _PropertyTypeInfo(False, "array") |
369 REF = _PropertyTypeInfo(False, "ref") | 368 REF = _PropertyTypeInfo(False, "ref") |
370 CHOICES = _PropertyTypeInfo(False, "choices") | 369 CHOICES = _PropertyTypeInfo(False, "choices") |
371 OBJECT = _PropertyTypeInfo(False, "object") | 370 OBJECT = _PropertyTypeInfo(False, "object") |
372 FUNCTION = _PropertyTypeInfo(False, "function") | 371 FUNCTION = _PropertyTypeInfo(False, "function") |
373 BINARY = _PropertyTypeInfo(False, "binary") | 372 BINARY = _PropertyTypeInfo(False, "binary") |
374 ANY = _PropertyTypeInfo(False, "any") | 373 ANY = _PropertyTypeInfo(False, "any") |
375 | 374 |
| 375 @memoize |
376 def UnixName(name): | 376 def UnixName(name): |
377 """Returns the unix_style name for a given lowerCamelCase string. | 377 '''Returns the unix_style name for a given lowerCamelCase string. |
378 """ | 378 ''' |
379 # First replace any lowerUpper patterns with lower_Upper. | 379 unix_name = [] |
380 s1 = re.sub('([a-z])([A-Z])', r'\1_\2', name) | 380 for i, c in enumerate(name): |
381 # Now replace any ACMEWidgets patterns with ACME_Widgets | 381 if c.isupper() and i > 0: |
382 s2 = re.sub('([A-Z]+)([A-Z][a-z])', r'\1_\2', s1) | 382 # Replace lowerUpper with lower_Upper. |
383 # Finally, replace any remaining periods, and make lowercase. | 383 if name[i - 1].islower(): |
384 return s2.replace('.', '_').lower() | 384 unix_name.append('_') |
| 385 # Replace ACMEWidgets with ACME_Widgets |
| 386 elif i + 1 < len(name) and name[i + 1].islower(): |
| 387 unix_name.append('_') |
| 388 if c == '.': |
| 389 # Replace hello.world with hello_world. |
| 390 unix_name.append('_') |
| 391 else: |
| 392 # Everything is lowercase. |
| 393 unix_name.append(c.lower()) |
| 394 return ''.join(unix_name) |
385 | 395 |
386 def _StripNamespace(name, namespace): | 396 def _StripNamespace(name, namespace): |
387 if name.startswith(namespace.name + '.'): | 397 if name.startswith(namespace.name + '.'): |
388 return name[len(namespace.name + '.'):] | 398 return name[len(namespace.name + '.'):] |
389 return name | 399 return name |
390 | 400 |
391 def _GetModelHierarchy(entity): | 401 def _GetModelHierarchy(entity): |
392 """Returns the hierarchy of the given model entity.""" | 402 """Returns the hierarchy of the given model entity.""" |
393 hierarchy = [] | 403 hierarchy = [] |
394 while entity is not None: | 404 while entity is not None: |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 def _GetPlatforms(json): | 468 def _GetPlatforms(json): |
459 if 'platforms' not in json: | 469 if 'platforms' not in json: |
460 return None | 470 return None |
461 platforms = [] | 471 platforms = [] |
462 for platform_name in json['platforms']: | 472 for platform_name in json['platforms']: |
463 for platform_enum in _Enum.GetAll(Platforms): | 473 for platform_enum in _Enum.GetAll(Platforms): |
464 if platform_name == platform_enum.name: | 474 if platform_name == platform_enum.name: |
465 platforms.append(platform_enum) | 475 platforms.append(platform_enum) |
466 break | 476 break |
467 return platforms | 477 return platforms |
OLD | NEW |