rope.base.resources

Files and folders in a project are represented as resource objects.

Files and folders are access through Resource objects. Resource has two subclasses: File and Folder. What we care about is that refactorings and `rope.base.change.Change`s use resources.

There are two options to create a Resource for a path in a project. Note that in these examples path is the path to a file or folder relative to the project’s root. A project’s root folder is represented by an empty string.

  1. Use the rope.base.Project.get_resource() method. E.g.:

    myresource = myproject.get_resource(path)

  2. Use the rope.base.libutils module. libutils 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)

Once we have a Resource, we can retrieve information from it, like getting the path relative to the project’s root (via path), reading from and writing to the resource, moving the resource, etc.

Classes

Resource

Represents files and folders in a project

File

Represents a file

Folder

Represents a folder

Module Contents

class rope.base.resources.Resource(project, path)

Bases: os.PathLike

Represents files and folders in a project

project
move(new_location)

Move resource to new_location

remove()

Remove resource from the project

is_dir()

Alias for is_folder()

is_folder()

Return True if the resource is a Folder

create()

Create this resource

exists()
property parent
property path: str

Return the path of this resource relative to the project root

The path is the list of parent directories separated by ‘/’ followed by the resource name.

property name: str

Return the name of this resource

property real_path: str
property pathlib: pathlib.Path

Return the file as a pathlib path.

class rope.base.resources.File(project, name)

Bases: Resource

Represents a file

newlines = None
read()
read_bytes()
write(contents)
is_folder()

Return True if the resource is a Folder

create()

Create this resource

project
move(new_location)

Move resource to new_location

remove()

Remove resource from the project

is_dir()

Alias for is_folder()

exists()
property parent
property path: str

Return the path of this resource relative to the project root

The path is the list of parent directories separated by ‘/’ followed by the resource name.

property name: str

Return the name of this resource

property real_path: str
property pathlib: pathlib.Path

Return the file as a pathlib path.

class rope.base.resources.Folder(project, path)

Bases: Resource

Represents a folder

is_folder()

Return True if the resource is a Folder

get_children()

Return the children of this folder

create_file(file_name)
create_folder(folder_name)
get_child(name)
has_child(name)
get_files()
get_folders()
contains(resource)
create()

Create this resource

project
move(new_location)

Move resource to new_location

remove()

Remove resource from the project

is_dir()

Alias for is_folder()

exists()
property parent
property path: str

Return the path of this resource relative to the project root

The path is the list of parent directories separated by ‘/’ followed by the resource name.

property name: str

Return the name of this resource

property real_path: str
property pathlib: pathlib.Path

Return the file as a pathlib path.