Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3..../lib2to3/fixes
File: fix_itertools_imports.py
""" Fixer for imports of itertools.(imap|ifilter|izip|ifilterfalse) """
[0] Fix | Delete
[1] Fix | Delete
# Local imports
[2] Fix | Delete
from lib2to3 import fixer_base
[3] Fix | Delete
from lib2to3.fixer_util import BlankLine, syms, token
[4] Fix | Delete
[5] Fix | Delete
[6] Fix | Delete
class FixItertoolsImports(fixer_base.BaseFix):
[7] Fix | Delete
BM_compatible = True
[8] Fix | Delete
PATTERN = """
[9] Fix | Delete
import_from< 'from' 'itertools' 'import' imports=any >
[10] Fix | Delete
""" %(locals())
[11] Fix | Delete
[12] Fix | Delete
def transform(self, node, results):
[13] Fix | Delete
imports = results['imports']
[14] Fix | Delete
if imports.type == syms.import_as_name or not imports.children:
[15] Fix | Delete
children = [imports]
[16] Fix | Delete
else:
[17] Fix | Delete
children = imports.children
[18] Fix | Delete
for child in children[::2]:
[19] Fix | Delete
if child.type == token.NAME:
[20] Fix | Delete
member = child.value
[21] Fix | Delete
name_node = child
[22] Fix | Delete
elif child.type == token.STAR:
[23] Fix | Delete
# Just leave the import as is.
[24] Fix | Delete
return
[25] Fix | Delete
else:
[26] Fix | Delete
assert child.type == syms.import_as_name
[27] Fix | Delete
name_node = child.children[0]
[28] Fix | Delete
member_name = name_node.value
[29] Fix | Delete
if member_name in ('imap', 'izip', 'ifilter'):
[30] Fix | Delete
child.value = None
[31] Fix | Delete
child.remove()
[32] Fix | Delete
elif member_name in ('ifilterfalse', 'izip_longest'):
[33] Fix | Delete
node.changed()
[34] Fix | Delete
name_node.value = ('filterfalse' if member_name[1] == 'f'
[35] Fix | Delete
else 'zip_longest')
[36] Fix | Delete
[37] Fix | Delete
# Make sure the import statement is still sane
[38] Fix | Delete
children = imports.children[:] or [imports]
[39] Fix | Delete
remove_comma = True
[40] Fix | Delete
for child in children:
[41] Fix | Delete
if remove_comma and child.type == token.COMMA:
[42] Fix | Delete
child.remove()
[43] Fix | Delete
else:
[44] Fix | Delete
remove_comma ^= True
[45] Fix | Delete
[46] Fix | Delete
while children and children[-1].type == token.COMMA:
[47] Fix | Delete
children.pop().remove()
[48] Fix | Delete
[49] Fix | Delete
# If there are no imports left, just get rid of the entire statement
[50] Fix | Delete
if (not (imports.children or getattr(imports, 'value', None)) or
[51] Fix | Delete
imports.parent is None):
[52] Fix | Delete
p = node.prefix
[53] Fix | Delete
node = BlankLine()
[54] Fix | Delete
node.prefix = p
[55] Fix | Delete
return node
[56] Fix | Delete
[57] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function