OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 """Updates enums in histograms.xml file with values read from provided C++ enum. | 5 """Updates enums in histograms.xml file with values read from provided C++ enum. |
6 | 6 |
7 If the file was pretty-printed, the updated version is pretty-printed too. | 7 If the file was pretty-printed, the updated version is pretty-printed too. |
8 """ | 8 """ |
9 | 9 |
10 import logging | 10 import logging |
(...skipping 20 matching lines...) Expand all Loading... | |
31 @property | 31 @property |
32 def message(self): | 32 def message(self): |
33 return self.args[0] | 33 return self.args[0] |
34 | 34 |
35 | 35 |
36 def Log(message): | 36 def Log(message): |
37 logging.info(message) | 37 logging.info(message) |
38 | 38 |
39 | 39 |
40 def ReadHistogramValues(filename, start_marker, end_marker, strip_k_prefix): | 40 def ReadHistogramValues(filename, start_marker, end_marker, strip_k_prefix): |
41 """Returns a dictionary of enum values and a pair of labels that have the same | 41 """Returns a dictionary of enum values, read from a C++ file; or raises an |
42 enum values, read from a C++ file. | 42 exception if a pair of labels have the same enum values. |
43 | 43 |
44 Args: | 44 Args: |
45 filename: The unix-style path (relative to src/) of the file to open. | 45 filename: The unix-style path (relative to src/) of the file to open. |
46 start_marker: A regex that signifies the start of the enum values. | 46 start_marker: A regex that signifies the start of the enum values. |
47 end_marker: A regex that signifies the end of the enum values. | 47 end_marker: A regex that signifies the end of the enum values. |
48 strip_k_prefix: Set to True if enum values are declared as kFoo and the | 48 strip_k_prefix: Set to True if enum values are declared as kFoo and the |
49 'k' should be stripped. | 49 'k' should be stripped. |
Ilya Sherman
2017/04/21 22:51:40
Please add a "Raises:" section (see go/pystyle) to
| |
50 """ | 50 """ |
51 # Read the file as a list of lines | 51 # Read the file as a list of lines |
52 with open(path_util.GetInputFile(filename)) as f: | 52 with open(path_util.GetInputFile(filename)) as f: |
53 content = f.readlines() | 53 content = f.readlines() |
54 | 54 |
55 START_REGEX = re.compile(start_marker) | 55 START_REGEX = re.compile(start_marker) |
56 ITEM_REGEX = re.compile(r'^(\w+)') | 56 ITEM_REGEX = re.compile(r'^(\w+)') |
57 ITEM_REGEX_WITH_INIT = re.compile(r'(\w+)\s*=\s*(\d*)') | 57 ITEM_REGEX_WITH_INIT = re.compile(r'(\w+)\s*=\s*(\d*)') |
58 WRAPPED_INIT = re.compile(r'(\d+)') | 58 WRAPPED_INIT = re.compile(r'(\d+)') |
59 END_REGEX = re.compile(end_marker) | 59 END_REGEX = re.compile(end_marker) |
(...skipping 22 matching lines...) Expand all Loading... | |
82 next_line = next(iterator).strip() | 82 next_line = next(iterator).strip() |
83 enum_value = int(WRAPPED_INIT.match(next_line).group(1)) | 83 enum_value = int(WRAPPED_INIT.match(next_line).group(1)) |
84 else: | 84 else: |
85 m = ITEM_REGEX.match(line) | 85 m = ITEM_REGEX.match(line) |
86 if m: | 86 if m: |
87 label = m.group(1) | 87 label = m.group(1) |
88 else: | 88 else: |
89 continue | 89 continue |
90 # If two enum labels have the same value | 90 # If two enum labels have the same value |
91 if enum_value in result: | 91 if enum_value in result: |
92 return result, (result[enum_value], label) | 92 raise Exception(result[enum_value], label) |
93 if strip_k_prefix: | 93 if strip_k_prefix: |
94 assert label.startswith('k'), "Enum " + label + " should start with 'k'." | 94 assert label.startswith('k'), "Enum " + label + " should start with 'k'." |
95 label = label[1:] | 95 label = label[1:] |
96 result[enum_value] = label | 96 result[enum_value] = label |
97 enum_value += 1 | 97 enum_value += 1 |
98 return result, None | 98 return result |
99 | 99 |
100 | 100 |
101 def CreateEnumItemNode(document, value, label): | 101 def CreateEnumItemNode(document, value, label): |
102 """Creates an int element to append to an enum.""" | 102 """Creates an int element to append to an enum.""" |
103 item_node = document.createElement('int') | 103 item_node = document.createElement('int') |
104 item_node.attributes['value'] = str(value) | 104 item_node.attributes['value'] = str(value) |
105 item_node.attributes['label'] = label | 105 item_node.attributes['label'] = label |
106 return item_node | 106 return item_node |
107 | 107 |
108 | 108 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 source_enum_path, histograms_doc) | 177 source_enum_path, histograms_doc) |
178 | 178 |
179 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 179 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) |
180 return (xml, new_xml) | 180 return (xml, new_xml) |
181 | 181 |
182 | 182 |
183 def HistogramNeedsUpdate(histogram_enum_name, source_enum_path, start_marker, | 183 def HistogramNeedsUpdate(histogram_enum_name, source_enum_path, start_marker, |
184 end_marker, strip_k_prefix = False): | 184 end_marker, strip_k_prefix = False): |
185 """Reads a C++ enum from a .h file and does a dry run of updating | 185 """Reads a C++ enum from a .h file and does a dry run of updating |
186 histograms.xml to match. Returns true if the histograms.xml file would be | 186 histograms.xml to match. Returns true if the histograms.xml file would be |
187 changed. | 187 changed. |
Ilya Sherman
2017/04/21 22:51:40
Please update the documentation to list the potent
| |
188 | 188 |
189 Args: | 189 Args: |
190 histogram_enum_name: The name of the XML <enum> attribute to update. | 190 histogram_enum_name: The name of the XML <enum> attribute to update. |
191 source_enum_path: A unix-style path, relative to src/, giving | 191 source_enum_path: A unix-style path, relative to src/, giving |
192 the C++ header file from which to read the enum. | 192 the C++ header file from which to read the enum. |
193 start_marker: A regular expression that matches the start of the C++ enum. | 193 start_marker: A regular expression that matches the start of the C++ enum. |
194 end_marker: A regular expression that matches the end of the C++ enum. | 194 end_marker: A regular expression that matches the end of the C++ enum. |
195 strip_k_prefix: Set to True if enum values are declared as kFoo and the | 195 strip_k_prefix: Set to True if enum values are declared as kFoo and the |
196 'k' should be stripped. | 196 'k' should be stripped. |
197 """ | 197 """ |
198 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | 198 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
199 source_enum_values, duplicated_values = ReadHistogramValues( | 199 source_enum_values, duplicated_values = ReadHistogramValues( |
Ilya Sherman
2017/04/21 22:51:40
Shouldn't this call site be updated? There's no l
| |
200 source_enum_path, start_marker, end_marker, strip_k_prefix) | 200 source_enum_path, start_marker, end_marker, strip_k_prefix) |
201 if duplicated_values: | 201 if duplicated_values: |
202 return False, duplicated_values | 202 return False, duplicated_values |
Ilya Sherman
2017/04/21 22:51:40
Please update this as well (presumably by omitting
| |
203 | 203 |
204 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, | 204 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, |
205 source_enum_path) | 205 source_enum_path) |
206 return xml != new_xml, None | 206 return xml != new_xml, None |
Ilya Sherman
2017/04/21 22:51:40
Please update this as well.
| |
207 | 207 |
208 | 208 |
209 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 209 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
210 source_enum_path): | 210 source_enum_path): |
211 """Updates |histogram_enum_name| enum in histograms.xml file with values | 211 """Updates |histogram_enum_name| enum in histograms.xml file with values |
212 from the {value: 'key'} dictionary |source_enum_values|. A comment is added | 212 from the {value: 'key'} dictionary |source_enum_values|. A comment is added |
213 to histograms.xml citing that the values in |histogram_enum_name| were | 213 to histograms.xml citing that the values in |histogram_enum_name| were |
214 sourced from |source_enum_path|. | 214 sourced from |source_enum_path|. |
215 """ | 215 """ |
216 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, | 216 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, |
(...skipping 22 matching lines...) Expand all Loading... | |
239 strip_k_prefix: Set to True if enum values are declared as kFoo and the | 239 strip_k_prefix: Set to True if enum values are declared as kFoo and the |
240 'k' should be stripped. | 240 'k' should be stripped. |
241 """ | 241 """ |
242 | 242 |
243 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | 243 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
244 source_enum_values, ignored = ReadHistogramValues(source_enum_path, | 244 source_enum_values, ignored = ReadHistogramValues(source_enum_path, |
245 start_marker, end_marker, strip_k_prefix) | 245 start_marker, end_marker, strip_k_prefix) |
246 | 246 |
247 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 247 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
248 source_enum_path) | 248 source_enum_path) |
OLD | NEW |