Пример #1
0
 def finish_tree(self, tree, filename):
     if self.found_future_import:
         return
     if not isinstance(tree, pytree.Node):
         # Empty files (usually __init__.py) show up as a single Leaf
         # instead of a Node, so leave them alone
         return
     first_stmt = tree.children[0]
     if is_docstring(first_stmt):
         # Skip a line and add the import after the docstring
         tree.insert_child(1, Newline())
         pos = 2
     elif first_stmt.prefix:
         # No docstring, but an initial comment (perhaps a #! line).
         # Transfer the initial comment to a new blank line.
         newline = Newline()
         newline.prefix = first_stmt.prefix
         first_stmt.prefix = ""
         tree.insert_child(0, newline)
         pos = 1
     else:
         # No comments or docstring, just insert at the start
         pos = 0
     tree.insert_child(pos, self.new_future_import(None))
     tree.insert_child(pos+1, Newline())  # terminates the import stmt
Пример #2
0
 def finish_tree(self, tree, filename):
     if self.found_future_import:
         return
     if not isinstance(tree, pytree.Node):
         # Empty files (usually __init__.py) show up as a single Leaf
         # instead of a Node, so leave them alone
         return
     first_stmt = tree.children[0]
     if is_docstring(first_stmt):
         # Skip a line and add the import after the docstring
         tree.insert_child(1, Newline())
         pos = 2
     elif first_stmt.prefix:
         # No docstring, but an initial comment (perhaps a #! line).
         # Transfer the initial comment to a new blank line.
         newline = Newline()
         newline.prefix = first_stmt.prefix
         first_stmt.prefix = ""
         tree.insert_child(0, newline)
         pos = 1
     else:
         # No comments or docstring, just insert at the start
         pos = 0
     tree.insert_child(pos, self.new_future_import(None))
     tree.insert_child(pos + 1, Newline())  # terminates the import stmt
Пример #3
0
        def _CreateCode(indent, package, symbols, comment):
            """
            Create code:
                from <package> import <symbols> # <comment>
            """
            # children: the children nodes for the final from-import statement
            children = [
                Name('from', prefix=' ' * indent),
                Name(package, prefix=' '),
                Name('import', prefix=' '),
            ]

            # name_leaf: list of leaf nodes with the symbols to import
            name_leafs = []
            symbols = sorted(symbols)
            for i, i_symbol in enumerate(symbols):
                prefix = ' ' if i == 0 else ', '
                name_leafs.append(i_symbol.CreateNameNode(prefix))

            # nodes_wrap: if true, we need to wrap the import statement
            nodes_wrap = False
            line_len = 0
            line_len += six.moves.reduce(lambda x, y: x + y,
                                         map(len, map(str, children)), 0)
            line_len += six.moves.reduce(lambda x, y: x + y,
                                         map(len, map(str, name_leafs)), 0)
            if line_len > page_width:
                # Add parenthesis around the "from" names
                name_leafs[0].prefix = ''
                name_leafs.insert(0, Name('(', prefix=' '))
                name_leafs.append(Name(')'))
                nodes_wrap = True

            # Adds the name_leafs to the children list
            children += [
                Node(pygram.python_symbols.import_as_names, name_leafs)
            ]

            # from_import: the final node for the import statement
            from_import = Node(pygram.python_symbols.import_from, children)

            # result: a simple-statement node with the import statement and
            # EOL.
            new_line = Newline()
            new_line.prefix = comment
            result = Node(
                pygram.python_symbols.simple_stmt,
                children=[
                    from_import,
                    new_line,
                ],
            )

            # Wrap nodes if necessary (see nodes_wrap above)
            if nodes_wrap:
                ImportBlock.TextWrapForNode(result, page_width, indent)

            return result
