Beispiel #1
0
def other_features():
    project = rope.base.project.Project('src', **ROPE_PREFS)
    project.validate(project.root)
    
    filename = osp.join('src', 'script2.py')
    source_code = file(filename, 'rb').read()
    offset = len(source_code)
    
    resource = rope.base.libutils.path_to_resource(project, filename)
    
    t0 = time.time()
    
    cts = rope.contrib.codeassist.get_calltip(
                                    project, source_code, offset, resource)
    doc_text = rope.contrib.codeassist.get_doc(
                                    project, source_code, offset, resource)
    def_loc = rope.contrib.codeassist.get_definition_location(
                                    project, source_code, offset, resource)
    
    msg = "Testing other rope instrospection features"
    print msg
    print "="*len(msg)
    print ""
    print "%s: %d ms" % ("elapsed time", 10*round(1e2*(time.time()-t0)))
    print ""
    print 'calltip:', cts
    print 'definition location:', def_loc
    print 'doc:'
    print '*** DOCUMENTATION ***' + '*'*60
    print doc_text
    print '*********************' + '*'*60
Beispiel #2
0
def other_features():
    project = rope.base.project.Project('src', **ROPE_PREFS)
    project.validate(project.root)

    filename = osp.join('src', 'script2.py')
    source_code = file(filename, 'rb').read()
    offset = len(source_code)

    resource = rope.base.libutils.path_to_resource(project, filename)

    t0 = time.time()

    cts = rope.contrib.codeassist.get_calltip(project, source_code, offset,
                                              resource)
    doc_text = rope.contrib.codeassist.get_doc(project, source_code, offset,
                                               resource)
    def_loc = rope.contrib.codeassist.get_definition_location(
        project, source_code, offset, resource)

    msg = "Testing other rope instrospection features"
    print msg
    print "=" * len(msg)
    print ""
    print "%s: %d ms" % ("elapsed time", 10 * round(1e2 * (time.time() - t0)))
    print ""
    print 'calltip:', cts
    print 'definition location:', def_loc
    print 'doc:'
    print '*** DOCUMENTATION ***' + '*' * 60
    print doc_text
    print '*********************' + '*' * 60
Beispiel #3
0
def get_result(changes, project, resource):
    # perform the changes
    project.do(changes)
    project.validate(resource)
    project.close()

    # get the refactored code from the temp file and return it
    file = io.open(TEMP_PATH)
    refactored_code = file.read()
    file.close()

    return refactored_code
Beispiel #4
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)
Beispiel #5
0
def ropetest():
    project = rope.base.project.Project('src', **ROPE_PREFS)
    project.validate(project.root)

    filename = osp.join('src', 'script.py')
    source_code = file(filename, 'rb').read()
    offset = len(source_code)

    resource = rope.base.libutils.path_to_resource(project, filename)

    t0 = time.time()

    proposals = rope.contrib.codeassist.code_assist(project, source_code,
                                                    offset, resource)
    proposals = rope.contrib.codeassist.sorted_proposals(proposals)

    print "%s: %d ms" % ("completion", 10 * round(1e2 * (time.time() - t0)))
    print 'loadtxt' in [proposal.name for proposal in proposals]
Beispiel #6
0
def ropetest():
    project = rope.base.project.Project('src', **ROPE_PREFS)
    project.validate(project.root)
    
    filename = osp.join('src', 'script.py')
    source_code = file(filename, 'rb').read()
    offset = len(source_code)
    
    resource = rope.base.libutils.path_to_resource(project, filename)
    
    t0 = time.time()
    
    proposals = rope.contrib.codeassist.code_assist(project, source_code,
                                                    offset, resource)
    proposals = rope.contrib.codeassist.sorted_proposals(proposals)
    
    print "%s: %d ms" % ("completion", 10*round(1e2*(time.time()-t0)))
    print 'loadtxt' in [proposal.name for proposal in proposals]
