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(); |
11 we need additional customization, like special attribute ordering in tags | 11 we need additional customization, like special attribute ordering in tags |
12 and wrapping text nodes, so we implement our own full custom XML pretty-printer. | 12 and wrapping text nodes, so we implement our own full custom XML pretty-printer. |
13 """ | 13 """ |
14 | 14 |
15 from __future__ import with_statement | 15 from __future__ import with_statement |
16 | 16 |
17 import logging | 17 import logging |
18 import os | 18 import os |
19 import shutil | 19 import shutil |
20 import sys | 20 import sys |
21 import xml.dom.minidom | 21 import xml.dom.minidom |
22 | 22 |
23 import print_style | |
24 | |
25 sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'python')) | |
26 from google import path_utils | |
27 | |
28 # Import the metrics/common module. | |
29 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) | 23 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) |
30 import diff_util | 24 import diff_util |
31 import presubmit_util | 25 import presubmit_util |
32 | 26 |
| 27 import print_style |
| 28 |
33 # Tags whose children we want to alphabetize. The key is the parent tag name, | 29 # Tags whose children we want to alphabetize. The key is the parent tag name, |
34 # and the value is a pair of the tag name of the children we want to sort, | 30 # and the value is a pair of the tag name of the children we want to sort, |
35 # and a key function that maps each child node to the desired sort key. | 31 # and a key function that maps each child node to the desired sort key. |
36 ALPHABETIZATION_RULES = { | 32 ALPHABETIZATION_RULES = { |
37 'histograms': ('histogram', lambda n: n.attributes['name'].value.lower()), | 33 'histograms': ('histogram', lambda n: n.attributes['name'].value.lower()), |
38 'enums': ('enum', lambda n: n.attributes['name'].value.lower()), | 34 'enums': ('enum', lambda n: n.attributes['name'].value.lower()), |
39 'enum': ('int', lambda n: int(n.attributes['value'].value)), | 35 'enum': ('int', lambda n: int(n.attributes['value'].value)), |
40 'histogram_suffixes_list': ( | 36 'histogram_suffixes_list': ( |
41 'histogram_suffixes', lambda n: n.attributes['name'].value.lower()), | 37 'histogram_suffixes', lambda n: n.attributes['name'].value.lower()), |
42 'histogram_suffixes': ('affected-histogram', | 38 'histogram_suffixes': ('affected-histogram', |
(...skipping 23 matching lines...) Expand all Loading... |
66 | 62 |
67 Args: | 63 Args: |
68 node: The minidom node to transform. | 64 node: The minidom node to transform. |
69 | 65 |
70 Returns: | 66 Returns: |
71 The minidom node, with children appropriately alphabetized. Note that the | 67 The minidom node, with children appropriately alphabetized. Note that the |
72 transformation is done in-place, i.e. the original minidom tree is modified | 68 transformation is done in-place, i.e. the original minidom tree is modified |
73 directly. | 69 directly. |
74 """ | 70 """ |
75 if node.nodeType != xml.dom.minidom.Node.ELEMENT_NODE: | 71 if node.nodeType != xml.dom.minidom.Node.ELEMENT_NODE: |
76 for c in node.childNodes: TransformByAlphabetizing(c) | 72 for c in node.childNodes: |
| 73 TransformByAlphabetizing(c) |
77 return node | 74 return node |
78 | 75 |
79 # Element node with a tag name that we alphabetize the children of? | 76 # Element node with a tag name that we alphabetize the children of? |
80 if node.tagName in ALPHABETIZATION_RULES: | 77 if node.tagName in ALPHABETIZATION_RULES: |
81 # Put subnodes in a list of node,key pairs to allow for custom sorting. | 78 # Put subnodes in a list of node,key pairs to allow for custom sorting. |
82 subtag, key_function = ALPHABETIZATION_RULES[node.tagName] | 79 subtag, key_function = ALPHABETIZATION_RULES[node.tagName] |
83 subnodes = [] | 80 subnodes = [] |
84 sort_key = -1 | 81 sort_key = -1 |
85 pending_node_indices = [] | 82 pending_node_indices = [] |
86 for c in node.childNodes: | 83 for c in node.childNodes: |
(...skipping 20 matching lines...) Expand all Loading... |
107 subnodes.sort(key=lambda pair: pair[1]) | 104 subnodes.sort(key=lambda pair: pair[1]) |
108 | 105 |
109 # Re-add the subnodes, transforming each recursively. | 106 # Re-add the subnodes, transforming each recursively. |
110 while node.firstChild: | 107 while node.firstChild: |
111 node.removeChild(node.firstChild) | 108 node.removeChild(node.firstChild) |
112 for (c, _) in subnodes: | 109 for (c, _) in subnodes: |
113 unsafeAppendChild(node, TransformByAlphabetizing(c)) | 110 unsafeAppendChild(node, TransformByAlphabetizing(c)) |
114 return node | 111 return node |
115 | 112 |
116 # Recursively handle other element nodes and other node types. | 113 # Recursively handle other element nodes and other node types. |
117 for c in node.childNodes: TransformByAlphabetizing(c) | 114 for c in node.childNodes: |
| 115 TransformByAlphabetizing(c) |
118 return node | 116 return node |
119 | 117 |
120 | 118 |
121 def PrettyPrint(raw_xml): | 119 def PrettyPrint(raw_xml): |
122 """Pretty-print the given XML. | 120 """Pretty-print the given XML. |
123 | 121 |
124 Args: | 122 Args: |
125 raw_xml: The contents of the histograms XML file, as a string. | 123 raw_xml: The contents of the histograms XML file, as a string. |
126 | 124 |
127 Returns: | 125 Returns: |
128 The pretty-printed version. | 126 The pretty-printed version. |
129 """ | 127 """ |
130 tree = xml.dom.minidom.parseString(raw_xml) | 128 tree = xml.dom.minidom.parseString(raw_xml) |
131 tree = TransformByAlphabetizing(tree) | 129 tree = TransformByAlphabetizing(tree) |
132 return print_style.GetPrintStyle().PrettyPrintNode(tree) | 130 return print_style.GetPrintStyle().PrettyPrintNode(tree) |
133 | 131 |
| 132 |
134 def main(): | 133 def main(): |
135 presubmit_util.DoPresubmitMain(sys.argv, 'histograms.xml', | 134 presubmit_util.DoPresubmitMain(sys.argv, 'histograms.xml', |
136 'histograms.before.pretty-print.xml', | 135 'histograms.before.pretty-print.xml', |
137 'pretty_print.py', PrettyPrint) | 136 'pretty_print.py', PrettyPrint) |
138 | 137 |
139 if __name__ == '__main__': | 138 if __name__ == '__main__': |
140 main() | 139 main() |
OLD | NEW |