Пример #4
0
        def _CreateCode(indent, package, symbols, comment):
            '''
            Create code:
                from <package> import <symbols> # <comment>
            '''
            # children: the children nodes for the final from-import statement
            children = [
                Name('from', prefix=' ' * indent),
                Name(package, prefix=' '),
                Name('import', prefix=' '),
            ]

            # name_leaf: list of leaf nodes with the symbols to import
            name_leafs = []
            symbols = sorted(symbols)
            for i, i_symbol in enumerate(symbols):
                prefix = ' ' if i == 0 else ', '
                name_leafs.append(i_symbol.CreateNameNode(prefix))

            # nodes_wrap: if true, we need to wrap the import statement
            nodes_wrap = False
            line_len = 0
            line_len += reduce(lambda x, y:x + y, map(len, map(str, children)), 0)
            line_len += reduce(lambda x, y:x + y, map(len, map(str, name_leafs)), 0)
            if line_len > page_width:
                # Add parenthesis around the "from" names
                name_leafs[0].prefix = ''
                name_leafs.insert(0, Name('(', prefix=' '))
                name_leafs.append(Name(')'))
                nodes_wrap = True

            # Adds the name_leafs to the children list
            children += [Node(pygram.python_symbols.import_as_names, name_leafs)]

            # from_import: the final node for the import statement
            from_import = Node(pygram.python_symbols.import_from, children)

            # result: a simple-statement node with the import statement and EOL.
            new_line = Newline()
            new_line.prefix = comment
            result = Node(
                pygram.python_symbols.simple_stmt,
                children=[
                    from_import,
                    new_line,
                ],
            )

            # Wrap nodes if necessary (see nodes_wrap above)
            if nodes_wrap:
                ImportBlock.TextWrapForNode(result, page_width, indent)

            return result
Пример #5
0
    def CreateCode(self, indent, page_width):
        from lib2to3 import pygram
        from lib2to3.fixer_util import Name, Newline
        from lib2to3.pytree import Node

        assert self.kind == self.KIND_IMPORT_NAME

        name_node = self.CreateNameNode()
        new_line = Newline()
        new_line.prefix = self.comment
        result = Node(pygram.python_symbols.simple_stmt,
                      prefix=' ' * indent,
                      children=[Name('import'), name_node, new_line])
        return [result]
Пример #6
0
    def CreateCode(self, indent, page_width):
        from lib2to3 import pygram
        from lib2to3.fixer_util import Name, Newline
        from lib2to3.pytree import Node

        assert self.kind == self.KIND_IMPORT_NAME

        name_node = self.CreateNameNode()
        new_line = Newline()
        new_line.prefix = self.comment
        result = Node(
            pygram.python_symbols.simple_stmt,
            prefix=' ' * indent,
            children=[Name('import'), name_node, new_line]
        )
        return [result]
Пример #7
0
def add_future_import(tree, name):
    """Add future import.

    From: https://github.com/facebook/tornado

    Copyright 2009 Facebook

    Licensed under the Apache License, Version 2.0 (the "License"); you may
    not use this file except in compliance with the License. You may obtain
    a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    License for the specific language governing permissions and limitations
    under the License.

    """
    if not isinstance(tree, Node):
        # Empty files (usually __init__.py) show up as a single Leaf
        # instead of a Node, so leave them alone
        return

    first_stmt = tree.children[0]
    if is_docstring(first_stmt):
        # Skip a line and add the import after the docstring
        tree.insert_child(1, Newline())
        pos = 2
    elif first_stmt.prefix:
        # No docstring, but an initial comment (perhaps a #! line).
        # Transfer the initial comment to a new blank line.
        newline = Newline()
        newline.prefix = first_stmt.prefix
        first_stmt.prefix = ''
        tree.insert_child(0, newline)
        pos = 1
    else:
        # No comments or docstring, just insert at the start
        pos = 0
    tree.insert_child(
        pos,
        FromImport('__future__', [Name(name, prefix=' ')]))
    tree.insert_child(pos + 1, Newline())  # terminates the import stmt