Esempio n. 1
0
 def transform(self, node, results):
     # If there is one or more newline between __future__ import and
     # the next line, we want to remove them.
     node.next_sibling.prefix = node.next_sibling.prefix.lstrip()
     node.next_sibling.changed()
     new = BlankLine()
     new.prefix = node.prefix
     return new
Esempio n. 2
0
        def t(imp):
            new_contents = []
        
            def handle_one(n):
                if n.type == token.NAME:
                    if "Orange." not in n.value:
                        new_contents.append(n)
                    else:
                        touch_import(None, "Orange", node)
                else:
                    new_contents.append(n)

            if imp.type == syms.dotted_as_names:
                for n in imp.children[::2]:
                    handle_one(n)
            else:
                handle_one(imp)
    
            #copy prefix, so you do not lose comments
            opref = node.prefix
            if new_contents:
                nn = Import(new_contents)
                nn.prefix = opref
            else:
                nn = BlankLine()
                if opref and opref[-1] in ["\n"]: #remove previous newline
                    opref = opref[:-1]
                nn.prefix = opref
        
            return nn
 def transform(self, node, results):
     if isinstance(node, Node) and node.type == python_symbols.file_input:
         for child in node.children:
             if child.type == python_symbols.simple_stmt:
                 if child.children and child.children[
                         0].type == token.STRING:
                     self.docstring = child.children[0].value
                     child.replace(BlankLine())
