Index: tools/metrics/histograms/pretty_print.py |
diff --git a/tools/metrics/histograms/pretty_print.py b/tools/metrics/histograms/pretty_print.py |
index 60e8c7833e59e5f0020d3188881526e0fe462385..f5d15da35065b859f7f9ac4b1a60544b6385b2e9 100755 |
--- a/tools/metrics/histograms/pretty_print.py |
+++ b/tools/metrics/histograms/pretty_print.py |
@@ -15,18 +15,19 @@ and wrapping text nodes, so we implement our own full custom XML pretty-printer. |
from __future__ import with_statement |
import diffutil |
Alexei Svitkine (slow)
2014/02/11 19:05:31
Should this be removed?
yao
2014/02/13 15:58:08
Done.
|
-import json |
import logging |
import os |
import shutil |
import sys |
-import textwrap |
import xml.dom.minidom |
sys.path.insert(1, os.path.join(sys.path[0], '..', '..', 'python')) |
from google import path_utils |
-WRAP_COLUMN = 80 |
+# Import the metrics/common module for pretty print xml. |
+sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) |
+import diffutil |
Alexei Svitkine (slow)
2014/02/11 19:05:31
Isn't this changed to diff_util?
But I don't see
yao
2014/02/13 15:58:08
This worked because of the .pyc file. So it's stil
|
+import pretty_print_xml |
# Desired order for tag attributes; attributes listed here will appear first, |
# and in the same order as in these lists. |
@@ -84,165 +85,6 @@ class Error(Exception): |
pass |
-def LastLineLength(s): |
- """Returns the length of the last line in s. |
- |
- Args: |
- s: A multi-line string, including newlines. |
- |
- Returns: |
- The length of the last line in s, in characters. |
- """ |
- if s.rfind('\n') == -1: return len(s) |
- return len(s) - s.rfind('\n') - len('\n') |
- |
- |
-def XmlEscape(s): |
- """XML-escapes the given string, replacing magic characters (&<>") with their |
- escaped equivalents.""" |
- s = s.replace("&", "&").replace("<", "<") |
- s = s.replace("\"", """).replace(">", ">") |
- return s |
- |
- |
-def PrettyPrintNode(node, indent=0): |
- """Pretty-prints the given XML node at the given indent level. |
- |
- Args: |
- node: The minidom node to pretty-print. |
- indent: The current indent level. |
- |
- Returns: |
- The pretty-printed string (including embedded newlines). |
- |
- Raises: |
- Error if the XML has unknown tags or attributes. |
- """ |
- # Handle the top-level document node. |
- if node.nodeType == xml.dom.minidom.Node.DOCUMENT_NODE: |
- return '\n'.join([PrettyPrintNode(n) for n in node.childNodes]) |
- |
- # Handle text nodes. |
- if node.nodeType == xml.dom.minidom.Node.TEXT_NODE: |
- # Wrap each paragraph in the text to fit in the 80 column limit. |
- wrapper = textwrap.TextWrapper() |
- wrapper.initial_indent = ' ' * indent |
- wrapper.subsequent_indent = ' ' * indent |
- wrapper.break_on_hyphens = False |
- wrapper.break_long_words = False |
- wrapper.width = WRAP_COLUMN |
- text = XmlEscape(node.data) |
- # Remove any common indent. |
- text = textwrap.dedent(text.strip('\n')) |
- lines = text.split('\n') |
- # Split the text into paragraphs at blank line boundaries. |
- paragraphs = [[]] |
- for l in lines: |
- if len(l.strip()) == 0 and len(paragraphs[-1]) > 0: |
- paragraphs.append([]) |
- else: |
- paragraphs[-1].append(l) |
- # Remove trailing empty paragraph if present. |
- if len(paragraphs) > 0 and len(paragraphs[-1]) == 0: |
- paragraphs = paragraphs[:-1] |
- # Wrap each paragraph and separate with two newlines. |
- return '\n\n'.join([wrapper.fill('\n'.join(p)) for p in paragraphs]) |
- |
- # Handle element nodes. |
- if node.nodeType == xml.dom.minidom.Node.ELEMENT_NODE: |
- newlines_after_open, newlines_before_close, newlines_after_close = ( |
- TAGS_THAT_HAVE_EXTRA_NEWLINE.get(node.tagName, (1, 1, 0))) |
- # Open the tag. |
- s = ' ' * indent + '<' + node.tagName |
- |
- # Calculate how much space to allow for the '>' or '/>'. |
- closing_chars = 1 |
- if not node.childNodes: |
- closing_chars = 2 |
- |
- # Pretty-print the attributes. |
- attributes = node.attributes.keys() |
- if attributes: |
- # Reorder the attributes. |
- if not node.tagName in ATTRIBUTE_ORDER: |
- unrecognized_attributes = attributes; |
- else: |
- unrecognized_attributes = ( |
- [a for a in attributes if not a in ATTRIBUTE_ORDER[node.tagName]]) |
- attributes = ( |
- [a for a in ATTRIBUTE_ORDER[node.tagName] if a in attributes]) |
- |
- for a in unrecognized_attributes: |
- logging.error( |
- 'Unrecognized attribute "%s" in tag "%s"' % (a, node.tagName)) |
- if unrecognized_attributes: |
- raise Error() |
- |
- for a in attributes: |
- value = XmlEscape(node.attributes[a].value) |
- # Replace sequences of whitespace with single spaces. |
- words = value.split() |
- a_str = ' %s="%s"' % (a, ' '.join(words)) |
- # Start a new line if the attribute will make this line too long. |
- if LastLineLength(s) + len(a_str) + closing_chars > WRAP_COLUMN: |
- s += '\n' + ' ' * (indent + 3) |
- # Output everything up to the first quote. |
- s += ' %s="' % (a) |
- value_indent_level = LastLineLength(s) |
- # Output one word at a time, splitting to the next line where necessary. |
- column = value_indent_level |
- for i, word in enumerate(words): |
- # This is slightly too conservative since not every word will be |
- # followed by the closing characters... |
- if i > 0 and (column + len(word) + 1 + closing_chars > WRAP_COLUMN): |
- s = s.rstrip() # remove any trailing whitespace |
- s += '\n' + ' ' * value_indent_level |
- column = value_indent_level |
- s += word + ' ' |
- column += len(word) + 1 |
- s = s.rstrip() # remove any trailing whitespace |
- s += '"' |
- s = s.rstrip() # remove any trailing whitespace |
- |
- # Pretty-print the child nodes. |
- if node.childNodes: |
- s += '>' |
- # Calculate the new indent level for child nodes. |
- new_indent = indent |
- if node.tagName not in TAGS_THAT_DONT_INDENT: |
- new_indent += 2 |
- child_nodes = node.childNodes |
- |
- # Recursively pretty-print the child nodes. |
- child_nodes = [PrettyPrintNode(n, indent=new_indent) for n in child_nodes] |
- child_nodes = [c for c in child_nodes if len(c.strip()) > 0] |
- |
- # Determine whether we can fit the entire node on a single line. |
- close_tag = '</%s>' % node.tagName |
- space_left = WRAP_COLUMN - LastLineLength(s) - len(close_tag) |
- if (node.tagName in TAGS_THAT_ALLOW_SINGLE_LINE and |
- len(child_nodes) == 1 and len(child_nodes[0].strip()) <= space_left): |
- s += child_nodes[0].strip() |
- else: |
- s += '\n' * newlines_after_open + '\n'.join(child_nodes) |
- s += '\n' * newlines_before_close + ' ' * indent |
- s += close_tag |
- else: |
- s += '/>' |
- s += '\n' * newlines_after_close |
- return s |
- |
- # Handle comment nodes. |
- if node.nodeType == xml.dom.minidom.Node.COMMENT_NODE: |
- return '<!--%s-->\n' % node.data |
- |
- # Ignore other node types. This could be a processing instruction (<? ... ?>) |
- # or cdata section (<![CDATA[...]]!>), neither of which are legal in the |
- # histograms XML at present. |
- logging.error('Ignoring unrecognized node data: %s' % node.toxml()) |
- raise Error() |
- |
- |
def unsafeAppendChild(parent, child): |
"""Append child to parent's list of children, ignoring the possibility that it |
is already in another node's childNodes list. Requires that the previous |
@@ -311,7 +153,11 @@ def PrettyPrint(raw_xml): |
""" |
tree = xml.dom.minidom.parseString(raw_xml) |
tree = TransformByAlphabetizing(tree) |
- return PrettyPrintNode(tree) |
+ xml_style = pretty_print_xml.XmlStyle(ATTRIBUTE_ORDER, |
+ TAGS_THAT_HAVE_EXTRA_NEWLINE, |
+ TAGS_THAT_DONT_INDENT, |
+ TAGS_THAT_ALLOW_SINGLE_LINE) |
+ return xml_style.PrettyPrintNode(tree) |
def main(): |