rope.refactor.occurrences

Find occurrences of a name in a project.

This module consists of a Finder that finds all occurrences of a name in a project. The Finder.find_occurrences() method is a generator that yields Occurrence instances for each occurrence of the name. To create a Finder object, use the create_finder() function:

finder = occurrences.create_finder(project, ‘foo’, pyname) for occurrence in finder.find_occurrences():

pass

It’s possible to filter the occurrences. They can be specified when calling the create_finder() function.

  • only_calls: If True, return only those instances where the name is a function that’s being called.

  • imports: If False, don’t return instances that are in import statements.

  • unsure: If a predicate function, return instances where we don’t know what the name references. It also filters based on the predicate function.

  • docs: If True, it will search for occurrences in regions normally ignored. E.g., strings and comments.

  • in_hierarchy: If True, it will find occurrences if the name is in the class’s hierarchy.

  • instance: Used only when you want implicit interfaces to be considered.

  • keywords: If False, don’t return instances that are the names of keyword arguments

Classes

Finder

For finding occurrences of a name

Occurrence

PyNameFilter

For finding occurrences of a name.

InHierarchyFilter

Finds the occurrence if the name is in the class's hierarchy.

UnsureFilter

Occurrences where we don't knoow what the name references.

NoImportsFilter

Don't include import statements as occurrences.

CallsFilter

Filter out non-call occurrences.

NoKeywordsFilter

Filter out keyword parameters.

Functions

create_finder(project, name, pyname[, only_calls, ...])

A factory for Finder

same_pyname(expected, pyname)

Check whether expected and pyname are the same

unsure_pyname(pyname[, unbound])

Return True if we don't know what this name references

Module Contents

class rope.refactor.occurrences.Finder(project, name, filters=None, docs=False)

For finding occurrences of a name

The constructor takes a filters argument. It should be a list of functions that take a single argument. For each possible occurrence, these functions are called in order with the an instance of Occurrence:

  • If it returns None other filters are tried.

  • If it returns True, the occurrence will be a match.

  • If it returns False, the occurrence will be skipped.

  • If all of the filters return None, it is skipped also.

project
name
docs = False
filters = None
find_occurrences(resource=None, pymodule=None)

Generate Occurrence instances

rope.refactor.occurrences.create_finder(project, name, pyname, only_calls=False, imports=True, unsure=None, docs=False, instance=None, in_hierarchy=False, keywords=True)

A factory for Finder

Based on the arguments it creates a list of filters. instance argument is needed only when you want implicit interfaces to be considered.

class rope.refactor.occurrences.Occurrence(tools, offset)
tools
offset
resource
get_word_range()
get_primary_range()
get_pyname()
get_primary_and_pyname()
is_in_import_statement()
is_called()
is_defined()
is_a_fixed_primary()
is_written()
is_unsure()
is_function_keyword_parameter()
property lineno
rope.refactor.occurrences.same_pyname(expected, pyname)

Check whether expected and pyname are the same

rope.refactor.occurrences.unsure_pyname(pyname, unbound=True)

Return True if we don’t know what this name references

class rope.refactor.occurrences.PyNameFilter(pyname)

For finding occurrences of a name.

pyname
class rope.refactor.occurrences.InHierarchyFilter(pyname, implementations_only=False)

Finds the occurrence if the name is in the class’s hierarchy.

pyname
impl_only = False
pyclass
class rope.refactor.occurrences.UnsureFilter(unsure)

Occurrences where we don’t knoow what the name references.

unsure
class rope.refactor.occurrences.NoImportsFilter

Don’t include import statements as occurrences.

class rope.refactor.occurrences.CallsFilter

Filter out non-call occurrences.

class rope.refactor.occurrences.NoKeywordsFilter

Filter out keyword parameters.