/usr/share/pyshared/rope/docs/library.txt is in python-rope 0.9.2-1.
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 | =========================
Using Rope As A Library
=========================
If you need other features, send a feature request. Have a look at
`contributing.txt`_.
.. contents:: Table of Contents
Quick Start
===========
This section will help you get started as soon as possible.
Making A Project
----------------
The first thing you should do is to make a project::
import rope.base.project
myproject = rope.base.project.Project('/path/to/myproject')
It's good to know that:
* A project is a folder in the file-system.
* It can contain anything.
* Rope searches for python modules and packages inside a project when
needed.
* Refactorings only change files and folders inside the project that
has been passed to them.
* Out of project modules that are imported from a module inside a
project are handled but never changed by refactorings.
* Rope makes a rope folder inside projects. By default the name of
this folder is ``.ropeproject`` but that can be changed using the
constructor's `ropefolder` parameter (passing `None` will prevent
rope from making this folder).
* Rope uses ``.ropeproject`` folder for things like saving object
information and loading project configurations.
* Project preferences can be configured by passing options to the
constructor or in ``.ropeproject/config.py``. See the default
``config.py``, `rope.base.default_config` module, for more
information.
* All configurations that are available in the ``config.py`` file can
be specified as keyword parameters to `Project` constructor. These
parameters override the ones in the ``config.py`` file.
* Each project has a set of ignored resource patterns; You can use it
to tell rope to ignore files and folders matching certain patterns.
* The ``.ropeproject`` folder can be safely copied in other clones of
a project if you don't want to lose your objectdb and history.
Library Utilities
-----------------
The `rope.base.libutils` module provides tools for making using rope
as a library easier. We'll talk more about this module later.
What Are These `Resource`\s?
----------------------------
In rope, files and folders in a project are accessed through
`rope.base.resources.Resource` objects. It has two subclasses `File`
and `Folder`. What we care about is that refactorings and `Change`\s
(we'll talk about them later) use resources.
In order to create a `Resource` for a path in a project we have two
options. The first approach uses the `Project` object (use
`Project.get_resource()`_ method). I prefer to describe the second
approach since it needs less to know.
We can use `libutils` module. It has a function named
`path_to_resource()`. It takes a project and a path::
from rope.base import libutils
myresource = libutils.path_to_resource(myproject, '/path/to/resource')
But this is only half of the answer. Consider we have a resource.
How can we know anything about it? The answer is to use its ``path``
and ``real_path`` fields. `Resource.real_path` is the absolute path
of the resource in the file-system. `Resource.path` field contains
the address of a resource relative to project root (the same format as
needed by `Project.get_resource()`_).
Performing Refactorings
-----------------------
There are examples at the end of this document. But as another
example we'll extract a variable in a file. First we need the
`Resource` object that points to a file in a project::
resource = libutils.path_to_resource(myproject, '/path/to/my/module.py')
So we can make our Refactoring class::
from rope.refactor.extract import ExtractVariable
extractor = ExtractVariable(myproject, resource, start, end)
Where `start` and `end` are the offsets of the region to extract in
resource. Be careful when calculating the offsets. Dos line-endings
and multi-byte characters are considered to be only one character.
This is actually easier for IDEs, since most GUI libraries do that
when calculating offsets.
Next, IDE's usually pop up a dialog for letting the user configure
refactoring options like the name of the extracted variable.
After that, we can calculate the changes::
changes = extractor.get_changes('extracted_variable')
`changes` holds the changes this refactoring makes. Calculating it
might be time consuming; See `rope.base.taskhandle.TaskHandle`_
section for measuring its progress or interrupting it.
Previewing And Performing Changes
---------------------------------
As mentioned in the last section each refactoring returns a
`rope.base.change.Change` object. Now how can we know what it
contains and how to perform it?
*Previewing*:
``str(changes)`` returns a short description of the changes. You can
use ``changes.get_description()`` to get a preview; it is useful when
you don't care much about the format. Otherwise you can use the
``changes`` object directly. See the documentation in
`rope.base.change` module.
*Performing*:
The easiest way for performing the refactoring is to use
`Project.do()`_ method::
myproject.do(changes)
If you want to perform the changes yourself, you have two options.
Note that the main reason for performing the changes manually is
handling version control systems that are not supported by rope.
The first approach is to use `rope.base.fscommands`_. See `Writing A
FileSystemCommands`_ section. The changes can be performed as before
using `Project.do()`.
The other is to perform the changes manually based on the returned
`changes` object (again see the documentation in `rope.base.change`
module). If this approach is used you cannot undo the refactoring
using ``project.history.undo()``.
*Updating Open Buffers In IDEs*:
Usually editors need to reload the files changed by rope. You can use
``Change.get_changed_resources()`` to get the list of resources that
need to be reloaded.
Validating The Project
----------------------
When using rope as a library, you probably change the files in it in
parallel (for example in IDEs). To force rope to invalidate cached
information about resources that have been removed or changed outside
rope you should call `Project.validate()`_ method. You can pass a
resource to this method. For example::
myproject.validate()
validates all files and directories in the project. So call this
function every time you want use rope (before performing refactorings,
for instance).
Activating Static Object Analysis
---------------------------------
One of the greatest strengths of rope is its static object analysis,
SOA. You can perform SOA on a module using `PyCore.analyze_module()`
method but performing SOA on a module is not cheap. So I decided that
the best time for performing SOA is when saving files and only
performing it on changed scopes.
But since rope is not notified about the changes the IDE performs, you
should tell rope about the change. You can do so by using
`rope.base.libutils.report_change()`. That is, whenever you want to
change a module you can do something like::
# Do the actual writing
old_contents = read(path)
write(path, new_content)
# Inform rope about the change
libutils.report_change(myproject, path, old_contents)
Where `read` and `write` stand for methods used for reading and
writing files.
Closing The Project
-------------------
`Project.close()`_ closes project open resources. Always call this
function when you don't need a project anymore::
myproject.close()
`rope.base.project.Project`
===========================
You can create a project by::
project = Project(root_address)
Where the `root_address` is the root folder of your project.
A project has some useful fields. `Project.address` is the address of
the root folder of a project. `Project.root` is a `Folder` object
that points to that folder.
`Project.get_resource()`
------------------------
You can use this method for getting a resource (that is file or
folder) inside a project. Uses ``'/'``s for separating directories.
For instance ``project.get_resource('my_folder/my_file.txt')`` returns
a `rope.base.resources.File` object that points to
``${projectroot}/my_folder/my_file.txt`` file.
Note that this method assumes the resource exists. If it does not
exist you can use `Project.get_file()` and `Project.get_folder()`
methods.
`Project.do()`
--------------
For committing changes returned by refactorings.
`Project.history`
-----------------
A `rope.base.history.History` object. You can use its `undo` and
`redo` methods for undoing or redoing changes. Note that you can use
it only if you have committed your changes using rope.
`Project.validate()`
--------------------
When using rope as a library you probably change the files in that
project in parallel (for example in IDEs). To force rope to
invalidate cached information about resources that have been
removed or changed outside rope you should call `Project.validate`.
You should pass a resource to this method. For example::
project.validate(project.root)
validates all files and directories in the project.
`Project.close()`
-----------------
Closes project open resources. Always call this function when you
don't need a project anymore. Currently it closes the files used for
storing object information and project history. Since some parts of
these files are in memory for efficiency not closing a project might
put them in an inconsistent state.
`rope.base.fscommands`
----------------------
The `rope.base.fscommands` module implements the basic file system
operations that rope needs to perform. The main reason for the
existence of this module is supporting version control systems. Have
a look at `FileSystemCommands` and `SubversionCommands` in the same
module. If you need other version control systems you can write a new
class that provides this interface. `rope.base.project.Project`
accepts a ``fscommands`` argument. You can use this argument to force
rope to use your new class.
``.ropeproject`` Folder
-----------------------
From version ``0.5``, rope makes a ``.ropeproject`` folder in the
project by default for saving project configurations and data. The
name of this folder is passed to the constructor if you want to change
that. Also you can force rope not to make such a folder by passing
`None`.
If such a folder exists rope loads the ``config.py`` file in that
folder. It might also use it for storing object information and
history.
`rope.base.pycore.PyCore`
=========================
Provides useful methods for managing python modules and packages.
Each project has a `PyCore` that can be accessed using
`Project.pycore` attribute.
`PyCore.run_module()` runs a resource. When running, it collects type
information to do dynamic object inference. For this reason modules
run much slower.
Also `Pycore.analyze_module()` collects object information for a
module. The collected information can be used to enhance rope's
static object inference.
`rope.base.taskhandle.TaskHandle`
=================================
Can be used for stopping and monitoring the progress of time consuming
tasks like some of refactorings. `Project.do()` and
`Refactoring.get_changes()` of most refactorings take a keyword
parameter called ``task_handle``. You can pass a `TaskHandle` object
to them. A `TaskHandle` can be used for interrupting or observing a
task.
Always pass ``task_handle`` as keyword argument; it will always be the
last argument and new arguments of the refactoring are added before
it.
A task might consist of a few `JobSet`\s. Each `JobSet` does a few
jobs. For instance calculating the changes for renaming a method in a
class hierarchy has two job sets; We need to find the classes for
constructing the class hierarchy and then we need to change the
occurrences.
The `TaskHandle.current_jobset()` returns the most recent `JobSet` or
`None` if none has been started. You can use the methods of `JobSet`
for obtaining information about the current job. So you might want to
do something like::
import rope.base.taskhandle
handle = rope.base.taskhandle.TaskHandle("Test Task")
def update_progress():
jobset = handle.current_jobsets()
if jobset:
text = ''
# getting current job set name
if jobset.get_name() is not None:
text += jobset.get_name()
# getting active job name
if jobset.get_active_job_name() is not None:
text += ' : ' + jobset.get_active_job_name()
# adding done percent
percent = jobset.get_percent_done()
if percent is not None:
text += ' ... %s percent done' % percent
print text
handle.add_observer(update_progress)
changes = renamer.get_changes('new_name', task_handle=handle)
Also you can use something like this for stopping the task::
def stop():
handle.stop()
After calling ``stop()``, the thread that is executing the task will
be interrupted by a `rope.base.exceptions.InterruptedTaskError`
exception.
Refactorings
============
Have a look at `rope.refactor` package and its sub-modules. For
example for performing a move refactoring you can create a
`Move` object like this::
mover = Move(project, resource, offset)
Where `resource` and `offset` is the location to perform the
refactoring.
Then you can commit the changes by it using `get_changes()` method::
project.do(mover.get_changes(destination))
Where `destination` module/package is the destination resource for
move refactoring. Other refactorings classes have a similar
interface.
List Of Refactorings
--------------------
Here is the list of refactorings rope provides. Note that this list
might be out of date. For more information about these refactoring
see pydocs in their modules and the unit-tests in the
``ropetest/refactor/`` folder.
* `rope.refactor.rename`:
Rename something in the project. See the example below.
* `rope.refactor.move`:
Move a python element in the project.
* `rope.refactor.restructure`:
Restructure code. See the example below.
* `rope.refactor.extract`:
Extract methods/variables.
* `rope.refactor.inline`:
Inline occurrences of a method/variable/parameter.
* `rope.refactor.usefunction`:
Try to use a function wherever possible.
* `rope.refactor.method_object`:
Transform a function or a method to a method object.
* `rope.refactor.change_signature`:
Change the signature of a function/method.
* `rope.refactor.introduce_factory`:
Introduce a factory for a class and changes all constructors to use
it.
* `rope.refactor.introduce_parameter`:
Introduce a parameter in a function.
* `rope.refactor.encapsulate_field`:
Generate a getter/setter for a field and changes its occurrences to
use them.
* `rope.refactor.localtofield`:
Change a local variable to field
* `rope.refactor.topackage`:
Transform a module to a package with the same name.
* `rope.refactor.importutils`:
Perform actions like organize imports.
Refactoring Resources Parameter
-------------------------------
Some refactorings, restructure and find occurrences accept an argument
called ``resources``. If it is a list of `File`\s, all other
resources in the project are ignored and the refactoring only analyzes
them; if it is `None` all python modules in the project will be
analyzed. Using this parameter, IDEs can let the user limit the files
on which a refactoring should be applied.
Examples
========
Rename
------
Using rename refactoring::
# Creating a project
>>> from rope.base.project import Project
>>> project = Project('.')
# Working with files to create a module
>>> mod1 = project.root.create_file('mod1.py')
>>> mod1.write('a_var = 10\n')
# Alternatively you can use `generate` module.
# Creating modules and packages using `generate` module
>>> from rope.contrib import generate
>>> pycore = project.pycore
>>> pkg = generate.create_package(project, 'pkg')
>>> mod2 = generate.create_module(project, 'mod2', pkg)
>>> mod2.write('import mod1\nprint mod1.a_var\n')
# We can use `PyCore.find_module` for finding modules, too
>>> assert mod2 == pycore.find_module('pkg.mod2')
# Performing rename refactoring on `mod1.a_var`
>>> from rope.refactor.rename import Rename
>>> changes = Rename(project, mod1, 1).get_changes('new_var')
>>> project.do(changes)
>>> mod1.read()
u'new_var = 10\n'
>>> mod2.read()
u'import mod1\nprint mod1.new_var\n'
# Undoing rename refactoring
>>> project.history.undo()
...
>>> mod1.read()
u'a_var = 10\n'
>>> mod2.read()
u'import mod1\nprint mod1.a_var\n'
# Cleaning up
>>> pkg.remove()
>>> mod1.remove()
>>> project.close()
Restructuring
-------------
The example for replacing occurrences of our `pow` function to ``**``
operator (see the restructuring section of `overview.txt`_)::
# Setting up the project
>>> from rope.base.project import Project
>>> project = Project('.')
>>> mod1 = project.root.create_file('mod1.py')
>>> mod1.write('def pow(x, y):\n result = 1\n'
... ' for i in range(y):\n result *= x\n'
... ' return result\n')
>>> mod2 = project.root.create_file('mod2.py')
>>> mod2.write('import mod1\nprint(mod1.pow(2, 3))\n')
>>> from rope.refactor import restructure
>>> pattern = '${pow_func}(${param1}, ${param2})'
>>> goal = '${param1} ** ${param2}'
>>> args = {'pow_func': 'name=mod1.pow'}
>>> restructuring = restructure.Restructure(project, pattern, goal, args)
>>> project.do(restructuring.get_changes())
>>> mod2.read()
u'import mod1\nprint(2 ** 3)\n'
# Cleaning up
>>> mod1.remove()
>>> mod2.remove()
>>> project.close()
See code documentation and test suites for more information.
.. _overview.txt: overview.html
.. _contributing.txt: contributing.html
Other Topics
============
Writing A `FileSystemCommands`
------------------------------
The `get_changes()` method of refactoring classes return a
`rope.base.change.Change` object. You perform these changes by
calling `Project.do()`. But as explained above some IDEs need to
perform the changes themselves.
Every change to file-system in rope is commited using an object that
provides `rope.base.fscommands.FileSystemCommands` interface. As
explained above in `rope.base.fscommands`_ section, rope uses this
interface to handle different VCSs.
You can implement your own fscommands object::
class MyFileSystemCommands(object):
def create_file(self, path):
"""Create a new file"""
# ...
def create_folder(self, path):
"""Create a new folder"""
# ...
def move(self, path, new_location):
"""Move resource at `path` to `new_location`"""
# ...
def remove(self, path):
"""Remove resource"""
# ...
def write(self, path, data):
"""Write `data` to file at `path`"""
# ...
And you can create a project like this::
my_fscommands = MyFileSystemCommands()
project = rope.base.project.Project('~/myproject',
fscommands=my_fscommands)
`rope.contrib.codeassist`
-------------------------
The `rope.contrib` package contains modules that use rope base parts
and provide useful features. `rope.contrib.codeassist` module can
be used in IDEs::
from rope.ide import codeassist
# Get the proposals; you might want to pass a Resource
proposals = codeassist.code_assist(project, source_code, offset)
# Sorting proposals; for changing the order see pydoc
proposals = codeassist.sorted_proposals(proposals)
# Where to insert the completions
starting_offset = codeassist.starting_offset(source_code, offset)
# Applying a proposal
proposal = proposals[x]
replacement = proposal.name
new_source_code = (source_code[:starting_offset] +
replacement + source_code[offset:])
`maxfixes` parameter of `code_assist` decides how many syntax errors
to fix. The default value is one. For instance::
def f():
g(my^
myvariable = None
def g(p):
invalid syntax ...
will report `myvariable`, only if `maxfixes` is bigger than 1.
`later_locals`, if `True`, forces rope to propose names that are
defined later in current scope. It is `True` by default. For
instance::
def f():
my^
myvariable = None
will not report `myvariable`, if `later_locals` is False.
See pydocs and source code for more information (other functions in
this module might be interesting, too; like `get_doc`,
`get_definition_location`).
`rope.contrib.findit`
---------------------
`findit` module provides `find_occurrences()` for finding occurrences
of a name. Also `find_implementations()` function finds the places in
which a method is overridden.
`rope.contrib.autoimport`
-------------------------
This module can be used to find the modules that provide a name. IDEs
can use this module to auto-import names. `AutoImport.get_modules()`
returns the list of modules with the given global name.
`AutoImport.import_assist()` tries to find the modules that have a
global name that starts with the given prefix.
Cross-Project Refactorings
--------------------------
`rope.refactor.multiproject` can be used to perform a refactoring
across multiple projects.
Usually refactorings have a main project. That is the project that
contains the definition of the changing python name. Other projects
depend on the main one and uses of the changed name in them should be
updated.
Each refactoring changes only one project (the project passed to its
constructor). But we can use `MultiProjectRefactoring` proxy to
perform a refactoring on other projects, too.
First we need to create a multi-project refactoring constructor. As
an example consider we want to perform a rename refactoring::
from rope.refactor import multiproject, rename
CrossRename = multiproject.MultiProjectRefactoring(rename.Rename,
projects)
Here `projects` is the list of dependant projects; it does not include
the main project. The first argument is the refactoring class (such
as `Rename`) or factory function (like `create_move`).
Next we can construct the refactoring::
renamer = CrossRename(project, resource, offset)
We create the rename refactoring as we do for normal refactorings.
Note that `project` is the main project.
As mentioned above, other projects use the main project; rope
automatically adds the main project to the python path of other
projects.
Finally we can calculate the changes. But instead of calling
`get_changes()` (which returns main project changes, only), we can
call `get_all_changes()` with the same arguments. It returns a list
of ``(project, changes)`` tuples. You can perform them manually by
calling ``project.do(changes)`` for each tuple or use
`multiproject.perform()`::
project_and_changes = renamer.get_all_changes('newname')
multiproject.perform(project_and_changes)
|