Пример #1
0
def do_move(project, source, target_file):
    filefrom, offset = resourcespec_to_resource_offset(project, source)

    fileto = project.get_resource(target_file)

    mover = rope.refactor.move.create_move(project, filefrom, offset)
    changes = mover.get_changes(fileto)
    project.do(changes)
Пример #2
0
def make_temporary_project_and_resource(source_string):
    # write source text to string, because rope can only process whole files
    file = io.open(TEMP_PATH, mode='w')
    file.write(source_string)
    file.close()

    # make project and resource from the temp file
    project = rope.base.project.Project('.')
    resource = project.get_resource('temp.py')

    return project, resource
Пример #3
0
def list_command(path):
    """List the global entities in PATH.

    This will show things that might be used as arguments in invocations of
    other commands.
    """
    # Note that if we called this function 'list', it would collide with the
    # built-in.
    project = rope.base.project.Project(".", ropefolder=".clirope")
    resource = project.get_resource(path)
    project.validate(resource)
    with open(path) as f:
        print_offsets(f)
Пример #4
0
    def _Context(self, context):
        """
    Returns a (project, resource, source, offset) tuple for the context.
    """

        project = self._ProjectForFile(context.file_path).rope_project
        relative_path = os.path.relpath(context.file_path, project.address)
        resource = project.get_resource(relative_path)

        return (
            project,
            resource,
            context.source_text + "\n",
            context.cursor_position,
        )
Пример #5
0
  def _Context(self, context):
    """
    Returns a (project, resource, source, offset) tuple for the context.
    """

    project       = self._ProjectForFile(context.file_path).rope_project
    relative_path = os.path.relpath(context.file_path, project.address)
    resource      = project.get_resource(relative_path)

    return (
      project,
      resource,
      context.source_text + "\n",
      context.cursor_position,
    )
Пример #6
0
def resourcespec_to_resource_offset(project, resourcespec):
    if "::" in resourcespec:
        file_path, module_item = resourcespec.split("::")
    else:
        file_path = resourcespec
        module_item = None
    file_resource = project.get_resource(file_path)

    if module_item is not None:
        with open(file_path) as f:
            offset = get_offset_in_file(f, module_item)
    else:
        offset = None

    return file_resource, offset
Пример #7
0
def froms_to_imports(path):
    """Change the 'from X import Y' statements in PATH to 'import X.Y'.

    e.g.

      rope froms-to-imports mypackage/mymodule.py

    """
    project = rope.base.project.Project(".", ropefolder=".clirope")

    resource = project.get_resource(path)
    pymodule = project.get_pymodule(resource)
    project.validate(resource)

    tools = rope.refactor.importutils.ImportTools(project)
    new_content = tools.froms_to_imports(pymodule)

    pathlib.Path(path).write_text(new_content)
Пример #8
0
def organize_imports(path):
    """Organize the import statements in PATH in an opinionated way.

    In particular; unused or duplicate imports will be dropped, imports will be
    sorted and grouped, and the standard import group will appear first.

    e.g.

      rope organize_imports mypackage/mymodule.py

    """
    project = rope.base.project.Project(".", ropefolder=".clirope")

    resource = project.get_resource(path)
    pymodule = project.get_pymodule(resource)
    project.validate(resource)

    tools = rope.refactor.importutils.ImportTools(project)
    new_content = tools.organize_imports(pymodule)

    pathlib.Path(path).write_text(new_content)
Пример #9
0
def path_to_resource(project, path, type=None):
    """Get the resource at path

    You only need to specify `type` if `path` does not exist.  It can
    be either 'file' or 'folder'.  If the type is `None` it is assumed
    that the resource already exists.

    Note that this function uses `Project.get_resource()`,
    `Project.get_file()`, and `Project.get_folder()` methods.

    """
    project_path = path_relative_to_project_root(project, path)
    if project_path is None:
        project_path = rope.base.project._realpath(path)
        project = rope.base.project.get_no_project()
    if type is None:
        return project.get_resource(project_path)
    if type == "file":
        return project.get_file(project_path)
    if type == "folder":
        return project.get_folder(project_path)
    return None
Пример #10
0
def path_to_resource(project, path, type=None):
    """Get the resource at path

    You only need to specify `type` if `path` does not exist.  It can
    be either 'file' or 'folder'.  If the type is `None` it is assumed
    that the resource already exists.

    Note that this function uses `Project.get_resource()`,
    `Project.get_file()`, and `Project.get_folder()` methods.

    """
    project_path = relative(project.address, path)
    if project_path is None:
        project_path = rope.base.project._realpath(path)
        project = rope.base.project.get_no_project()
    if type is None:
        return project.get_resource(project_path)
    if type == 'file':
        return project.get_file(project_path)
    if type == 'folder':
        return project.get_folder(project_path)
    return None