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

Side by Side Diff: tools/metrics/common/pretty_print_xml.py

Issue 2890013004: Clean up histograms.xml formatting
Patch Set: Created 3 years, 7 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
« no previous file with comments | « tools/metrics/actions/print_style.py ('k') | tools/metrics/histograms/enums.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 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 """Utility file for pretty printing xml file. 5 """Utility file for pretty printing xml file.
6 6
7 The function PrettyPrintXml will be used for formatting both histograms.xml 7 The function PrettyPrintXml will be used for formatting both histograms.xml
8 and actions.xml. 8 and actions.xml.
9 """ 9 """
10 10
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 paragraphs[-1].append(l) 59 paragraphs[-1].append(l)
60 # Remove trailing empty paragraph if present. 60 # Remove trailing empty paragraph if present.
61 if paragraphs and not paragraphs[-1]: 61 if paragraphs and not paragraphs[-1]:
62 paragraphs = paragraphs[:-1] 62 paragraphs = paragraphs[:-1]
63 return ['\n'.join(p) for p in paragraphs] 63 return ['\n'.join(p) for p in paragraphs]
64 64
65 65
66 class XmlStyle(object): 66 class XmlStyle(object):
67 """A class that stores all style specification for an output xml file.""" 67 """A class that stores all style specification for an output xml file."""
68 68
69 def __init__(self, attribute_order, tags_that_have_extra_newline, 69 def __init__(self, attribute_order, required_attributes,
70 tags_that_dont_indent, tags_that_allow_single_line, 70 tags_that_have_extra_newline, tags_that_dont_indent,
71 tags_alphabetization_rules): 71 tags_that_allow_single_line, tags_alphabetization_rules):
72 self.attribute_order = attribute_order 72 self.attribute_order = attribute_order
73 self.required_attributes = required_attributes
73 self.tags_that_have_extra_newline = tags_that_have_extra_newline 74 self.tags_that_have_extra_newline = tags_that_have_extra_newline
74 self.tags_that_dont_indent = tags_that_dont_indent 75 self.tags_that_dont_indent = tags_that_dont_indent
75 self.tags_that_allow_single_line = tags_that_allow_single_line 76 self.tags_that_allow_single_line = tags_that_allow_single_line
76 self.tags_alphabetization_rules = tags_alphabetization_rules 77 self.tags_alphabetization_rules = tags_alphabetization_rules
77 78
78 def PrettyPrintXml(self, tree): 79 def PrettyPrintXml(self, tree):
79 tree = self._TransformByAlphabetizing(tree) 80 tree = self._TransformByAlphabetizing(tree)
80 tree = self.PrettyPrintNode(tree) 81 tree = self.PrettyPrintNode(tree)
81 return tree 82 return tree
82 83
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 newlines_after_open, newlines_before_close, newlines_after_close = ( 193 newlines_after_open, newlines_before_close, newlines_after_close = (
193 self.tags_that_have_extra_newline.get(node.tagName, (1, 1, 0))) 194 self.tags_that_have_extra_newline.get(node.tagName, (1, 1, 0)))
194 # Open the tag. 195 # Open the tag.
195 s = ' ' * indent + '<' + node.tagName 196 s = ' ' * indent + '<' + node.tagName
196 197
197 # Calculate how much space to allow for the '>' or '/>'. 198 # Calculate how much space to allow for the '>' or '/>'.
198 closing_chars = 1 199 closing_chars = 1
199 if not node.childNodes: 200 if not node.childNodes:
200 closing_chars = 2 201 closing_chars = 2
201 202
203 attributes = node.attributes.keys()
204 required_attributes = [attribute for attribute in self.required_attributes
205 if attribute in self.attribute_order[node.tagName]]
206 missing_attributes = [attribute for attribute in required_attributes
207 if attribute not in attributes]
208
209 for attribute in missing_attributes:
210 logging.error(
211 'Missing attribute "%s" in tag "%s"', attribute, node.tagName)
212 if missing_attributes:
213 raise Error(
214 'Missing attributes {0} in tag "{1}"'.format(
215 ', '.join('"{0}"'.format(a) for a in missing_attributes),
Alexei Svitkine (slow) 2017/05/19 20:05:28 Nit: I'd extract this inner statement into a var a
216 node.tagName))
217
202 # Pretty-print the attributes. 218 # Pretty-print the attributes.
203 attributes = node.attributes.keys()
204 if attributes: 219 if attributes:
205 # Reorder the attributes. 220 # Reorder the attributes.
206 unrecognized_attributes = ( 221 unrecognized_attributes = (
207 [a for a in attributes 222 [a for a in attributes
208 if a not in self.attribute_order[node.tagName]]) 223 if a not in self.attribute_order[node.tagName]])
209 attributes = [a for a in self.attribute_order[node.tagName] 224 attributes = [a for a in self.attribute_order[node.tagName]
210 if a in attributes] 225 if a in attributes]
211 226
212 for a in unrecognized_attributes: 227 for a in unrecognized_attributes:
213 logging.error( 228 logging.error(
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 292
278 # Handle comment nodes. 293 # Handle comment nodes.
279 if node.nodeType == xml.dom.minidom.Node.COMMENT_NODE: 294 if node.nodeType == xml.dom.minidom.Node.COMMENT_NODE:
280 return '<!--%s-->\n' % node.data 295 return '<!--%s-->\n' % node.data
281 296
282 # Ignore other node types. This could be a processing instruction 297 # Ignore other node types. This could be a processing instruction
283 # (<? ... ?>) or cdata section (<![CDATA[...]]!>), neither of which are 298 # (<? ... ?>) or cdata section (<![CDATA[...]]!>), neither of which are
284 # legal in the histograms XML at present. 299 # legal in the histograms XML at present.
285 logging.error('Ignoring unrecognized node data: %s', node.toxml()) 300 logging.error('Ignoring unrecognized node data: %s', node.toxml())
286 raise Error('Ignoring unrecognized node data: {0}'.format(node.toxml())) 301 raise Error('Ignoring unrecognized node data: {0}'.format(node.toxml()))
OLDNEW
« no previous file with comments | « tools/metrics/actions/print_style.py ('k') | tools/metrics/histograms/enums.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698