tools/metrics/common/pretty_print_xml.py
def _TransformByAlphabetizing(self, node):
    """Transform the given XML by alphabetizing nodes.

    Args:
      node: The elementtree node to transform.

    Returns:
      The elementtree node, with children appropriately alphabetized. Note that
      the transformation is done in-place, i.e. the original tree is modified
      directly.
    """
    # Element node with a tag name that we alphabetize the children of?
    alpha_rules = self.tags_alphabetization_rules
    if node.tag in alpha_rules:
      # Put subnodes in a list of node, key pairs to allow for custom sorting.
      subtags = {}
      for index, (subtag, key_function) in enumerate(alpha_rules[node.tag]):
        subtags[subtag] = (index, key_function)

      subnodes = []
      sort_key = -1
      pending_node_indices = []
      for c in node:
        if c.tag in subtags:
          subtag_sort_index, key_function = subtags[c.tag]
          sort_key = (subtag_sort_index, key_function(c))
          # Replace sort keys for delayed nodes.
          for idx in pending_node_indices:
            subnodes[idx][1] = sort_key
          pending_node_indices = []
        else:
          # Subnodes that we don't want to rearrange use the next node's key,
          # so they stay in the same relative position.
          # Therefore we delay setting key until the next node is found.
          pending_node_indices.append(len(subnodes))
        subnodes.append([c, sort_key])

      # Sort the subnode list.
      subnodes.sort(key=lambda pair: pair[1])

      # Remove the existing nodes
      for child in list(node):
        node.remove(child)

      # Re-add the sorted subnodes, transforming each recursively.
      for (c, _) in subnodes:
        node.append(self._TransformByAlphabetizing(c))
      return node

    # Recursively handle other element nodes and other node types.
    for c in node:
      self._TransformByAlphabetizing(c)
    return node
Transform the given XML by alphabetizing nodes.

    Args:
      node: The elementtree node to transform.

    Returns:
      The elementtree node, with children appropriately alphabetized. Note that
      the transformation is done in-place, i.e. the original tree is modified
      directly.
    
Transform the given XML by alphabetizing nodes.
Transform the given XML by alphabetizing nodes .
def _TransformByAlphabetizing(self, node):
    alpha_rules = self.tags_alphabetization_rules
    if node.tag in alpha_rules:
      subtags = {}
      for index, (subtag, key_function) in enumerate(alpha_rules[node.tag]):
        subtags[subtag] = (index, key_function)
      subnodes = []
      sort_key = -1
      pending_node_indices = []
      for c in node:
        if c.tag in subtags:
          subtag_sort_index, key_function = subtags[c.tag]
          sort_key = (subtag_sort_index, key_function(c))
          for idx in pending_node_indices:
            subnodes[idx][1] = sort_key
          pending_node_indices = []
        else:
          pending_node_indices.append(len(subnodes))
        subnodes.append([c, sort_key])
      subnodes.sort(key=lambda pair: pair[1])
      for child in list(node):
        node.remove(child)
      for (c, _) in subnodes:
        node.append(self._TransformByAlphabetizing(c))
      return node
    for c in node:
      self._TransformByAlphabetizing(c)
    return node
def _TransformByAlphabetizing ( self , node ) : alpha_rules = self . tags_alphabetization_rules if node . tag in alpha_rules : subtags = { } for index , ( subtag , key_function ) in enumerate ( alpha_rules [ node . tag ] ) : subtags [ subtag ] = ( index , key_function ) subnodes = [ ] sort_key = - 1 pending_node_indices = [ ] for c in node : if c . tag in subtags : subtag_sort_index , key_function = subtags [ c . tag ] sort_key = ( subtag_sort_index , key_function ( c ) ) for idx in pending_node_indices : subnodes [ idx ] [ 1 ] = sort_key pending_node_indices = [ ] else : pending_node_indices . append ( len ( subnodes ) ) subnodes . append ( [ c , sort_key ] ) subnodes . sort ( key = lambda pair : pair [ 1 ] ) for child in list ( node ) : node . remove ( child ) for ( c , _ ) in subnodes : node . append ( self . _TransformByAlphabetizing ( c ) ) return node for c in node : self . _TransformByAlphabetizing ( c ) return node
Transform the given XML by alphabetizing nodes.
Transform the given XML by alphabetizing nodes .
"""Transform the given XML by alphabetizing nodes.

    Args:
      node: The elementtree node to transform.

    Returns:
      The elementtree node, with children appropriately alphabetized. Note that
      the transformation is done in-place, i.e. the original tree is modified
      directly.
    """ # Element node with a tag name that we alphabetize the children of? # Put subnodes in a list of node, key pairs to allow for custom sorting. # Replace sort keys for delayed nodes. # Subnodes that we don't want to rearrange use the next node's key, # so they stay in the same relative position. # Therefore we delay setting key until the next node is found. # Sort the subnode list. # Remove the existing nodes # Re-add the sorted subnodes, transforming each recursively. # Recursively handle other element nodes and other node types.
The elementtree node, with children appropriately alphabetized. Note that
the transformation is done in-place, i.e. the original tree is modified
directly.
The elementtree node with children appropriately alphabetized . Note that the transformation is done in - place i . e . the original tree is modified directly .
self
node
The elementtree node to transform.
The elementtree node to transform .