Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """Extract histogram names from the description XML file. | 5 """Extract histogram names from the description XML file. |
| 6 | 6 |
| 7 For more information on the format of the XML file, which is self-documenting, | 7 For more information on the format of the XML file, which is self-documenting, |
| 8 see histograms.xml; however, here is a simple example to get you started. The | 8 see histograms.xml; however, here is a simple example to get you started. The |
| 9 XML below will generate the following five histograms: | 9 XML below will generate the following five histograms: |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 <suffix name="Firefox"/> | 47 <suffix name="Firefox"/> |
| 48 <affected-histogram name="HistogramEnum"/> | 48 <affected-histogram name="HistogramEnum"/> |
| 49 </histogram_suffixes> | 49 </histogram_suffixes> |
| 50 | 50 |
| 51 </histogram_suffixes_list> | 51 </histogram_suffixes_list> |
| 52 | 52 |
| 53 </histogram-configuration> | 53 </histogram-configuration> |
| 54 | 54 |
| 55 """ | 55 """ |
| 56 | 56 |
| 57 import bisect | |
| 57 import copy | 58 import copy |
| 58 import logging | 59 import logging |
| 59 import xml.dom.minidom | 60 import xml.dom.minidom |
| 60 | 61 |
| 61 OWNER_FIELD_PLACEHOLDER = ( | 62 OWNER_FIELD_PLACEHOLDER = ( |
| 62 'Please list the metric\'s owners. Add more owner tags as needed.') | 63 'Please list the metric\'s owners. Add more owner tags as needed.') |
| 63 | 64 |
| 64 MAX_HISTOGRAM_SUFFIX_DEPENDENCY_DEPTH = 5 | 65 MAX_HISTOGRAM_SUFFIX_DEPENDENCY_DEPTH = 5 |
| 65 | 66 |
| 66 DEFAULT_BASE_HISTOGRAM_OBSOLETE_REASON = ( | 67 DEFAULT_BASE_HISTOGRAM_OBSOLETE_REASON = ( |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 logging.error('Enums %s and %s are not in alphabetical order', | 194 logging.error('Enums %s and %s are not in alphabetical order', |
| 194 last_name, name) | 195 last_name, name) |
| 195 have_errors = True | 196 have_errors = True |
| 196 last_name = name | 197 last_name = name |
| 197 | 198 |
| 198 if name in enums: | 199 if name in enums: |
| 199 logging.error('Duplicate enum %s', name) | 200 logging.error('Duplicate enum %s', name) |
| 200 have_errors = True | 201 have_errors = True |
| 201 continue | 202 continue |
| 202 | 203 |
| 203 last_int_value = None | |
| 204 enum_dict = {} | 204 enum_dict = {} |
| 205 enum_dict['name'] = name | 205 enum_dict['name'] = name |
| 206 enum_dict['values'] = {} | 206 enum_dict['values'] = {} |
| 207 | 207 |
| 208 for int_tag in enum.getElementsByTagName('int'): | 208 for int_tag in enum.getElementsByTagName('int'): |
| 209 value_dict = {} | 209 value_dict = {} |
| 210 int_value = int(int_tag.getAttribute('value')) | 210 int_value = int(int_tag.getAttribute('value')) |
| 211 if last_int_value is not None and int_value < last_int_value: | |
| 212 logging.error('Enum %s int values %d and %d are not in numerical order', | |
| 213 name, last_int_value, int_value) | |
| 214 have_errors = True | |
| 215 last_int_value = int_value | |
| 216 if int_value in enum_dict['values']: | 211 if int_value in enum_dict['values']: |
| 217 logging.error('Duplicate enum value %d for enum %s', int_value, name) | 212 logging.error('Duplicate enum value %d for enum %s', int_value, name) |
| 218 have_errors = True | 213 have_errors = True |
| 219 continue | 214 continue |
| 220 value_dict['label'] = int_tag.getAttribute('label') | 215 value_dict['label'] = int_tag.getAttribute('label') |
| 221 value_dict['summary'] = _JoinChildNodes(int_tag) | 216 value_dict['summary'] = _JoinChildNodes(int_tag) |
| 222 enum_dict['values'][int_value] = value_dict | 217 enum_dict['values'][int_value] = value_dict |
| 223 | 218 |
| 219 enum_items = sorted(enum_dict['values'].iteritems(), key=lambda i: i[0]) | |
| 220 enum_int_values = list(i[0] for i in enum_items) | |
|
Ilya Sherman
2017/04/12 20:29:33
Optional: I was thinking you could write something
| |
| 221 | |
| 222 last_int_value = None | |
| 223 for int_tag in enum.getElementsByTagName('int'): | |
| 224 int_value = int(int_tag.getAttribute('value')) | |
| 225 if last_int_value is not None and int_value < last_int_value: | |
| 226 logging.error('Enum %s int values %d and %d are not in numerical order', | |
| 227 name, last_int_value, int_value) | |
| 228 have_errors = True | |
| 229 left_item_index = bisect.bisect_left(enum_int_values, int_value) | |
| 230 if left_item_index == 0: | |
| 231 logging.warning('Insert value %d at the beginning', int_value) | |
| 232 else: | |
| 233 left_int_value, left_value_dict = enum_items[left_item_index - 1] | |
| 234 logging.warning('Insert value %d after %d ("%s")', | |
| 235 int_value, left_int_value, left_value_dict['label']) | |
| 236 else: | |
| 237 last_int_value = int_value | |
| 238 | |
| 224 summary_nodes = enum.getElementsByTagName('summary') | 239 summary_nodes = enum.getElementsByTagName('summary') |
| 225 if summary_nodes: | 240 if summary_nodes: |
| 226 enum_dict['summary'] = _NormalizeString(_JoinChildNodes(summary_nodes[0])) | 241 enum_dict['summary'] = _NormalizeString(_JoinChildNodes(summary_nodes[0])) |
| 227 | 242 |
| 228 enums[name] = enum_dict | 243 enums[name] = enum_dict |
| 229 | 244 |
| 230 return enums, have_errors | 245 return enums, have_errors |
| 231 | 246 |
| 232 | 247 |
| 233 def _ExtractOwners(xml_node): | 248 def _ExtractOwners(xml_node): |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 with open(filename, 'r') as f: | 524 with open(filename, 'r') as f: |
| 510 histograms, had_errors = ExtractHistogramsFromFile(f) | 525 histograms, had_errors = ExtractHistogramsFromFile(f) |
| 511 if had_errors: | 526 if had_errors: |
| 512 logging.error('Error parsing %s', filename) | 527 logging.error('Error parsing %s', filename) |
| 513 raise Error() | 528 raise Error() |
| 514 return histograms | 529 return histograms |
| 515 | 530 |
| 516 | 531 |
| 517 def ExtractNames(histograms): | 532 def ExtractNames(histograms): |
| 518 return sorted(histograms.keys()) | 533 return sorted(histograms.keys()) |
| OLD | NEW |