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: pymodule = self.tools.new_pymodule(pymodule, source) source = self.tools.add_imports(pymodule, [new_import]) source = self.tools.new_source(pymodule, source) if source is not None and source != pymodule.resource.read(): return source return None
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: pymodule = self.tools.new_pymodule(pymodule, source) source = self.tools.add_imports(pymodule, [new_import]) source = self.tools.new_source(pymodule, source) if source is not None and source != pymodule.resource.read(): return source return None
def _add_imports(pycore, source, resource, imports): if not imports: return source pymodule = pycore.get_string_module(source, resource) module_import = importutils.get_module_imports(pycore, pymodule) for import_info in imports: module_import.add_import(import_info) source = module_import.get_changed_source() pymodule = pycore.get_string_module(source, resource) import_tools = importutils.ImportTools(pycore) return import_tools.organize_imports(pymodule, unused=False, sort=False)
def _add_imports(project, source, resource, imports): if not imports: return source pymodule = libutils.get_string_module(project, source, resource) module_import = importutils.get_module_imports(project, pymodule) for import_info in imports: module_import.add_import(import_info) source = module_import.get_changed_source() pymodule = libutils.get_string_module(project, source, resource) import_tools = importutils.ImportTools(project) return import_tools.organize_imports(pymodule, unused=False, sort=False)
def _add_imports(self, source, file): if not self.imports: return source pymodule = self.pycore.get_string_module(source, file) module_import = importutils.get_module_imports(self.pycore, pymodule) for import_info in self.imports: module_import.add_import(import_info) source = module_import.get_changed_source() pymodule = self.pycore.get_string_module(source, file) source = self.import_tools.organize_imports( pymodule, unused=False, sort=False) return 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 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 = self.project.pycore.get_string_module(code) except exceptions.ModuleSyntaxError: return 1 testmodname = '__rope_testmodule_rope' importinfo = importutils.NormalImport(((testmodname, None),)) module_imports = importutils.get_module_imports( self.project.pycore, 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 _remove_from(pycore, pyname, source, resource): pymodule = pycore.get_string_module(source, resource) module_import = importutils.get_module_imports(pycore, pymodule) module_import.remove_pyname(pyname) return module_import.get_changed_source()
def _remove_from(project, pyname, source, resource): pymodule = libutils.get_string_module(project, source, resource) module_import = importutils.get_module_imports(project, pymodule) module_import.remove_pyname(pyname) return module_import.get_changed_source()