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 _insert_import(name, module, ctx): if not ctx.resource: source, _ = env.get_offset_params() lineno = ctx.importer.find_insertion_line(source) line = 'from %s import %s' % (module, name) env.curbuf[lineno - 1:lineno - 1] = [env.prepare_value(line, dumps=False)] return True pyobject = ctx.project.pycore.resource_to_pyobject(ctx.resource) import_tools = importutils.ImportTools(ctx.project) module_imports = import_tools.module_imports(pyobject) new_import = importutils.FromImport(module, 0, [[name, None]]) module_imports.add_import(new_import) changes = change.ChangeContents(ctx.resource, module_imports.get_changed_source()) action = env.user_input_choices('Choose what to do:', 'perform', 'preview') if not action: return False if action == 'preview': print("\n ") print("-------------------------------") print("\n%s\n" % changes.get_description()) print("-------------------------------\n\n") if not env.user_confirm('Do the changes?'): return False progress = ProgressHandler('Apply changes ...') ctx.project.do(changes, task_handle=progress.handle) reload_changes(changes)
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) == \ moving_module: import_stmt.import_info = importutils.FromImport( new_name, import_stmt.import_info.level, import_stmt.import_info.names_and_aliases) changed = True return changed