Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3..../lib2to3/fixes
File: fix_import.py
"""Fixer for import statements.
[0] Fix | Delete
If spam is being imported from the local directory, this import:
[1] Fix | Delete
from spam import eggs
[2] Fix | Delete
Becomes:
[3] Fix | Delete
from .spam import eggs
[4] Fix | Delete
[5] Fix | Delete
And this import:
[6] Fix | Delete
import spam
[7] Fix | Delete
Becomes:
[8] Fix | Delete
from . import spam
[9] Fix | Delete
"""
[10] Fix | Delete
[11] Fix | Delete
# Local imports
[12] Fix | Delete
from .. import fixer_base
[13] Fix | Delete
from os.path import dirname, join, exists, sep
[14] Fix | Delete
from ..fixer_util import FromImport, syms, token
[15] Fix | Delete
[16] Fix | Delete
[17] Fix | Delete
def traverse_imports(names):
[18] Fix | Delete
"""
[19] Fix | Delete
Walks over all the names imported in a dotted_as_names node.
[20] Fix | Delete
"""
[21] Fix | Delete
pending = [names]
[22] Fix | Delete
while pending:
[23] Fix | Delete
node = pending.pop()
[24] Fix | Delete
if node.type == token.NAME:
[25] Fix | Delete
yield node.value
[26] Fix | Delete
elif node.type == syms.dotted_name:
[27] Fix | Delete
yield "".join([ch.value for ch in node.children])
[28] Fix | Delete
elif node.type == syms.dotted_as_name:
[29] Fix | Delete
pending.append(node.children[0])
[30] Fix | Delete
elif node.type == syms.dotted_as_names:
[31] Fix | Delete
pending.extend(node.children[::-2])
[32] Fix | Delete
else:
[33] Fix | Delete
raise AssertionError("unknown node type")
[34] Fix | Delete
[35] Fix | Delete
[36] Fix | Delete
class FixImport(fixer_base.BaseFix):
[37] Fix | Delete
BM_compatible = True
[38] Fix | Delete
[39] Fix | Delete
PATTERN = """
[40] Fix | Delete
import_from< 'from' imp=any 'import' ['('] any [')'] >
[41] Fix | Delete
|
[42] Fix | Delete
import_name< 'import' imp=any >
[43] Fix | Delete
"""
[44] Fix | Delete
[45] Fix | Delete
def start_tree(self, tree, name):
[46] Fix | Delete
super(FixImport, self).start_tree(tree, name)
[47] Fix | Delete
self.skip = "absolute_import" in tree.future_features
[48] Fix | Delete
[49] Fix | Delete
def transform(self, node, results):
[50] Fix | Delete
if self.skip:
[51] Fix | Delete
return
[52] Fix | Delete
imp = results['imp']
[53] Fix | Delete
[54] Fix | Delete
if node.type == syms.import_from:
[55] Fix | Delete
# Some imps are top-level (eg: 'import ham')
[56] Fix | Delete
# some are first level (eg: 'import ham.eggs')
[57] Fix | Delete
# some are third level (eg: 'import ham.eggs as spam')
[58] Fix | Delete
# Hence, the loop
[59] Fix | Delete
while not hasattr(imp, 'value'):
[60] Fix | Delete
imp = imp.children[0]
[61] Fix | Delete
if self.probably_a_local_import(imp.value):
[62] Fix | Delete
imp.value = "." + imp.value
[63] Fix | Delete
imp.changed()
[64] Fix | Delete
else:
[65] Fix | Delete
have_local = False
[66] Fix | Delete
have_absolute = False
[67] Fix | Delete
for mod_name in traverse_imports(imp):
[68] Fix | Delete
if self.probably_a_local_import(mod_name):
[69] Fix | Delete
have_local = True
[70] Fix | Delete
else:
[71] Fix | Delete
have_absolute = True
[72] Fix | Delete
if have_absolute:
[73] Fix | Delete
if have_local:
[74] Fix | Delete
# We won't handle both sibling and absolute imports in the
[75] Fix | Delete
# same statement at the moment.
[76] Fix | Delete
self.warning(node, "absolute and local imports together")
[77] Fix | Delete
return
[78] Fix | Delete
[79] Fix | Delete
new = FromImport(".", [imp])
[80] Fix | Delete
new.prefix = node.prefix
[81] Fix | Delete
return new
[82] Fix | Delete
[83] Fix | Delete
def probably_a_local_import(self, imp_name):
[84] Fix | Delete
if imp_name.startswith("."):
[85] Fix | Delete
# Relative imports are certainly not local imports.
[86] Fix | Delete
return False
[87] Fix | Delete
imp_name = imp_name.split(".", 1)[0]
[88] Fix | Delete
base_path = dirname(self.filename)
[89] Fix | Delete
base_path = join(base_path, imp_name)
[90] Fix | Delete
# If there is no __init__.py next to the file its not in a package
[91] Fix | Delete
# so can't be a relative import.
[92] Fix | Delete
if not exists(join(dirname(base_path), "__init__.py")):
[93] Fix | Delete
return False
[94] Fix | Delete
for ext in [".py", sep, ".pyc", ".so", ".sl", ".pyd"]:
[95] Fix | Delete
if exists(base_path + ext):
[96] Fix | Delete
return True
[97] Fix | Delete
return False
[98] Fix | Delete
[99] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function