Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _source_module_changes(self, dest):
placeholder = '__rope_moving_%s_' % self.old_name
handle = _ChangeMoveOccurrencesHandle(placeholder)
occurrence_finder = occurrences.create_finder(
self.project, self.old_name, self.old_pyname)
start, end = self._get_moving_region()
renamer = ModuleSkipRenamer(occurrence_finder, self.source,
handle, start, end)
source = renamer.get_changed_module()
pymodule = libutils.get_string_module(self.project, source, self.source)
source = self.import_tools.organize_imports(pymodule, sort=False)
if handle.occurred:
pymodule = libutils.get_string_module(
self.project, source, self.source)
# Adding new import
source, imported = importutils.add_import(
self.project, pymodule, self._new_modname(dest), self.old_name)
source = source.replace(placeholder, imported)
return ChangeContents(self.source, source)
project, source[lines.get_line_start(start):], resource)
origin = project.get_pymodule(resource)
imports = []
for stmt in import_tools.module_imports(origin).imports:
imports.append(stmt.import_info)
back_names = []
for name in origin:
if name not in pymodule:
back_names.append(name)
imports.append(import_tools.get_from_import(resource, back_names))
source = _add_imports_to_module(import_tools, pymodule, imports)
pymodule = libutils.get_string_module(project, source, resource)
source = import_tools.relatives_to_absolutes(pymodule)
pymodule = libutils.get_string_module(project, source, resource)
source = import_tools.organize_imports(pymodule, selfs=False)
pymodule = libutils.get_string_module(project, source, resource)
# extracting imports after changes
module_imports = import_tools.module_imports(pymodule)
imports = [import_stmt.import_info
for import_stmt in module_imports.imports]
start = 1
if module_imports.imports:
start = module_imports.imports[-1].end_line
lines = codeanalyze.SourceLinesAdapter(source)
while start < lines.length() and not lines.get_line(start).strip():
start += 1
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)
"""If `offset` is None, the `resource` itself will be renamed"""
self.project = project
self.resource = resource
if offset is not None:
self.old_name = worder.get_name_at(self.resource, offset)
this_pymodule = self.project.get_pymodule(self.resource)
self.old_instance, self.old_pyname = \
evaluate.eval_location2(this_pymodule, offset)
if self.old_pyname is None:
raise exceptions.RefactoringError(
'Rename refactoring should be performed'
' on resolvable python identifiers.')
else:
if not resource.is_folder() and resource.name == '__init__.py':
resource = resource.parent
dummy_pymodule = libutils.get_string_module(self.project, '')
self.old_instance = None
self.old_pyname = pynames.ImportedModule(dummy_pymodule,
resource=resource)
if resource.is_folder():
self.old_name = resource.name
else:
self.old_name = resource.name[:-3]
module_with_imports.expand_stars()
source = module_with_imports.get_changed_source()
if source is not None:
pymodule = libutils.get_string_module(
self.project, source, resource)
source = self.relatives_to_absolutes(pymodule)
if source is not None:
pymodule = libutils.get_string_module(
self.project, source, resource)
module_with_imports = self.module_imports(pymodule, import_filter)
module_with_imports.remove_duplicates()
module_with_imports.remove_unused_imports()
source = module_with_imports.get_changed_source()
if source is not None:
pymodule = libutils.get_string_module(
self.project, source, resource)
return pymodule
@utils.saveit
def get_pymodule(self):
"""Get a `PyModule`"""
msg = None
code = self.code
tries = 0
while True:
try:
if tries == 0 and self.resource is not None and \
self.resource.read() == code:
return self.project.get_pymodule(self.resource,
force_errors=True)
return libutils.get_string_module(
self.project, code, resource=self.resource,
force_errors=True)
except exceptions.ModuleSyntaxError, e:
if msg is None:
msg = '%s:%s %s' % (e.filename, e.lineno, e.message_)
if tries < self.maxfixes:
tries += 1
self.commenter.comment(e.lineno)
code = '\n'.join(self.commenter.lines)
else:
raise exceptions.ModuleSyntaxError(
e.filename, e.lineno,
'Failed to fix error: {}'.format(msg))
def _add_imports2(self, pymodule, new_imports):
source = self.tools.add_imports(pymodule, new_imports)
if source is None:
return pymodule, False
else:
resource = pymodule.get_resource()
pymodule = libutils.get_string_module(
self.project, source, resource)
return pymodule, True
if module_with_imports.imports:
lineno = module_with_imports.imports[-1].end_line - 1
else:
while lineno < pymodule.lines.length() and \
pymodule.lines.get_line(lineno + 1).\
lstrip().startswith('#'):
lineno += 1
if lineno > 0:
cut = pymodule.lines.get_line_end(lineno) + 1
result = source[:cut] + '\n\n' + moving + source[cut:]
else:
result = moving + source
# Organizing imports
source = result
pymodule = libutils.get_string_module(self.project, source, dest)
source = self.import_tools.organize_imports(pymodule, sort=False,
unused=False)
# Remove unused imports of the old module
pymodule = libutils.get_string_module(self.project, source, dest)
source = self.import_tools.organize_imports(
pymodule, sort=False, selfs=False, unused=True,
import_filter=self._import_filter)
return ChangeContents(dest, source)
def _source_module_changes(self, dest):
placeholder = '__rope_moving_%s_' % self.old_name
handle = _ChangeMoveOccurrencesHandle(placeholder)
occurrence_finder = occurrences.create_finder(
self.project, self.old_name, self.old_pyname)
start, end = self._get_moving_region()
renamer = ModuleSkipRenamer(occurrence_finder, self.source,
handle, start, end)
source = renamer.get_changed_module()
pymodule = libutils.get_string_module(self.project, source, self.source)
source = self.import_tools.organize_imports(pymodule, sort=False)
if handle.occurred:
pymodule = libutils.get_string_module(
self.project, source, self.source)
# Adding new import
source, imported = importutils.add_import(
self.project, pymodule, self._new_modname(dest), self.old_name)
source = source.replace(placeholder, imported)
return ChangeContents(self.source, source)
def _clean_up_imports(self, pymodule, import_filter):
resource = pymodule.get_resource()
module_with_imports = self.module_imports(pymodule, import_filter)
module_with_imports.expand_stars()
source = module_with_imports.get_changed_source()
if source is not None:
pymodule = libutils.get_string_module(
self.project, source, resource)
source = self.relatives_to_absolutes(pymodule)
if source is not None:
pymodule = libutils.get_string_module(
self.project, source, resource)
module_with_imports = self.module_imports(pymodule, import_filter)
module_with_imports.remove_duplicates()
module_with_imports.remove_unused_imports()
source = module_with_imports.get_changed_source()
if source is not None:
pymodule = libutils.get_string_module(
self.project, source, resource)
return pymodule