Ejemplo n.º 1
0
def _ParseAndUnwrap(code, dumptree=False):
    """Produces unwrapped lines from the given code.

  Parses the code into a tree, performs comment splicing and runs the
  unwrapper.

  Arguments:
    code: code to parse as a string
    dumptree: if True, the parsed pytree (after comment splicing) is dumped
      to stderr. Useful for debugging.

  Returns:
    List of unwrapped lines.
  """
    style.SetGlobalStyle(style.CreateGoogleStyle())
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
    subtype_assigner.AssignSubtypes(tree)
    split_penalty.ComputeSplitPenalties(tree)
    blank_line_calculator.CalculateBlankLines(tree)

    if dumptree:
        pytree_visitor.DumpPyTree(tree, target_stream=sys.stderr)

    uwlines = pytree_unwrapper.UnwrapPyTree(tree)
    for uwl in uwlines:
        uwl.CalculateFormattingInformation()

    return uwlines
Ejemplo n.º 2
0
def ParseAndUnwrap(code, dumptree=False):
    """Produces logical lines from the given code.

  Parses the code into a tree, performs comment splicing and runs the
  unwrapper.

  Arguments:
    code: code to parse as a string
    dumptree: if True, the parsed pytree (after comment splicing) is dumped
              to stderr. Useful for debugging.

  Returns:
    List of logical lines.
  """
    tree = pytree_utils.ParseCodeToTree(code)
    comment_splicer.SpliceComments(tree)
    continuation_splicer.SpliceContinuations(tree)
    subtype_assigner.AssignSubtypes(tree)
    identify_container.IdentifyContainers(tree)
    split_penalty.ComputeSplitPenalties(tree)
    blank_line_calculator.CalculateBlankLines(tree)

    if dumptree:
        pytree_visitor.DumpPyTree(tree, target_stream=sys.stderr)

    llines = pytree_unwrapper.UnwrapPyTree(tree)
    for lline in llines:
        lline.CalculateFormattingInformation()

    return llines
Ejemplo n.º 3
0
    def testDumpPyTree(self):
        # Similar sanity checking for the convenience wrapper DumpPyTree
        tree = pytree_utils.ParseCodeToTree(_VISITOR_TEST_SIMPLE_CODE)
        stream = py3compat.StringIO()
        pytree_visitor.DumpPyTree(tree, target_stream=stream)

        dump_output = stream.getvalue()
        self.assertIn('file_input [3 children]', dump_output)
        self.assertIn("NAME(Leaf(1, 'foo'))", dump_output)
        self.assertIn("EQUAL(Leaf(22, '='))", dump_output)
Ejemplo n.º 4
0
  def _ParseAndComputePenalties(self, code, dumptree=False):
    """Parses the code and computes split penalties.

    Arguments:
      code: code to parse as a string
      dumptree: if True, the parsed pytree (after penalty assignment) is dumped
        to stderr. Useful for debugging.

    Returns:
      Parse tree.
    """
    tree = pytree_utils.ParseCodeToTree(code)
    split_penalty.ComputeSplitPenalties(tree)
    if dumptree:
      pytree_visitor.DumpPyTree(tree, target_stream=sys.stderr)
    return tree
Ejemplo n.º 5
0
    def _ParseAndUnwrap(self, code, dumptree=False):
        """Produces unwrapped lines from the given code.

    Parses the code into a tree, assigns subtypes and runs the unwrapper.

    Arguments:
      code: code to parse as a string
      dumptree: if True, the parsed pytree (after comment splicing) is dumped
        to stderr. Useful for debugging.

    Returns:
      List of unwrapped lines.
    """
        tree = pytree_utils.ParseCodeToTree(code)
        subtype_assigner.AssignSubtypes(tree)

        if dumptree:
            pytree_visitor.DumpPyTree(tree, target_stream=sys.stderr)

        return pytree_unwrapper.UnwrapPyTree(tree)