OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Pretty-prints the histograms.xml file, alphabetizing tags, wrapping text | 6 """Pretty-prints the histograms.xml file, alphabetizing tags, wrapping text |
7 at 80 chars, enforcing standard attribute ordering, and standardizing | 7 at 80 chars, enforcing standard attribute ordering, and standardizing |
8 indentation. | 8 indentation. |
9 | 9 |
10 This is quite a bit more complicated than just calling tree.toprettyxml(); | 10 This is quite a bit more complicated than just calling tree.toprettyxml(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 """ | 73 """ |
74 if node.nodeType != xml.dom.minidom.Node.ELEMENT_NODE: | 74 if node.nodeType != xml.dom.minidom.Node.ELEMENT_NODE: |
75 for c in node.childNodes: TransformByAlphabetizing(c) | 75 for c in node.childNodes: TransformByAlphabetizing(c) |
76 return node | 76 return node |
77 | 77 |
78 # Element node with a tag name that we alphabetize the children of? | 78 # Element node with a tag name that we alphabetize the children of? |
79 if node.tagName in ALPHABETIZATION_RULES: | 79 if node.tagName in ALPHABETIZATION_RULES: |
80 # Put subnodes in a list of node,key pairs to allow for custom sorting. | 80 # Put subnodes in a list of node,key pairs to allow for custom sorting. |
81 subtag, key_function = ALPHABETIZATION_RULES[node.tagName] | 81 subtag, key_function = ALPHABETIZATION_RULES[node.tagName] |
82 subnodes = [] | 82 subnodes = [] |
83 last_key = -1 | 83 sort_key = -1 |
| 84 pending_node_indices = [] |
84 for c in node.childNodes: | 85 for c in node.childNodes: |
85 if (c.nodeType == xml.dom.minidom.Node.ELEMENT_NODE and | 86 if (c.nodeType == xml.dom.minidom.Node.ELEMENT_NODE and |
86 c.tagName == subtag): | 87 c.tagName == subtag): |
87 last_key = key_function(c) | 88 sort_key = key_function(c) |
88 # Subnodes that we don't want to rearrange use the last node's key, | 89 # Replace sort keys for delayed nodes. |
89 # so they stay in the same relative position. | 90 for idx in pending_node_indices: |
90 subnodes.append( (c, last_key) ) | 91 subnodes[idx][1] = sort_key |
| 92 pending_node_indices = [] |
| 93 else: |
| 94 # Subnodes that we don't want to rearrange use the next node's key, |
| 95 # so they stay in the same relative position. |
| 96 # Therefore we delay setting key until the next node is found. |
| 97 pending_node_indices.append(len(subnodes)) |
| 98 |
| 99 subnodes.append( [c, sort_key] ) |
| 100 |
| 101 # Use last sort key for trailing unknown nodes. |
| 102 for idx in pending_node_indices: |
| 103 subnodes[idx][1] = sort_key |
91 | 104 |
92 # Sort the subnode list. | 105 # Sort the subnode list. |
93 subnodes.sort(key=lambda pair: pair[1]) | 106 subnodes.sort(key=lambda pair: pair[1]) |
94 | 107 |
95 # Re-add the subnodes, transforming each recursively. | 108 # Re-add the subnodes, transforming each recursively. |
96 while node.firstChild: | 109 while node.firstChild: |
97 node.removeChild(node.firstChild) | 110 node.removeChild(node.firstChild) |
98 for (c, _) in subnodes: | 111 for (c, _) in subnodes: |
99 unsafeAppendChild(node, TransformByAlphabetizing(c)) | 112 unsafeAppendChild(node, TransformByAlphabetizing(c)) |
100 return node | 113 return node |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 logging.info('Creating backup file %s' % histograms_backup_filename) | 182 logging.info('Creating backup file %s' % histograms_backup_filename) |
170 shutil.move(histograms_pathname, histograms_backup_pathname) | 183 shutil.move(histograms_pathname, histograms_backup_pathname) |
171 | 184 |
172 logging.info('Writing new %s file' % histograms_filename) | 185 logging.info('Writing new %s file' % histograms_filename) |
173 with open(histograms_pathname, 'wb') as f: | 186 with open(histograms_pathname, 'wb') as f: |
174 f.write(pretty) | 187 f.write(pretty) |
175 | 188 |
176 | 189 |
177 if __name__ == '__main__': | 190 if __name__ == '__main__': |
178 main() | 191 main() |
OLD | NEW |