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

Side by Side Diff: build/android/gyp/java_cpp_enum.py

Issue 2815103004: Android: convert kEnumName to ENUM_NAME in java_cpp_enum.py. (Closed)
Patch Set: rebase Created 3 years, 8 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
OLDNEW
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
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
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 if isinstance(v, basestring):
agrieve 2017/04/13 13:52:34 nit: can you just mention in a comment here when t
estevenson 2017/04/13 14:11:14 Done.
120 ret[normal_keys[k]] = reduce(
agrieve 2017/04/13 13:52:34 nit: this is pretty hard to mentally grok (at leas
estevenson 2017/04/13 14:11:13 Done.
121 lambda x, y: x.replace(y, normal_keys[y]), normal_keys.keys(), v)
122 else:
123 ret[normal_keys[k]] = v
124 return ret
125
126
127 def _KCamelToShouty(s):
128 """Convert |s| from kCamelCase or CamelCase to SHOUTY_CASE.
129
130 kFooBar -> FOO_BAR
131 FooBar -> FOO_BAR
132 FooBAR9 -> FOO_BAR9
133 FooBARBaz -> FOO_BAR_BAZ
134 """
135 if not re.match(r'^k?([A-Z][^A-Z]+|[A-Z0-9]+)+$', s):
136 return s
137 # Strip the leading k.
138 s = re.sub(r'^k', '', s)
139 # Add _ between title words and anything else.
140 s = re.sub(r'([^_])([A-Z][^A-Z_0-9]+)', r'\1_\2', s)
141 # Add _ between lower -> upper transitions.
142 s = re.sub(r'([^A-Z_0-9])([A-Z])', r'\1_\2', s)
143 return s.upper()
144
145
108 class DirectiveSet(object): 146 class DirectiveSet(object):
109 class_name_override_key = 'CLASS_NAME_OVERRIDE' 147 class_name_override_key = 'CLASS_NAME_OVERRIDE'
110 enum_package_key = 'ENUM_PACKAGE' 148 enum_package_key = 'ENUM_PACKAGE'
111 prefix_to_strip_key = 'PREFIX_TO_STRIP' 149 prefix_to_strip_key = 'PREFIX_TO_STRIP'
112 150
113 known_keys = [class_name_override_key, enum_package_key, prefix_to_strip_key] 151 known_keys = [class_name_override_key, enum_package_key, prefix_to_strip_key]
114 152
115 def __init__(self): 153 def __init__(self):
116 self._directives = {} 154 self._directives = {}
117 155
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar: 435 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar:
398 for output_path, data in DoGenerate(input_paths): 436 for output_path, data in DoGenerate(input_paths):
399 build_utils.AddToZipHermetic(srcjar, output_path, data=data) 437 build_utils.AddToZipHermetic(srcjar, output_path, data=data)
400 438
401 if options.depfile: 439 if options.depfile:
402 build_utils.WriteDepfile(options.depfile, options.srcjar) 440 build_utils.WriteDepfile(options.depfile, options.srcjar)
403 441
404 442
405 if __name__ == '__main__': 443 if __name__ == '__main__':
406 DoMain(sys.argv[1:]) 444 DoMain(sys.argv[1:])
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698