Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _import_filter(self, stmt):
module_name = libutils.modname(self.source)
if isinstance(stmt.import_info, importutils.NormalImport):
# Affect any statement that imports the source module
return any(module_name == name
for name, alias in stmt.import_info.names_and_aliases)
elif isinstance(stmt.import_info, importutils.FromImport):
# Affect statements importing from the source package
if '.' in module_name:
package_name, basename = module_name.rsplit('.', 1)
if (stmt.import_info.module_name == package_name and
any(basename == name
for name, alias in stmt.import_info.names_and_aliases)):
return True
return stmt.import_info.module_name == module_name
return False
def _handle_moving_in_from_import_stmt(self, dest, import_stmt,
module_imports, parent_module):
changed = False
context = importutils.importinfo.ImportContext(self.project, None)
if import_stmt.import_info.get_imported_resource(context) == \
parent_module:
imports = import_stmt.import_info.names_and_aliases
new_imports = []
for name, alias in imports:
# The moving module was imported.
if name == self.old_name:
changed = True
new_import = importutils.FromImport(
libutils.modname(dest), 0,
[(self.old_name, alias)])
module_imports.add_import(new_import)
else:
new_imports.append((name, alias))
# Update the imports if the imported names were changed.
if new_imports != imports:
changed = True
if new_imports:
import_stmt.import_info = importutils.FromImport(
import_stmt.import_info.module_name,
import_stmt.import_info.level,
new_imports)
else:
import_stmt.empty_import()
return changed
def _change_import_statements(self, dest, new_name, module_imports):
moving_module = self.source
parent_module = moving_module.parent
changed = False
for import_stmt in module_imports.imports:
if not any(name_and_alias[0] == self.old_name
for name_and_alias in
import_stmt.import_info.names_and_aliases) and \
not any(name_and_alias[0] == libutils.modname(self.source)
for name_and_alias in
import_stmt.import_info.names_and_aliases):
continue
# Case 1: Look for normal imports of the moving module.
if isinstance(import_stmt.import_info, importutils.NormalImport):
continue
# Case 2: The moving module is from-imported.
changed = self._handle_moving_in_from_import_stmt(
dest, import_stmt, module_imports, parent_module) or changed
# Case 3: Names are imported from the moving module.
context = importutils.importinfo.ImportContext(self.project, None)
if not import_stmt.import_info.is_empty() and \
import_stmt.import_info.get_imported_resource(context) == \
def _module_name(self):
return libutils.modname(self.resource)
def _add_import_to_module(project, resource, imported):
pymodule = project.get_pymodule(resource)
import_tools = importutils.ImportTools(project)
module_imports = import_tools.module_imports(pymodule)
module_name = libutils.modname(imported)
new_import = importutils.NormalImport(((module_name, None), ))
module_imports.add_import(new_import)
return change.ChangeContents(resource, module_imports.get_changed_source())
def _new_modname(self, dest):
destname = libutils.modname(dest)
if destname:
return destname + '.' + self.old_name
return self.old_name
def _create_structural_attributes(self):
result = {}
modname = rope.base.libutils.modname(self.resource)
extension_submodules = self.pycore._builtin_submodules(modname)
for name, module in extension_submodules.items():
result[name] = rope.base.builtins.BuiltinName(module)
if self.resource is None:
return result
for name, resource in self._get_child_resources().items():
result[name] = pynames.ImportedModule(self, resource=resource)
return result
def _change_import_statements(self, dest, new_name, module_imports):
moving_module = self.source
parent_module = moving_module.parent
changed = False
for import_stmt in module_imports.imports:
if not any(name_and_alias[0] == self.old_name
for name_and_alias in
import_stmt.import_info.names_and_aliases) and \
not any(name_and_alias[0] == libutils.modname(self.source)
for name_and_alias in
import_stmt.import_info.names_and_aliases):
continue
# Case 1: Look for normal imports of the moving module.
if isinstance(import_stmt.import_info, importutils.NormalImport):
continue
# Case 2: The moving module is from-imported.
changed = self._handle_moving_in_from_import_stmt(
dest, import_stmt, module_imports, parent_module) or changed
# Case 3: Names are imported from the moving module.
context = importutils.importinfo.ImportContext(self.project, None)
if not import_stmt.import_info.is_empty() and \
import_stmt.import_info.get_imported_resource(context) == \
def _handle_moving_in_from_import_stmt(self, dest, import_stmt,
module_imports, parent_module):
changed = False
context = importutils.importinfo.ImportContext(self.project, None)
if import_stmt.import_info.get_imported_resource(context) == \
parent_module:
imports = import_stmt.import_info.names_and_aliases
new_imports = []
for name, alias in imports:
# The moving module was imported.
if name == self.old_name:
changed = True
new_import = importutils.FromImport(
libutils.modname(dest), 0,
[(self.old_name, alias)])
module_imports.add_import(new_import)
else:
new_imports.append((name, alias))
# Update the imports if the imported names were changed.
if new_imports != imports:
changed = True
if new_imports:
import_stmt.import_info = importutils.FromImport(
import_stmt.import_info.module_name,
import_stmt.import_info.level,
new_imports)
else:
import_stmt.empty_import()
return changed
def _change_occurrences_in_module(self, dest, pymodule=None,
resource=None):
if not self.tools.occurs_in_module(pymodule=pymodule,
resource=resource):
return
if pymodule is None:
pymodule = self.project.get_pymodule(resource)
new_name = self._new_modname(dest)
module_imports = importutils.get_module_imports(self.project, pymodule)
changed = False
source = None
if libutils.modname(dest):
changed = self._change_import_statements(dest, new_name,
module_imports)
if changed:
source = module_imports.get_changed_source()
source = self.tools.new_source(pymodule, source)
pymodule = self.tools.new_pymodule(pymodule, source)
new_import = self._new_import(dest)
source = self.tools.rename_in_module(
new_name, imports=True, pymodule=pymodule,
resource=resource if not changed else None)
should_import = self.tools.occurs_in_module(
pymodule=pymodule, resource=resource, imports=False)
pymodule = self.tools.new_pymodule(pymodule, source)
source = self.tools.remove_old_imports(pymodule)
if should_import: