rope.refactor.restructure

Classes

Restructure

A class to perform python restructurings

Functions

replace(code, pattern, goal)

used by other refactorings

Module Contents

class rope.refactor.restructure.Restructure(project, pattern, goal, args=None, imports=None, wildcards=None)

A class to perform python restructurings

A restructuring transforms pieces of code matching pattern to goal. In the pattern wildcards can appear. Wildcards match some piece of code based on their kind and arguments that are passed to them through args.

args is a dictionary of wildcard names to wildcard arguments. If the argument is a tuple, the first item of the tuple is considered to be the name of the wildcard to use; otherwise the “default” wildcard is used. For getting the list arguments a wildcard supports, see the pydoc of the wildcard. (see rope.refactor.wildcard.DefaultWildcard for the default wildcard.)

wildcards is the list of wildcard types that can appear in pattern. See rope.refactor.wildcards. If a wildcard does not specify its kind (by using a tuple in args), the wildcard named “default” is used. So there should be a wildcard with “default” name in wildcards.

imports is the list of imports that changed modules should import. Note that rope handles duplicate imports and does not add the import if it already appears.

Example #1:

pattern ${pyobject}.get_attribute(${name})
goal ${pyobject}[${name}]
args pyobject: instance=rope.base.pyobjects.PyObject

Example #2:

pattern ${name} in ${pyobject}.get_attributes()
goal ${name} in {pyobject}
args pyobject: instance=rope.base.pyobjects.PyObject

Example #3:

pattern ${pycore}.create_module(${project}.root, ${name})
goal generate.create_module(${project}, ${name})

imports
 from rope.contrib import generate

args
 project: type=rope.base.project.Project

Example #4:

pattern ${pow}(${param1}, ${param2})
goal ${param1} ** ${param2}
args pow: name=mod.pow, exact

Example #5:

pattern ${inst}.longtask(${p1}, ${p2})
goal
 ${inst}.subtask1(${p1})
 ${inst}.subtask2(${p2})
args
 inst: type=mod.A,unsure
project
pattern
goal
args = None
imports = None
wildcards = None
template
get_changes(checks=None, imports=None, resources=None, task_handle=taskhandle.DEFAULT_TASK_HANDLE)

Get the changes needed by this restructuring

resources can be a list of rope.base.resources.File to apply the restructuring on. If None, the restructuring will be applied to all python files.

checks argument has been deprecated. Use the args argument of the constructor. The usage of:

strchecks = {'obj1.type': 'mod.A', 'obj2': 'mod.B',
             'obj3.object': 'mod.C'}
checks = restructuring.make_checks(strchecks)

can be replaced with:

args = {'obj1': 'type=mod.A', 'obj2': 'name=mod.B',
        'obj3': 'object=mod.C'}

where obj1, obj2 and obj3 are wildcard names that appear in restructuring pattern.

make_checks(string_checks)

Convert str to str dicts to str to PyObject dicts

This function is here to ease writing a UI.

rope.refactor.restructure.replace(code, pattern, goal)

used by other refactorings