Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: tools/metrics/histograms/pretty_print.py

Issue 1143323006: Histograms.xml python script housekeeping (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bad_message
Patch Set: Small whitespace adjustment Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 23 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) # tools/metrics
24 24 from common import diff_util
25 sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'python')) 25 from common import presubmit_util
26 from google import path_utils 26 from histograms import print_style
27
28 # Import the metrics/common module.
29 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
30 import diff_util
31 import presubmit_util
32 27
33 # Tags whose children we want to alphabetize. The key is the parent tag name, 28 # 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, 29 # 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. 30 # and a key function that maps each child node to the desired sort key.
36 ALPHABETIZATION_RULES = { 31 ALPHABETIZATION_RULES = {
37 'histograms': ('histogram', lambda n: n.attributes['name'].value.lower()), 32 'histograms': ('histogram', lambda n: n.attributes['name'].value.lower()),
38 'enums': ('enum', lambda n: n.attributes['name'].value.lower()), 33 'enums': ('enum', lambda n: n.attributes['name'].value.lower()),
39 'enum': ('int', lambda n: int(n.attributes['value'].value)), 34 'enum': ('int', lambda n: int(n.attributes['value'].value)),
40 'histogram_suffixes_list': ( 35 'histogram_suffixes_list': (
41 'histogram_suffixes', lambda n: n.attributes['name'].value.lower()), 36 'histogram_suffixes', lambda n: n.attributes['name'].value.lower()),
(...skipping 24 matching lines...) Expand all
66 61
67 Args: 62 Args:
68 node: The minidom node to transform. 63 node: The minidom node to transform.
69 64
70 Returns: 65 Returns:
71 The minidom node, with children appropriately alphabetized. Note that the 66 The minidom node, with children appropriately alphabetized. Note that the
72 transformation is done in-place, i.e. the original minidom tree is modified 67 transformation is done in-place, i.e. the original minidom tree is modified
73 directly. 68 directly.
74 """ 69 """
75 if node.nodeType != xml.dom.minidom.Node.ELEMENT_NODE: 70 if node.nodeType != xml.dom.minidom.Node.ELEMENT_NODE:
76 for c in node.childNodes: TransformByAlphabetizing(c) 71 for c in node.childNodes:
72 TransformByAlphabetizing(c)
77 return node 73 return node
78 74
79 # Element node with a tag name that we alphabetize the children of? 75 # Element node with a tag name that we alphabetize the children of?
80 if node.tagName in ALPHABETIZATION_RULES: 76 if node.tagName in ALPHABETIZATION_RULES:
81 # Put subnodes in a list of node,key pairs to allow for custom sorting. 77 # Put subnodes in a list of node,key pairs to allow for custom sorting.
82 subtag, key_function = ALPHABETIZATION_RULES[node.tagName] 78 subtag, key_function = ALPHABETIZATION_RULES[node.tagName]
83 subnodes = [] 79 subnodes = []
84 sort_key = -1 80 sort_key = -1
85 pending_node_indices = [] 81 pending_node_indices = []
86 for c in node.childNodes: 82 for c in node.childNodes:
(...skipping 20 matching lines...) Expand all
107 subnodes.sort(key=lambda pair: pair[1]) 103 subnodes.sort(key=lambda pair: pair[1])
108 104
109 # Re-add the subnodes, transforming each recursively. 105 # Re-add the subnodes, transforming each recursively.
110 while node.firstChild: 106 while node.firstChild:
111 node.removeChild(node.firstChild) 107 node.removeChild(node.firstChild)
112 for (c, _) in subnodes: 108 for (c, _) in subnodes:
113 unsafeAppendChild(node, TransformByAlphabetizing(c)) 109 unsafeAppendChild(node, TransformByAlphabetizing(c))
114 return node 110 return node
115 111
116 # Recursively handle other element nodes and other node types. 112 # Recursively handle other element nodes and other node types.
117 for c in node.childNodes: TransformByAlphabetizing(c) 113 for c in node.childNodes:
114 TransformByAlphabetizing(c)
118 return node 115 return node
119 116
120 117
121 def PrettyPrint(raw_xml): 118 def PrettyPrint(raw_xml):
122 """Pretty-print the given XML. 119 """Pretty-print the given XML.
123 120
124 Args: 121 Args:
125 raw_xml: The contents of the histograms XML file, as a string. 122 raw_xml: The contents of the histograms XML file, as a string.
126 123
127 Returns: 124 Returns:
128 The pretty-printed version. 125 The pretty-printed version.
129 """ 126 """
130 tree = xml.dom.minidom.parseString(raw_xml) 127 tree = xml.dom.minidom.parseString(raw_xml)
131 tree = TransformByAlphabetizing(tree) 128 tree = TransformByAlphabetizing(tree)
132 return print_style.GetPrintStyle().PrettyPrintNode(tree) 129 return print_style.GetPrintStyle().PrettyPrintNode(tree)
133 130
131
134 def main(): 132 def main():
135 presubmit_util.DoPresubmitMain(sys.argv, 'histograms.xml', 133 presubmit_util.DoPresubmitMain(sys.argv, 'histograms.xml',
136 'histograms.before.pretty-print.xml', 134 'histograms.before.pretty-print.xml',
137 'pretty_print.py', PrettyPrint) 135 'pretty_print.py', PrettyPrint)
138 136
139 if __name__ == '__main__': 137 if __name__ == '__main__':
140 main() 138 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698