Esempio n. 4
0
    def transform(self, node, results):
        imports = results['imports']
        if imports.type == syms.import_as_name or not imports.children:
            children = [imports]
        else:
            children = imports.children
        for child in children[::2]:
            if child.type == token.NAME:
                member = child.value
                name_node = child
            elif child.type == token.STAR:
                # Just leave the import as is.
                return
            else:
                assert child.type == syms.import_as_name
                name_node = child.children[0]
            member_name = name_node.value
            if member_name in ('imap', 'izip', 'ifilter'):
                child.value = None
                child.remove()
            elif member_name in ('ifilterfalse', 'izip_longest'):
                node.changed()
                name_node.value = ('filterfalse'
                                   if member_name[1] == 'f' else 'zip_longest')

        # Make sure the import statement is still sane
        children = imports.children[:] or [imports]
        remove_comma = True
        for child in children:
            if remove_comma and child.type == token.COMMA:
                child.remove()
            else:
                remove_comma ^= True

        while children and children[-1].type == token.COMMA:
            children.pop().remove()

        # If there are no imports left, just get rid of the entire statement
        if (not (imports.children or getattr(imports, 'value', None))
                or imports.parent is None):
            p = node.prefix
            node = BlankLine()
            node.prefix = p
            return node
    def transform(self, node, results):
        imports = results['imports']
        if imports.type == syms.import_as_name or not imports.children:
            children = [imports]
        else:
            children = imports.children
        for child in children[::2]:
            if child.type == token.NAME:
                member = child.value
                name_node = child
            elif child.type == token.STAR:
                # Just leave the import as is.
                return
            else:
                assert child.type == syms.import_as_name
                name_node = child.children[0]
            member_name = name_node.value
            if member_name in ('imap', 'izip', 'ifilter'):
                child.value = None
                child.remove()
            elif member_name in ('ifilterfalse', 'izip_longest'):
                node.changed()
                name_node.value = ('filterfalse' if member_name[1] == 'f'
                                   else 'zip_longest')

        # Make sure the import statement is still sane
        children = imports.children[:] or [imports]
        remove_comma = True
        for child in children:
            if remove_comma and child.type == token.COMMA:
                child.remove()
            else:
                remove_comma ^= True

        while children and children[-1].type == token.COMMA:
            children.pop().remove()

        # If there are no imports left, just get rid of the entire statement
        if (not (imports.children or getattr(imports, 'value', None)) or
            imports.parent is None):
            p = node.prefix
            node = BlankLine()
            node.prefix = p
            return node
    def transform(self, node, results):
        imports = results['imports']
        if imports.type == syms.import_as_name or not imports.children:
            children = [imports]
        else:
            children = imports.children
        for child in children[::2]:
            if child.type == token.NAME:
                member = child.value
                name_node = child
            else:
                if child.type == token.STAR:
                    return
                raise child.type == syms.import_as_name or AssertionError
                name_node = child.children[0]
            member_name = name_node.value
            if member_name in (u'imap', u'izip', u'ifilter'):
                child.value = None
                child.remove()
            elif member_name in (u'ifilterfalse', u'izip_longest'):
                node.changed()
                name_node.value = u'filterfalse' if member_name[
                    1] == u'f' else u'zip_longest'

        children = imports.children[:] or [imports]
        remove_comma = True
        for child in children:
            if remove_comma and child.type == token.COMMA:
                child.remove()
            else:
                remove_comma ^= True

        while children and children[-1].type == token.COMMA:
            children.pop().remove()

        if not (imports.children
                or getattr(imports, 'value', None)) or imports.parent is None:
            p = node.prefix
            node = BlankLine()
            node.prefix = p
            return node
        else:
            return
 def transform(self, node, result):
     suite = next(
         (x for x in node.children if x.type == python_symbols.suite), None)
     if not suite: return
     statements = [
         x for x in suite.children if x.type == python_symbols.simple_stmt
     ]
     if len(statements) != 1: return
     if not self.PATTERN_MAIN.match(statements[0].children[0], {}): return
     return BlankLine()
    def transform(self, node, results):
        imports = results['imports']
        if imports.type == syms.import_as_name or not imports.children:
            children = [imports]
        else:
            children = imports.children
        for child in children[::2]:
            if child.type == token.NAME:
                member = child.value
                name_node = child
            else:
                if child.type == token.STAR:
                    return
                raise child.type == syms.import_as_name or AssertionError
                name_node = child.children[0]
            member_name = name_node.value
            if member_name in (u'imap', u'izip', u'ifilter'):
                child.value = None
                child.remove()
            elif member_name in (u'ifilterfalse', u'izip_longest'):
                node.changed()
                name_node.value = u'filterfalse' if member_name[1] == u'f' else u'zip_longest'

        children = imports.children[:] or [imports]
        remove_comma = True
        for child in children:
            if remove_comma and child.type == token.COMMA:
                child.remove()
            else:
                remove_comma ^= True

        while children and children[-1].type == token.COMMA:
            children.pop().remove()

        if not (imports.children or getattr(imports, 'value', None)) or imports.parent is None:
            p = node.prefix
            node = BlankLine()
            node.prefix = p
            return node
        else:
            return
    def transform(self, node, results):
        imports = results['imports']
        if imports.type == syms.import_as_name or not imports.children:
            children = [imports]
        else:
            children = imports.children
        for child in children[::2]:
            if child.type == token.NAME:
                member = child.value
                name_node = child
            else:
                assert child.type == syms.import_as_name
                name_node = child.children[0]
            member_name = name_node.value
            if member_name in (u'imap', u'izip', u'ifilter'):
                child.value = None
                child.remove()
            elif member_name == u'ifilterfalse':
                node.changed()
                name_node.value = u'filterfalse'

        # Make sure the import statement is still sane
        children = imports.children[:] or [imports]
        remove_comma = True
        for child in children:
            if remove_comma and child.type == token.COMMA:
                child.remove()
            else:
                remove_comma ^= True

        if children[-1].type == token.COMMA:
            children[-1].remove()

        # If there are no imports left, just get rid of the entire statement
        if not (imports.children or getattr(imports, 'value', None)) or \
                imports.parent is None:
            p = node.get_prefix()
            node = BlankLine()
            node.prefix = p
        return node
 def transform(self, node, results):
     passed_import = False
     for child in node.children:
         if isinstance(
                 child, Leaf
         ) and child.type == token.NAME and child.value == 'import':
             passed_import = True
             continue
         if not passed_import:
             continue
         if isinstance(
                 child,
                 Node) and child.type == python_symbols.import_as_names:
             # from x import a, b
             for leaf in child.children:
                 if leaf.type == token.NAME and leaf.value not in self.imports:
                     self.imports.append(leaf.value)
         elif isinstance(child, Leaf) and child.type == token.NAME:
             # from x import a
             if child.value not in self.imports:
                 self.imports.append(child.value)
     new = BlankLine()
     new.prefix = node.prefix
     return new
Esempio n. 11
0
 def transform(self, node, results):
     new = BlankLine()
     new.prefix = node.prefix
     return new