OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 import collections | 7 import collections |
8 from datetime import date | 8 from datetime import date |
9 import re | 9 import re |
10 import optparse | 10 import optparse |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 self.comments[key] = value | 47 self.comments[key] = value |
48 | 48 |
49 @property | 49 @property |
50 def class_name(self): | 50 def class_name(self): |
51 return self.class_name_override or self.original_enum_name | 51 return self.class_name_override or self.original_enum_name |
52 | 52 |
53 def Finalize(self): | 53 def Finalize(self): |
54 self._Validate() | 54 self._Validate() |
55 self._AssignEntryIndices() | 55 self._AssignEntryIndices() |
56 self._StripPrefix() | 56 self._StripPrefix() |
| 57 self._NormalizeNames() |
57 | 58 |
58 def _Validate(self): | 59 def _Validate(self): |
59 assert self.class_name | 60 assert self.class_name |
60 assert self.enum_package | 61 assert self.enum_package |
61 assert self.entries | 62 assert self.entries |
62 if self.fixed_type and self.fixed_type not in ENUM_FIXED_TYPE_WHITELIST: | 63 if self.fixed_type and self.fixed_type not in ENUM_FIXED_TYPE_WHITELIST: |
63 raise Exception('Fixed type %s for enum %s not whitelisted.' % | 64 raise Exception('Fixed type %s for enum %s not whitelisted.' % |
64 (self.fixed_type, self.class_name)) | 65 (self.fixed_type, self.class_name)) |
65 | 66 |
66 def _AssignEntryIndices(self): | 67 def _AssignEntryIndices(self): |
(...skipping 18 matching lines...) Expand all Loading... |
85 prefix_to_strip = self.prefix_to_strip | 86 prefix_to_strip = self.prefix_to_strip |
86 if not prefix_to_strip: | 87 if not prefix_to_strip: |
87 prefix_to_strip = self.original_enum_name | 88 prefix_to_strip = self.original_enum_name |
88 prefix_to_strip = re.sub('(?!^)([A-Z]+)', r'_\1', prefix_to_strip).upper() | 89 prefix_to_strip = re.sub('(?!^)([A-Z]+)', r'_\1', prefix_to_strip).upper() |
89 prefix_to_strip += '_' | 90 prefix_to_strip += '_' |
90 if not all([w.startswith(prefix_to_strip) for w in self.entries.keys()]): | 91 if not all([w.startswith(prefix_to_strip) for w in self.entries.keys()]): |
91 prefix_to_strip = '' | 92 prefix_to_strip = '' |
92 | 93 |
93 def StripEntries(entries): | 94 def StripEntries(entries): |
94 ret = collections.OrderedDict() | 95 ret = collections.OrderedDict() |
95 for (k, v) in entries.iteritems(): | 96 for k, v in entries.iteritems(): |
96 stripped_key = k.replace(prefix_to_strip, '', 1) | 97 stripped_key = k.replace(prefix_to_strip, '', 1) |
97 if isinstance(v, basestring): | 98 if isinstance(v, basestring): |
98 stripped_value = v.replace(prefix_to_strip, '', 1) | 99 stripped_value = v.replace(prefix_to_strip, '') |
99 else: | 100 else: |
100 stripped_value = v | 101 stripped_value = v |
101 ret[stripped_key] = stripped_value | 102 ret[stripped_key] = stripped_value |
102 | 103 |
103 return ret | 104 return ret |
104 | 105 |
105 self.entries = StripEntries(self.entries) | 106 self.entries = StripEntries(self.entries) |
106 self.comments = StripEntries(self.comments) | 107 self.comments = StripEntries(self.comments) |
107 | 108 |
| 109 def _NormalizeNames(self): |
| 110 self.entries = _TransformKeys(self.entries, _KCamelToShouty) |
| 111 self.comments = _TransformKeys(self.comments, _KCamelToShouty) |
| 112 |
| 113 |
| 114 def _TransformKeys(d, func): |
| 115 """Normalize keys in |d| and update references to old keys in |d| values.""" |
| 116 normal_keys = {k: func(k) for k in d} |
| 117 ret = collections.OrderedDict() |
| 118 for k, v in d.iteritems(): |
| 119 # Need to transform values as well when the entry value was explicitly set |
| 120 # (since it could contain references to other enum entry values). |
| 121 if isinstance(v, basestring): |
| 122 for normal_key in normal_keys: |
| 123 v = v.replace(normal_key, normal_keys[normal_key]) |
| 124 ret[normal_keys[k]] = v |
| 125 return ret |
| 126 |
| 127 |
| 128 def _KCamelToShouty(s): |
| 129 """Convert |s| from kCamelCase or CamelCase to SHOUTY_CASE. |
| 130 |
| 131 kFooBar -> FOO_BAR |
| 132 FooBar -> FOO_BAR |
| 133 FooBAR9 -> FOO_BAR9 |
| 134 FooBARBaz -> FOO_BAR_BAZ |
| 135 """ |
| 136 if not re.match(r'^k?([A-Z][^A-Z]+|[A-Z0-9]+)+$', s): |
| 137 return s |
| 138 # Strip the leading k. |
| 139 s = re.sub(r'^k', '', s) |
| 140 # Add _ between title words and anything else. |
| 141 s = re.sub(r'([^_])([A-Z][^A-Z_0-9]+)', r'\1_\2', s) |
| 142 # Add _ between lower -> upper transitions. |
| 143 s = re.sub(r'([^A-Z_0-9])([A-Z])', r'\1_\2', s) |
| 144 return s.upper() |
| 145 |
| 146 |
108 class DirectiveSet(object): | 147 class DirectiveSet(object): |
109 class_name_override_key = 'CLASS_NAME_OVERRIDE' | 148 class_name_override_key = 'CLASS_NAME_OVERRIDE' |
110 enum_package_key = 'ENUM_PACKAGE' | 149 enum_package_key = 'ENUM_PACKAGE' |
111 prefix_to_strip_key = 'PREFIX_TO_STRIP' | 150 prefix_to_strip_key = 'PREFIX_TO_STRIP' |
112 | 151 |
113 known_keys = [class_name_override_key, enum_package_key, prefix_to_strip_key] | 152 known_keys = [class_name_override_key, enum_package_key, prefix_to_strip_key] |
114 | 153 |
115 def __init__(self): | 154 def __init__(self): |
116 self._directives = {} | 155 self._directives = {} |
117 | 156 |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar: | 436 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar: |
398 for output_path, data in DoGenerate(input_paths): | 437 for output_path, data in DoGenerate(input_paths): |
399 build_utils.AddToZipHermetic(srcjar, output_path, data=data) | 438 build_utils.AddToZipHermetic(srcjar, output_path, data=data) |
400 | 439 |
401 if options.depfile: | 440 if options.depfile: |
402 build_utils.WriteDepfile(options.depfile, options.srcjar) | 441 build_utils.WriteDepfile(options.depfile, options.srcjar) |
403 | 442 |
404 | 443 |
405 if __name__ == '__main__': | 444 if __name__ == '__main__': |
406 DoMain(sys.argv[1:]) | 445 DoMain(sys.argv[1:]) |
OLD | NEW |