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
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, 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
def transform(self, node, results): new = BlankLine() new.prefix = node.prefix return new