def rename_in_module(self, new_name, pymodule=None, imports=False, resource=None): occurrence_finder = self._create_finder(imports) source = rename.rename_in_module( occurrence_finder, new_name, replace_primary=True, pymodule=pymodule, resource=resource) return source
def _inline_variable(pycore, pymodule, pyname, name, remove=True, region=None): assignment = pyname.assignments[0] definition_line = assignment.ast_node.lineno lines = pymodule.lines logicals = pymodule.logical_lines start, end = logicals.logical_line_in(definition_line) definition_with_assignment = _join_lines( [lines.get_line(n) for n in range(start, end + 1)]) if assignment.levels: raise rope.base.exceptions.RefactoringError( 'Cannot inline tuple assignments.') definition = definition_with_assignment[definition_with_assignment.\ index('=') + 1:].strip() occurrence_finder = occurrences.create_finder(pycore, name, pyname) changed_source = rename.rename_in_module( occurrence_finder, definition, pymodule=pymodule, replace_primary=True, writes=False, region=region) if changed_source is None: changed_source = pymodule.source_code if remove: lines = codeanalyze.SourceLinesAdapter(changed_source) source = changed_source[:lines.get_line_start(start)] + \ changed_source[lines.get_line_end(end) + 1:] else: source = changed_source return source
def _inline_variable(project, pymodule, pyname, name, remove=True, region=None, docs=False): definition = _getvardef(pymodule, pyname) start, end = _assigned_lineno(pymodule, pyname) occurrence_finder = occurrences.create_finder(project, name, pyname, docs=docs) changed_source = rename.rename_in_module(occurrence_finder, definition, pymodule=pymodule, replace_primary=True, writes=False, region=region) if changed_source is None: changed_source = pymodule.source_code if remove: lines = codeanalyze.SourceLinesAdapter(changed_source) source = changed_source[:lines.get_line_start(start)] + \ changed_source[lines.get_line_end(end) + 1:] else: source = changed_source return source
def _calculate_definition(self, primary, pyname, call, host_vars, returns): header, to_be_inlined = self._calculate_header(primary, pyname, call) source = header + self.body mod = libutils.get_string_module(self.project, source) name_dict = mod.get_scope().get_names() all_names = [x for x in name_dict if not isinstance(name_dict[x], rope.base.builtins.BuiltinName)] # If there is a name conflict, all variable names # inside the inlined function are renamed if len(set(all_names).intersection(set(host_vars))) > 0: prefix = _DefinitionGenerator.unique_prefix.next() guest = libutils.get_string_module(self.project, source, self.resource) to_be_inlined = [prefix + item for item in to_be_inlined] for item in all_names: pyname = guest[item] occurrence_finder = occurrences.create_finder(self.project, item, pyname) source = rename.rename_in_module(occurrence_finder, prefix + item, pymodule=guest) guest = libutils.get_string_module(self.project, source, self.resource) # parameters not reassigned inside the functions are now inlined. for name in to_be_inlined: pymodule = libutils.get_string_module(self.project, source, self.resource) pyname = pymodule[name] source = _inline_variable(self.project, pymodule, pyname, name) return self._replace_returns_with(source, returns)
def _get_body(self, host="host"): self_name = self._get_self_name() body = self_name + " = None\n" + self._get_unchanged_body() pymodule = libutils.get_string_module(self.project, body) finder = occurrences.create_finder(self.project, self_name, pymodule[self_name]) result = rename.rename_in_module(finder, host, pymodule=pymodule) if result is None: result = body return result[result.index("\n") + 1 :]
def _get_body(self, host='host'): self_name = self._get_self_name() body = self_name + ' = None\n' + self._get_unchanged_body() pymodule = self.pycore.get_string_module(body) finder = occurrences.create_finder( self.pycore, self_name, pymodule[self_name]) result = rename.rename_in_module(finder, host, pymodule=pymodule) if result is None: result = body return result[result.index('\n') + 1:]
def _get_body(self, host='host'): self_name = self._get_self_name() body = self_name + ' = None\n' + self._get_unchanged_body() pymodule = libutils.get_string_module(self.project, body) finder = occurrences.create_finder(self.project, self_name, pymodule[self_name]) result = rename.rename_in_module(finder, host, pymodule=pymodule) if result is None: result = body return result[result.index('\n') + 1:]
def _rename_occurrences(self, file_, changed_name, global_factory): finder = occurrences.create_finder(self.project, self.old_name, self.old_pyname, only_calls=True) result = rename.rename_in_module(finder, changed_name, resource=file_, replace_primary=global_factory) return result
def test_rename_in_module_renaming_one_letter_names_for_expressions(self): mod1 = testutils.create_module(self.project, 'mod1') mod1.write('a = 10\nprint(1+a)\n') pymod = self.project.get_module('mod1') old_pyname = pymod['a'] finder = rope.refactor.occurrences.create_finder( self.project, 'a', old_pyname) refactored = rename.rename_in_module( finder, 'new_var', pymodule=pymod, replace_primary=True) self.assertEquals('new_var = 10\nprint(1+new_var)\n', refactored)
def _get_body(self): body = sourceutils.get_body(self.pyfunction) for param in self._get_parameter_names(): body = param + ' = None\n' + body pymod = self.pycore.get_string_module(body, self.resource) pyname = pymod[param] finder = occurrences.create_finder(self.pycore, param, pyname) result = rename.rename_in_module(finder, 'self.' + param, pymodule=pymod) body = result[result.index('\n') + 1:] return body
def _get_body(self): body = sourceutils.get_body(self.pyfunction) for param in self._get_parameter_names(): body = param + " = None\n" + body pymod = libutils.get_string_module(self.project, body, self.resource) pyname = pymod[param] finder = occurrences.create_finder(self.project, param, pyname) result = rename.rename_in_module(finder, "self." + param, pymodule=pymod) body = result[result.index("\n") + 1:] return body
def _change_module(self, resource, remove, only_current): filters = [occurrences.NoImportsFilter(), occurrences.PyNameFilter(self.pyname)] if only_current and resource == self.original: def check_aim(occurrence): start, end = occurrence.get_primary_range() if self.offset < start or end < self.offset: return False filters.insert(0, check_aim) finder = occurrences.Finder(self.pycore, self.name, filters=filters) changed = rename.rename_in_module( finder, self.imported, resource=resource, replace_primary=True) if changed and remove: changed = _remove_from(self.pycore, self.pyname, changed, resource) return changed
def _from_to_normal(self, pymodule, import_stmt): resource = pymodule.get_resource() from_import = import_stmt.import_info module_name = from_import.module_name for name, alias in from_import.names_and_aliases: imported = name if alias is not None: imported = alias occurrence_finder = occurrences.create_finder(self.project, imported, pymodule[imported], imports=False) source = rename.rename_in_module( occurrence_finder, module_name + "." + name, pymodule=pymodule, replace_primary=True ) if source is not None: pymodule = libutils.get_string_module(self.project, source, resource) return pymodule
def _change_module(self, resource, remove, only_current): filters = [occurrences.NoImportsFilter(), occurrences.PyNameFilter(self.pyname)] if only_current and resource == self.original: def check_aim(occurrence): start, end = occurrence.get_primary_range() if self.offset < start or end < self.offset: return False filters.insert(0, check_aim) finder = occurrences.Finder(self.project, self.name, filters=filters) changed = rename.rename_in_module( finder, self.imported, resource=resource, replace_primary=True) if changed and remove: changed = _remove_from(self.project, self.pyname, changed, resource) return changed
def _inline_variable(project, pymodule, pyname, name, remove=True, region=None): definition = _getvardef(pymodule, pyname) start, end = _assigned_lineno(pymodule, pyname) occurrence_finder = occurrences.create_finder(project, name, pyname) changed_source = rename.rename_in_module( occurrence_finder, definition, pymodule=pymodule, replace_primary=True, writes=False, region=region ) if changed_source is None: changed_source = pymodule.source_code if remove: lines = codeanalyze.SourceLinesAdapter(changed_source) source = changed_source[: lines.get_line_start(start)] + changed_source[lines.get_line_end(end) + 1 :] else: source = changed_source return source
def _from_to_normal(self, pymodule, import_stmt): resource = pymodule.get_resource() from_import = import_stmt.import_info module_name = from_import.module_name for name, alias in from_import.names_and_aliases: imported = name if alias is not None: imported = alias occurrence_finder = occurrences.create_finder( self.project, imported, pymodule[imported], imports=False) source = rename.rename_in_module( occurrence_finder, module_name + '.' + name, pymodule=pymodule, replace_primary=True) if source is not None: pymodule = libutils.get_string_module( self.project, source, resource) return pymodule
def _calculate_definition(self, primary, pyname, call, host_vars, returns): header, to_be_inlined = self._calculate_header(primary, pyname, call) source = header + self.body mod = libutils.get_string_module(self.project, source) name_dict = mod.get_scope().get_names() all_names = [ x for x in name_dict if not isinstance(name_dict[x], rope.base.builtins.BuiltinName) ] # If there is a name conflict, all variable names # inside the inlined function are renamed if len(set(all_names).intersection(set(host_vars))) > 0: prefix = next(_DefinitionGenerator.unique_prefix) guest = libutils.get_string_module(self.project, source, self.resource) to_be_inlined = [prefix + item for item in to_be_inlined] for item in all_names: pyname = guest[item] occurrence_finder = occurrences.create_finder( self.project, item, pyname) source = rename.rename_in_module(occurrence_finder, prefix + item, pymodule=guest) guest = libutils.get_string_module(self.project, source, self.resource) #parameters not reassigned inside the functions are now inlined. for name in to_be_inlined: pymodule = libutils.get_string_module(self.project, source, self.resource) pyname = pymodule[name] source = _inline_variable(self.project, pymodule, pyname, name) return self._replace_returns_with(source, returns)
def _rename_occurrences(self, file_, changed_name, global_factory): finder = occurrences.create_finder(self.pycore, self.old_name, self.old_pyname, only_calls=True) result = rename.rename_in_module(finder, changed_name, resource=file_, replace_primary=global_factory) return result