Beispiel #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)
Beispiel #8
0
    def get_project(self, project_root):
        """Return a project object for the given path.

        This caches previously used project objects so they do not
        have to be re-created.

        """
        if project_root is None:
            raise ValueError("No project root is specified, "
                             "but required for Rope")
        if not os.path.isdir(project_root):
            return None
        project = self.projects.get(project_root)
        if project is None:
            prefs = dict(ignored_resources=[
                '*.pyc', '*~', '.ropeproject', '.hg', '.svn', '_svn', '.git'
            ],
                         python_files=['*.py'],
                         save_objectdb=False,
                         compress_objectdb=False,
                         automatic_soa=True,
                         soa_followed_calls=0,
                         perform_doa=True,
                         validate_objectdb=True,
                         max_history_items=32,
                         save_history=False,
                         compress_history=False,
                         indent_size=4,
                         extension_modules=[],
                         import_dynload_stdmods=True,
                         ignore_syntax_errors=False,
                         ignore_bad_imports=False)
            project = self.projectlib.Project(project_root,
                                              ropefolder=None,
                                              **prefs)

            self.projects[project_root] = project
        last_validation = self.last_validation.get(project_root, 0.0)
        now = time.time()
        if (now - last_validation) > VALIDATE_EVERY_SECONDS:
            project.validate()
            self.last_validation[project_root] = now
        return project
Beispiel #9
0
    def get_project(self, project_root):
        """Return a project object for the given path.

        This caches previously used project objects so they do not
        have to be re-created.

        """
        if project_root is None:
            raise ValueError("No project root is specified, "
                             "but required for Rope")
        if not os.path.isdir(project_root):
            return None
        project = self.projects.get(project_root)
        if project is None:
            prefs = dict(ignored_resources=['*.pyc', '*~', '.ropeproject',
                                            '.hg', '.svn', '_svn', '.git'],
                         python_files=['*.py'],
                         save_objectdb=False,
                         compress_objectdb=False,
                         automatic_soa=True,
                         soa_followed_calls=0,
                         perform_doa=True,
                         validate_objectdb=True,
                         max_history_items=32,
                         save_history=False,
                         compress_history=False,
                         indent_size=4,
                         extension_modules=[],
                         import_dynload_stdmods=True,
                         ignore_syntax_errors=False,
                         ignore_bad_imports=False)
            project = self.projectlib.Project(project_root,
                                              ropefolder=None,
                                              **prefs)

            self.projects[project_root] = project
        last_validation = self.last_validation.get(project_root, 0.0)
        now = time.time()
        if (now - last_validation) > VALIDATE_EVERY_SECONDS:
            project.validate()
            self.last_validation[project_root] = now
        return project
Beispiel #10
0
    def goto_definition(self, *args):
        project = self.project
        if not project:
            project = getattr(self.editor, 'ropeproject', None)
            if not project:
                self.editor.update_message(_("Can't find project path"), "no", 1)
                return False

        project.validate()

        current_resource = self.get_rope_resource(project)

        try:
            resource, line = codeassist.get_definition_location(
                project, *self.get_source_and_offset(),
                resource=current_resource)
        except Exception, e:
            self.editor.update_message(str(e), "no", 1)
            traceback.print_exc()
            return False
Beispiel #11
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)
Beispiel #12
0
    def get_project(self, project_root):
        """Return a project object for the given path.

        This caches previously used project objects so they do not
        have to be re-created.

        """
        if project_root is None:
            raise ValueError("No project root is specified, "
                             "but required for Rope")
        if not os.path.isdir(project_root):
            return None
        project = self.projects.get(project_root)
        if project is None:
            project = self.projectlib.Project(project_root)
            self.projects[project_root] = project
        last_validation = self.last_validation.get(project_root, 0.0)
        now = time.time()
        if (now - last_validation) > VALIDATE_EVERY_SECONDS:
            project.validate()
            self.last_validation[project_root] = now
        return project
Beispiel #13
0
    def goto_definition(self, *args):
        project = self.project
        if not project:
            project = getattr(self.editor, 'ropeproject', None)
            if not project:
                self.editor.update_message(_("Can't find project path"), "no",
                                           1)
                return False

        project.validate()

        current_resource = self.get_rope_resource(project)

        try:
            resource, line = codeassist.get_definition_location(
                project,
                *self.get_source_and_offset(),
                resource=current_resource)
        except Exception, e:
            self.editor.update_message(str(e), "no", 1)
            traceback.print_exc()
            return False
Beispiel #14
0
    def get_project(self, project_root):
        """Return a project object for the given path.

        This caches previously used project objects so they do not
        have to be re-created.

        """
        if project_root is None:
            raise ValueError("No project root is specified, "
                             "but required for Rope")
        if not os.path.isdir(project_root):
            return None
        project = self.projects.get(project_root)
        if project is None:
            project = self.projectlib.Project(project_root)
            self.projects[project_root] = project
        last_validation = self.last_validation.get(project_root, 0.0)
        now = time.time()
        if (now - last_validation) > VALIDATE_EVERY_SECONDS:
            project.validate()
            self.last_validation[project_root] = now
        return project