/usr/share/pyshared/rope/contrib/findit.py is in python-rope 0.9.2-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | import rope.base.codeanalyze
import rope.base.evaluate
import rope.base.pyobjects
from rope.base import taskhandle, exceptions, worder
from rope.contrib import fixsyntax
from rope.refactor import occurrences
def find_occurrences(project, resource, offset, unsure=False, resources=None,
in_hierarchy=False, task_handle=taskhandle.NullTaskHandle()):
"""Return a list of `Location`\s
If `unsure` is `True`, possible matches are returned, too. You
can use `Location.unsure` to see which are unsure occurrences.
`resources` can be a list of `rope.base.resource.File`\s that
should be searched for occurrences; if `None` all python files
in the project are searched.
"""
name = worder.get_name_at(resource, offset)
this_pymodule = project.pycore.resource_to_pyobject(resource)
primary, pyname = rope.base.evaluate.eval_location2(
this_pymodule, offset)
def is_match(occurrence):
return unsure
finder = occurrences.create_finder(
project.pycore, name, pyname, unsure=is_match,
in_hierarchy=in_hierarchy, instance=primary)
if resources is None:
resources = project.pycore.get_python_files()
job_set = task_handle.create_jobset('Finding Occurrences',
count=len(resources))
return _find_locations(finder, resources, job_set)
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.pycore.get_python_files()
job_set = task_handle.create_jobset('Finding Implementations',
count=len(resources))
return _find_locations(finder, resources, job_set)
def find_definition(project, code, offset, resource=None, maxfixes=1):
"""Return the definition location of the python name at `offset`
A `Location` object is returned if the definition location can be
determined, otherwise ``None`` is returned.
"""
fixer = fixsyntax.FixSyntax(project.pycore, code, resource, maxfixes)
main_module = fixer.get_pymodule()
pyname = fixer.pyname_at(offset)
if pyname is not None:
module, lineno = pyname.get_definition_location()
name = rope.base.worder.Worder(code).get_word_at(offset)
if lineno is not None:
start = module.lines.get_line_start(lineno)
def check_offset(occurrence):
if occurrence.offset < start:
return False
pyname_filter = occurrences.PyNameFilter(pyname)
finder = occurrences.Finder(project.pycore, name,
[check_offset, pyname_filter])
for occurrence in finder.find_occurrences(pymodule=module):
return Location(occurrence)
class Location(object):
def __init__(self, occurrence):
self.resource = occurrence.resource
self.region = occurrence.get_word_range()
self.offset = self.region[0]
self.unsure = occurrence.is_unsure()
self.lineno = occurrence.lineno
def _find_locations(finder, resources, job_set):
result = []
for resource in resources:
job_set.started_job(resource.path)
for occurrence in finder.find_occurrences(resource):
result.append(Location(occurrence))
job_set.finished_job()
return result
|