| 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. |
| 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: |
| (...skipping 29 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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |