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 _add_relative_import_to_module(project, resource, imported, name): pymodule = project.get_pymodule(resource) import_tools = importutils.ImportTools(project) module_imports = import_tools.module_imports(pymodule) new_import = import_tools.get_from_import(imported, name) module_imports.add_import(new_import) return change.ChangeContents(resource, module_imports.get_changed_source())
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 get_changes(self, classname=None, new_class_name=None): if new_class_name is not None: warnings.warn( "new_class_name parameter is deprecated; use classname", DeprecationWarning, stacklevel=2, ) classname = new_class_name collector = codeanalyze.ChangeCollector(self.pymodule.source_code) start, end = sourceutils.get_body_region(self.pyfunction) indents = sourceutils.get_indents( self.pymodule.lines, self.pyfunction.get_scope().get_start()) + sourceutils.get_indent( self.project) new_contents = " " * indents + "return %s(%s)()\n" % ( classname, ", ".join(self._get_parameter_names()), ) collector.add_change(start, end, new_contents) insertion = self._get_class_insertion_point() collector.add_change(insertion, insertion, "\n\n" + self.get_new_class(classname)) changes = change.ChangeSet( "Replace method with method object refactoring") changes.add_change( change.ChangeContents(self.resource, collector.get_changed())) return changes
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 write(self, contents): try: if contents == self.read(): return except IOError: pass self._perform_change(change.ChangeContents(self, contents), 'Writing file <%s>' % self.path)
def get_changes(self): changes = change.ChangeSet('Generate %s <%s>' % (self._get_element_kind(), self.name)) indents = self.info.get_scope_indents() blanks = self.info.get_blank_lines() base_definition = sourceutils.fix_indentation(self._get_element(), indents) definition = '\n' * blanks[0] + base_definition + '\n' * blanks[1] resource = self.info.get_insertion_resource() start, end = self.info.get_insertion_offsets() collector = codeanalyze.ChangeCollector(resource.read()) collector.add_change(start, end, definition) changes.add_change(change.ChangeContents( resource, collector.get_changed())) return changes
def get_changes(self): changes = change.ChangeSet("Generate %s <%s>" % (self._get_element_kind(), self.name)) indents = self.info.get_scope_indents() blanks = self.info.get_blank_lines() base_definition = sourceutils.fix_indentation(self._get_element(), indents) definition = "\n" * blanks[0] + base_definition + "\n" * blanks[1] resource = self.info.get_insertion_resource() start, end = self.info.get_insertion_offsets() collector = codeanalyze.ChangeCollector(resource.read()) collector.add_change(start, end, definition) changes.add_change( change.ChangeContents(resource, collector.get_changed())) if self.goal_resource: relative_import = _add_relative_import_to_module( self.project, self.resource, self.goal_resource, self.name) changes.add_change(relative_import) return changes