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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 logging.error('Enums %s and %s are not in alphabetical order', | 193 logging.error('Enums %s and %s are not in alphabetical order', |
| 194 last_name, name) | 194 last_name, name) |
| 195 have_errors = True | 195 have_errors = True |
| 196 last_name = name | 196 last_name = name |
| 197 | 197 |
| 198 if name in enums: | 198 if name in enums: |
| 199 logging.error('Duplicate enum %s', name) | 199 logging.error('Duplicate enum %s', name) |
| 200 have_errors = True | 200 have_errors = True |
| 201 continue | 201 continue |
| 202 | 202 |
| 203 last_int_value = None | |
| 204 enum_dict = {} | 203 enum_dict = {} |
| 205 enum_dict['name'] = name | 204 enum_dict['name'] = name |
| 206 enum_dict['values'] = {} | 205 enum_dict['values'] = {} |
| 207 | 206 |
| 208 for int_tag in enum.getElementsByTagName('int'): | 207 for int_tag in enum.getElementsByTagName('int'): |
| 209 value_dict = {} | 208 value_dict = {} |
| 210 int_value = int(int_tag.getAttribute('value')) | 209 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']: | 210 if int_value in enum_dict['values']: |
| 217 logging.error('Duplicate enum value %d for enum %s', int_value, name) | 211 logging.error('Duplicate enum value %d for enum %s', int_value, name) |
| 218 have_errors = True | 212 have_errors = True |
| 219 continue | 213 continue |
| 220 value_dict['label'] = int_tag.getAttribute('label') | 214 value_dict['label'] = int_tag.getAttribute('label') |
| 221 value_dict['summary'] = _JoinChildNodes(int_tag) | 215 value_dict['summary'] = _JoinChildNodes(int_tag) |
| 222 enum_dict['values'][int_value] = value_dict | 216 enum_dict['values'][int_value] = value_dict |
| 223 | 217 |
| 218 enum_items = sorted(enum_dict['values'].iteritems(), key=lambda i: i[0]) | |
| 219 def _FindLeftEnumItemIndex(int_value): | |
| 220 """ Copy of bisect.bisect_left() modified to work with |enum_items|. """ | |
| 221 lo = 0 | |
| 222 hi = len(enum_items) | |
| 223 while lo < hi: | |
| 224 mid = (lo + hi) / 2 | |
| 225 if enum_items[mid][0] < int_value: lo = mid + 1 | |
| 226 else: hi = mid | |
| 227 return lo | |
|
Ilya Sherman
2017/04/11 21:34:25
Rather than copying bisect.bisect_left(), could yo
DmitrySkiba
2017/04/12 16:24:13
Done.
| |
| 228 | |
| 229 last_int_value = None | |
| 230 for int_tag in enum.getElementsByTagName('int'): | |
| 231 int_value = int(int_tag.getAttribute('value')) | |
| 232 if last_int_value is not None and int_value < last_int_value: | |
| 233 logging.error('Enum %s int values %d and %d are not in numerical order', | |
| 234 name, last_int_value, int_value) | |
| 235 have_errors = True | |
| 236 left_item_index = _FindLeftEnumItemIndex(int_value) | |
| 237 if left_item_index == 0: | |
| 238 logging.warning('Insert value %d at the beginning of enum %s', | |
| 239 int_value, name) | |
| 240 else: | |
| 241 left_item_index -= 1 | |
| 242 left_int_value, left_value_dict = enum_items[left_item_index] | |
|
Ilya Sherman
2017/04/11 21:34:25
nit: It looks like the -1 calculation could be mov
DmitrySkiba
2017/04/12 16:24:13
Done.
| |
| 243 logging.warning('Insert value %d after %d ("%s")', | |
| 244 int_value, left_int_value, left_value_dict['label']) | |
| 245 else: | |
| 246 last_int_value = int_value | |
| 247 | |
| 224 summary_nodes = enum.getElementsByTagName('summary') | 248 summary_nodes = enum.getElementsByTagName('summary') |
| 225 if summary_nodes: | 249 if summary_nodes: |
| 226 enum_dict['summary'] = _NormalizeString(_JoinChildNodes(summary_nodes[0])) | 250 enum_dict['summary'] = _NormalizeString(_JoinChildNodes(summary_nodes[0])) |
| 227 | 251 |
| 228 enums[name] = enum_dict | 252 enums[name] = enum_dict |
| 229 | 253 |
| 230 return enums, have_errors | 254 return enums, have_errors |
| 231 | 255 |
| 232 | 256 |
| 233 def _ExtractOwners(xml_node): | 257 def _ExtractOwners(xml_node): |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 with open(filename, 'r') as f: | 533 with open(filename, 'r') as f: |
| 510 histograms, had_errors = ExtractHistogramsFromFile(f) | 534 histograms, had_errors = ExtractHistogramsFromFile(f) |
| 511 if had_errors: | 535 if had_errors: |
| 512 logging.error('Error parsing %s', filename) | 536 logging.error('Error parsing %s', filename) |
| 513 raise Error() | 537 raise Error() |
| 514 return histograms | 538 return histograms |
| 515 | 539 |
| 516 | 540 |
| 517 def ExtractNames(histograms): | 541 def ExtractNames(histograms): |
| 518 return sorted(histograms.keys()) | 542 return sorted(histograms.keys()) |
| OLD | NEW |