Chromium Code Reviews| 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 15 matching lines...) Expand all Loading... | |
| 26 | 26 |
| 27 class UserError(Exception): | 27 class UserError(Exception): |
| 28 def __init__(self, message): | 28 def __init__(self, message): |
| 29 Exception.__init__(self, message) | 29 Exception.__init__(self, message) |
| 30 | 30 |
| 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 class DuplicatedValue(Exception): | |
| 37 """Exception raised for duplicated enum values. | |
| 38 | |
| 39 Attributes: | |
| 40 first_label: First enum label that shares the duplicated enum value. | |
| 41 second_label: Second enum label that shares the duplicated enum value. | |
| 42 """ | |
| 43 def __init__(self, first_label, second_label): | |
| 44 self.first_label = first_label | |
| 45 self.second_label = second_label | |
| 46 | |
| 47 | |
| 36 def Log(message): | 48 def Log(message): |
| 37 logging.info(message) | 49 logging.info(message) |
| 38 | 50 |
| 39 | 51 |
| 40 def ReadHistogramValues(filename, start_marker, end_marker, strip_k_prefix): | 52 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 | 53 """Creates a dictionary of enum values, read from a C++ file. |
| 42 enum values, read from a C++ file. | |
| 43 | 54 |
| 44 Args: | 55 Args: |
| 45 filename: The unix-style path (relative to src/) of the file to open. | 56 filename: The unix-style (relative to src/) of the file to open. |
| 46 start_marker: A regex that signifies the start of the enum values. | 57 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. | 58 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 | 59 strip_k_prefix: Set to True if enum values are declared as kFoo and the |
| 49 'k' should be stripped. | 60 'k' should be stripped. |
| 50 """ | 61 """ |
|
Ilya Sherman
2017/04/27 22:23:15
Why did you remove the Returns and Raises document
lunalu1
2017/04/28 00:31:32
Sorry I must have missed it after reverting this f
| |
| 51 # Read the file as a list of lines | 62 # Read the file as a list of lines |
| 52 with open(path_util.GetInputFile(filename)) as f: | 63 with open(path_util.GetInputFile(filename)) as f: |
| 53 content = f.readlines() | 64 content = f.readlines() |
| 54 | 65 |
| 55 START_REGEX = re.compile(start_marker) | 66 START_REGEX = re.compile(start_marker) |
| 56 ITEM_REGEX = re.compile(r'^(\w+)') | 67 ITEM_REGEX = re.compile(r'^(\w+)') |
| 57 ITEM_REGEX_WITH_INIT = re.compile(r'(\w+)\s*=\s*(\d*)') | 68 ITEM_REGEX_WITH_INIT = re.compile(r'(\w+)\s*=\s*(\d*)') |
| 58 WRAPPED_INIT = re.compile(r'(\d+)') | 69 WRAPPED_INIT = re.compile(r'(\d+)') |
| 59 END_REGEX = re.compile(end_marker) | 70 END_REGEX = re.compile(end_marker) |
| 60 | 71 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 82 next_line = next(iterator).strip() | 93 next_line = next(iterator).strip() |
| 83 enum_value = int(WRAPPED_INIT.match(next_line).group(1)) | 94 enum_value = int(WRAPPED_INIT.match(next_line).group(1)) |
| 84 else: | 95 else: |
| 85 m = ITEM_REGEX.match(line) | 96 m = ITEM_REGEX.match(line) |
| 86 if m: | 97 if m: |
| 87 label = m.group(1) | 98 label = m.group(1) |
| 88 else: | 99 else: |
| 89 continue | 100 continue |
| 90 # If two enum labels have the same value | 101 # If two enum labels have the same value |
| 91 if enum_value in result: | 102 if enum_value in result: |
| 92 return result, (result[enum_value], label) | 103 raise DuplicatedValue(result[enum_value], label) |
| 93 if strip_k_prefix: | 104 if strip_k_prefix: |
| 94 assert label.startswith('k'), "Enum " + label + " should start with 'k'." | 105 assert label.startswith('k'), "Enum " + label + " should start with 'k'." |
| 95 label = label[1:] | 106 label = label[1:] |
| 96 result[enum_value] = label | 107 result[enum_value] = label |
| 97 enum_value += 1 | 108 enum_value += 1 |
| 98 return result, None | 109 return result |
| 99 | 110 |
| 100 | 111 |
| 101 def CreateEnumItemNode(document, value, label): | 112 def CreateEnumItemNode(document, value, label): |
| 102 """Creates an int element to append to an enum.""" | 113 """Creates an int element to append to an enum.""" |
| 103 item_node = document.createElement('int') | 114 item_node = document.createElement('int') |
| 104 item_node.attributes['value'] = str(value) | 115 item_node.attributes['value'] = str(value) |
| 105 item_node.attributes['label'] = label | 116 item_node.attributes['label'] = label |
| 106 return item_node | 117 return item_node |
| 107 | 118 |
| 108 | 119 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, | 187 UpdateHistogramDefinitions(histogram_enum_name, source_enum_values, |
| 177 source_enum_path, histograms_doc) | 188 source_enum_path, histograms_doc) |
| 178 | 189 |
| 179 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 190 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) |
| 180 return (xml, new_xml) | 191 return (xml, new_xml) |
| 181 | 192 |
| 182 | 193 |
| 183 def HistogramNeedsUpdate(histogram_enum_name, source_enum_path, start_marker, | 194 def HistogramNeedsUpdate(histogram_enum_name, source_enum_path, start_marker, |
| 184 end_marker, strip_k_prefix = False): | 195 end_marker, strip_k_prefix = False): |
| 185 """Reads a C++ enum from a .h file and does a dry run of updating | 196 """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 | 197 histograms.xml to match. |
| 187 changed. | |
| 188 | 198 |
| 189 Args: | 199 Args: |
| 190 histogram_enum_name: The name of the XML <enum> attribute to update. | 200 histogram_enum_name: The name of the XML <enum> attribute to update. |
| 191 source_enum_path: A unix-style path, relative to src/, giving | 201 source_enum_path: A unix-style path, relative to src/, giving |
| 192 the C++ header file from which to read the enum. | 202 the C++ header file from which to read the enum. |
| 193 start_marker: A regular expression that matches the start of the C++ enum. | 203 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. | 204 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 | 205 strip_k_prefix: Set to True if enum values are declared as kFoo and the |
| 196 'k' should be stripped. | 206 'k' should be stripped. |
| 207 | |
| 208 Returns: | |
| 209 A boolean indicating wheather the histograms.xml file would be changed. | |
| 210 | |
| 211 Raises: | |
| 212 DuplicatedValue: An error when two enum labels share the same value. | |
| 197 """ | 213 """ |
| 198 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | 214 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
| 199 source_enum_values, duplicated_values = ReadHistogramValues( | 215 source_enum_values, duplicated_values = ReadHistogramValues( |
| 200 source_enum_path, start_marker, end_marker, strip_k_prefix) | 216 source_enum_path, start_marker, end_marker, strip_k_prefix) |
|
Ilya Sherman
2017/04/27 22:23:15
ReadHistogramValues now only returns the source_en
lunalu1
2017/04/28 00:31:32
Done.
| |
| 201 if duplicated_values: | 217 if duplicated_values: |
| 202 return False, duplicated_values | 218 return False |
| 203 | 219 |
| 204 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, | 220 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, |
| 205 source_enum_path) | 221 source_enum_path) |
| 206 return xml != new_xml, None | 222 return xml != new_xml |
| 207 | 223 |
| 208 | 224 |
| 209 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 225 def UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
| 210 source_enum_path): | 226 source_enum_path): |
| 211 """Updates |histogram_enum_name| enum in histograms.xml file with values | 227 """Updates |histogram_enum_name| enum in histograms.xml file with values |
| 212 from the {value: 'key'} dictionary |source_enum_values|. A comment is added | 228 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 | 229 to histograms.xml citing that the values in |histogram_enum_name| were |
| 214 sourced from |source_enum_path|. | 230 sourced from |source_enum_path|. |
| 215 """ | 231 """ |
| 216 (xml, new_xml) = _GetOldAndUpdatedXml(histogram_enum_name, source_enum_values, | 232 (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 | 255 strip_k_prefix: Set to True if enum values are declared as kFoo and the |
| 240 'k' should be stripped. | 256 'k' should be stripped. |
| 241 """ | 257 """ |
| 242 | 258 |
| 243 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) | 259 Log('Reading histogram enum definition from "{0}".'.format(source_enum_path)) |
| 244 source_enum_values, ignored = ReadHistogramValues(source_enum_path, | 260 source_enum_values, ignored = ReadHistogramValues(source_enum_path, |
| 245 start_marker, end_marker, strip_k_prefix) | 261 start_marker, end_marker, strip_k_prefix) |
| 246 | 262 |
| 247 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, | 263 UpdateHistogramFromDict(histogram_enum_name, source_enum_values, |
| 248 source_enum_path) | 264 source_enum_path) |
| OLD | NEW |