Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _rename_in_module(self, pymodule, name, new_name, till_dot=False):
old_name = name.split('.')[-1]
old_pyname = rope.base.evaluate.eval_str(pymodule.get_scope(), name)
occurrence_finder = occurrences.create_finder(
self.project, old_name, old_pyname, imports=False)
changes = rope.base.codeanalyze.ChangeCollector(pymodule.source_code)
for occurrence in occurrence_finder.find_occurrences(
pymodule=pymodule):
start, end = occurrence.get_primary_range()
if till_dot:
new_end = pymodule.source_code.index('.', end) + 1
space = pymodule.source_code[end:new_end - 1].strip()
if not space == '':
for c in space:
if not c.isspace() and c not in '\\':
raise ValueError()
end = new_end
changes.add_change(start, end, new_name)
source = changes.get_changed()
name = identifier_before_dot()
if not name:
return [], " Not at an identifier."
module = None
try:
module = __import__(name)
except ImportError, e:
return [], " %s." % e
names = dir(module)
for name in names:
if not name.startswith("__"):
p = rope.contrib.codeassist.CompletionProposal(
name, "imported", rope.base.pynames.UnboundName())
type_name = type(getattr(module, name)).__name__
if type_name.find('function') != -1 or type_name.find('method') != -1:
p.type = 'function'
elif type_name == 'module':
p.type = 'module'
elif type_name == 'type':
p.type = 'class'
else:
p.type = 'instance'
result.append(p)
# if module is a package, check the direc tory
if hasattr(module,"__path__"):
in_dir_names = [os.path.split(n)[1] for n in glob.glob(os.path.join(module.__path__[0], "*"))]
in_dir_names = [n.replace(".py","") for n in in_dir_names
if not n.endswith(".pyc") and not n == "__init__.py"]
def __init__(self, pycore, ast_node, parent):
rope.base.pyobjects.AbstractFunction.__init__(self)
rope.base.pyobjects.PyDefinedObject.__init__(
self, pycore, ast_node, parent)
self.arguments = self.ast_node.args
self.parameter_pyobjects = pynames._Inferred(
self._infer_parameters, self.get_module()._get_concluded_data())
self.returned = pynames._Inferred(self._infer_returned)
self.parameter_pynames = None
def get_changes(self, new_parameter):
definition_info = functionutils.DefinitionInfo.read(self.pyfunction)
definition_info.args_with_defaults.append((new_parameter,
self._get_primary()))
collector = codeanalyze.ChangeCollector(self.resource.read())
header_start, header_end = self._get_header_offsets()
body_start, body_end = sourceutils.get_body_region(self.pyfunction)
collector.add_change(header_start, header_end,
definition_info.to_string())
self._change_function_occurances(collector, body_start,
body_end, new_parameter)
changes = rope.base.change.ChangeSet('Introduce parameter <%s>' %
new_parameter)
change = rope.base.change.ChangeContents(self.resource,
collector.get_changed())
changes.add_change(change)
return changes
def _default_config(self):
import rope.base.default_config
import inspect
return inspect.getsource(rope.base.default_config)
such a folder at all.
- `prefs`: Specify project preferences. These values
overwrite config file preferences.
"""
if projectroot != '/':
projectroot = _realpath(projectroot).rstrip('/\\')
self._address = projectroot
self._ropefolder_name = ropefolder
if not os.path.exists(self._address):
os.mkdir(self._address)
elif not os.path.isdir(self._address):
raise exceptions.RopeError('Project root exists and'
' is not a directory')
if fscommands is None:
fscommands = rope.base.fscommands.create_fscommands(self._address)
super(Project, self).__init__(fscommands)
self.ignored = _ResourceMatcher()
self.file_list = _FileListCacher(self)
self.prefs.add_callback('ignored_resources', self.ignored.set_patterns)
if ropefolder is not None:
self.prefs['ignored_resources'] = [ropefolder]
self._init_prefs(prefs)
self._init_source_folders()
def __init__(self, project_dir, cross_project_dirs=[]):
super(Project, self).__init__()
self.proj = rope.base.project.Project(project_dir)
self.cross_projects = dict()
cross_dirs = set(cross_project_dirs)
cross_dirs.discard(project_dir)
emap(self.add_cross_project, cross_dirs)
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,
def instance_to_pyobject(self, textual):
type = self.transform(textual[1])
if type is not None:
return rope.base.pyobjects.PyObject(type)
def _default_config(self):
import rope.base.default_config
import inspect
return inspect.getsource(rope.base.default_config)