def get_changes(self, checks=None, imports=None, resources=None, task_handle=taskhandle.NullTaskHandle()): """Get the changes needed by this restructuring `resources` can be a list of `rope.base.resources.File`\s to apply the restructuring on. If `None`, the restructuring will be applied to all python files. `checks` argument has been deprecated. Use the `args` argument of the constructor. The usage of:: strchecks = {'obj1.type': 'mod.A', 'obj2': 'mod.B', 'obj3.object': 'mod.C'} checks = restructuring.make_checks(strchecks) can be replaced with:: args = {'obj1': 'type=mod.A', 'obj2': 'name=mod.B', 'obj3': 'object=mod.C'} where obj1, obj2 and obj3 are wildcard names that appear in restructuring pattern. """ if checks is not None: warnings.warn( 'The use of checks parameter is deprecated; ' 'use the args parameter of the constructor instead.', DeprecationWarning, stacklevel=2) for name, value in checks.items(): self.args[name] = similarfinder._pydefined_to_str(value) if imports is not None: warnings.warn( 'The use of imports parameter is deprecated; ' 'use imports parameter of the constructor, instead.', DeprecationWarning, stacklevel=2) self.imports = imports changes = change.ChangeSet('Restructuring <%s> to <%s>' % (self.pattern, self.goal)) if resources is not None: files = [resource for resource in resources if libutils.is_python_file(self.project, resource)] else: files = self.project.get_python_files() job_set = task_handle.create_jobset('Collecting Changes', len(files)) for resource in files: job_set.started_job(resource.path) pymodule = self.project.get_pymodule(resource) finder = similarfinder.SimilarFinder(pymodule, wildcards=self.wildcards) matches = list(finder.get_matches(self.pattern, self.args)) computer = self._compute_changes(matches, pymodule) result = computer.get_changed() if result is not None: imported_source = self._add_imports(resource, result, self.imports) changes.add_change(change.ChangeContents(resource, imported_source)) job_set.finished_job() return changes
def main(): from rope.base.project import Project from rope.base import libutils from rope.refactor.rename import Rename from rope.base.exceptions import RopeError path = editor.get_path() selection = editor.get_selection() if not path or not selection: console.hud_alert('Not a Python file', 'error') return tab.save() project = None try: project = Project(os.path.dirname(path), ropefolder=None) resource = libutils.path_to_resource(project, path) if not libutils.is_python_file(project, resource): console.hud_alert('Not a Python file', 'error') return renamer = Rename(project, resource, selection[0]) old_name = renamer.get_old_name() if not old_name: console.hud_alert('Unable to get identifier name', 'error') return new_name = _ask_for_new_name(old_name) change_set = renamer.get_changes(new_name, docs=True, resources=[resource]) if not change_set: console.hud_alert('No changes required') return if refactoring.ask_if_apply_change_set(change_set): refactoring.apply_change_set(change_set, path, selection) console.hud_alert('Identifier renamed') except RopeError as e: console.hud_alert(str(e), 'error') except KeyboardInterrupt: pass finally: if project: project.close()
def main(): from rope.base.project import Project from rope.base import libutils from rope.refactor.importutils import ImportOrganizer from rope.base.exceptions import RopeError path = editor.get_path() selection = editor.get_selection() if not path or not selection: console.hud_alert('Not a Python file', 'error') return tab.save() project = None try: project = Project(os.path.dirname(path), ropefolder=None) resource = libutils.path_to_resource(project, path) if not libutils.is_python_file(project, resource): console.hud_alert('Not a Python file', 'error') return organizer = ImportOrganizer(project) change_set = organizer.organize_imports(resource) if not change_set: console.hud_alert('No changes required') return if refactoring.ask_if_apply_change_set(change_set): refactoring.apply_change_set(change_set, path, selection) console.hud_alert('Imports organized') except RopeError as e: console.hud_alert(str(e), 'error') except KeyboardInterrupt: pass finally: if project: project.close()