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 find_insertion_line(self, code): """Guess at what line the new import should be inserted""" match = re.search(r'^(def|class)\s+', code) if match is not None: code = code[:match.start()] try: pymodule = libutils.get_string_module(self.project, code) except exceptions.ModuleSyntaxError: return 1 testmodname = '__rope_testmodule_rope' importinfo = importutils.NormalImport(((testmodname, None), )) module_imports = importutils.get_module_imports(self.project, pymodule) module_imports.add_import(importinfo) code = module_imports.get_changed_source() offset = code.index(testmodname) lineno = code.count('\n', 0, offset) + 1 return lineno
def _new_import(self, dest): return importutils.NormalImport([(self._new_modname(dest), None)])