Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _check_redefinition(self, name, function_scope):
class_scope = function_scope.parent
if name in class_scope.pyobject:
raise exceptions.RefactoringError(
'The field %s already exists' % name)
def newfunc(*args, **kwds):
try:
return func(*args, **kwds)
except exceptions.RopeError as e:
short = None
if isinstance(e, input_exceptions):
if not raise_exceptions:
return error_return
short = _exception_message(e)
logger(str(traceback.format_exc()), short)
newfunc.__name__ = func.__name__
def _create_resource(self, file_name, kind='file'):
resource_path = self.project._get_resource_path(file_name)
if os.path.exists(resource_path):
raise exceptions.RopeError('Resource <%s> already exists'
% resource_path)
resource = self.project.get_file(file_name)
if not resource.parent.exists():
raise exceptions.ResourceNotFoundError(
'Parent folder of <%s> does not exist' % resource.path)
fscommands = self._get_fscommands(resource)
try:
if kind == 'file':
fscommands.create_file(resource_path)
else:
fscommands.create_folder(resource_path)
except IOError as e:
raise exceptions.RopeError(e)
def __init__(self, project, resource, offset):
self.project = project
self.resource = resource
self.offset = offset
self._set_name_and_pyname()
if self.pyname is None or self.pyname.get_object() is None or \
not isinstance(self.pyname.get_object(), pyobjects.PyFunction):
raise rope.base.exceptions.RefactoringError(
'Change method signature should be performed on functions')
def redo(self, change=None, task_handle=taskhandle.NullTaskHandle()):
"""Redo undone changes from the history
When `change` is `None`, the last undone change will be
redone. If change is not `None` it should be an item from
`self.redo_list`; this change and all changes that depend on
it will be redone. In both cases the list of redone changes
will be returned.
"""
if not self.redo_list:
raise exceptions.HistoryError('Redo list is empty')
if change is None:
change = self.redo_list[-1]
dependencies = self._find_dependencies(self.redo_list, change)
self._move_front(self.redo_list, dependencies)
self._perform_redos(len(dependencies), task_handle)
return self.undo_list[-len(dependencies):]
def _get_factory_method(self, lines, class_scope,
factory_name, global_):
unit_indents = ' ' * sourceutils.get_indent(self.project)
if global_:
if self._get_scope_indents(lines, class_scope) > 0:
raise rope.base.exceptions.RefactoringError(
'Cannot make global factory method for nested classes.')
return ('\ndef %s(*args, **kwds):\n%sreturn %s(*args, **kwds)\n' %
(factory_name, unit_indents, self.old_name))
unindented_factory = \
('@staticmethod\ndef %s(*args, **kwds):\n' % factory_name +
'%sreturn %s(*args, **kwds)\n' % (unit_indents, self.old_name))
indents = self._get_scope_indents(lines, class_scope) + \
sourceutils.get_indent(self.project)
return '\n' + sourceutils.indent_lines(unindented_factory, indents)
def find_implementations(project, resource, offset, resources=None,
task_handle=taskhandle.NullTaskHandle()):
"""Find the places a given method is overridden.
Finds the places a method is implemented. Returns a list of
`Location`\s.
"""
name = worder.get_name_at(resource, offset)
this_pymodule = project.pycore.resource_to_pyobject(resource)
pyname = rope.base.evaluate.eval_location(this_pymodule, offset)
if pyname is not None:
pyobject = pyname.get_object()
if not isinstance(pyobject, rope.base.pyobjects.PyFunction) or \
pyobject.get_kind() != 'method':
raise exceptions.BadIdentifierError('Not a method!')
else:
raise exceptions.BadIdentifierError('Cannot resolve the identifier!')
def is_defined(occurrence):
if not occurrence.is_defined():
return False
def not_self(occurrence):
if occurrence.get_pyname().get_object() == pyname.get_object():
return False
filters = [is_defined, not_self,
occurrences.InHierarchyFilter(pyname, True)]
finder = occurrences.Finder(project.pycore, name, filters=filters)
if resources is None:
resources = project.get_python_files()
job_set = task_handle.create_jobset('Finding Implementations',
def get_name_locations(self, name):
"""Return a list of ``(resource, lineno)`` tuples"""
result = []
pycore = self.project.pycore
for module in self.names:
if name in self.names[module]:
try:
pymodule = pycore.get_module(module)
if name in pymodule:
pyname = pymodule[name]
module, lineno = pyname.get_definition_location()
if module is not None:
resource = module.get_module().get_resource()
if resource is not None and lineno is not None:
result.append((resource, lineno))
except exceptions.ModuleNotFoundError:
pass
return result
def update_resource(self, resource, underlined=None):
"""Update the cache for global names in `resource`"""
try:
pymodule = self.project.pycore.resource_to_pyobject(resource)
modname = self._module_name(resource)
self._add_names(pymodule, modname, underlined)
except exceptions.ModuleSyntaxError:
pass
def _init_source(self, pycore, source_code, resource):
filename = 'string'
if resource:
filename = resource.path
try:
if source_code is None:
source_bytes = resource.read_bytes()
source_code = fscommands.file_data_to_unicode(source_bytes)
else:
if isinstance(source_code, unicode):
source_bytes = fscommands.unicode_to_file_data(source_code)
else:
source_bytes = source_code
ast_node = ast.parse(source_bytes, filename=filename)
except SyntaxError as e:
raise exceptions.ModuleSyntaxError(filename, e.lineno, e.msg)
except UnicodeDecodeError as e:
raise exceptions.ModuleSyntaxError(filename, 1, '%s' % (e.reason))
return source_code, ast_node