Path: blob/master/elisp/emacs-for-python/rope-dist/rope/base/libutils.py
1415 views
"""A few useful functions for using rope as a library"""1import os.path23import rope.base.project4import rope.base.pycore5from rope.base import taskhandle678def path_to_resource(project, path, type=None):9"""Get the resource at path1011You only need to specify `type` if `path` does not exist. It can12be either 'file' or 'folder'. If the type is `None` it is assumed13that the resource already exists.1415Note that this function uses `Project.get_resource()`,16`Project.get_file()`, and `Project.get_folder()` methods.1718"""19project_path = relative(project.address, path)20if project_path is None:21project_path = rope.base.project._realpath(path)22project = rope.base.project.get_no_project()23if type is None:24return project.get_resource(project_path)25if type == 'file':26return project.get_file(project_path)27if type == 'folder':28return project.get_folder(project_path)29return None3031def relative(root, path):32root = rope.base.project._realpath(root).replace(os.path.sep, '/')33path = rope.base.project._realpath(path).replace(os.path.sep, '/')34if path == root:35return ''36if path.startswith(root + '/'):37return path[len(root) + 1:]3839def report_change(project, path, old_content):40"""Report that the contents of file at `path` was changed4142The new contents of file is retrieved by reading the file.4344"""45resource = path_to_resource(project, path)46if resource is None:47return48for observer in list(project.observers):49observer.resource_changed(resource)50if project.pycore.automatic_soa:51rope.base.pycore.perform_soa_on_changed_scopes(project, resource,52old_content)5354def analyze_modules(project, task_handle=taskhandle.NullTaskHandle()):55"""Perform static object analysis on all python files in the project5657Note that this might be really time consuming.58"""59resources = project.pycore.get_python_files()60job_set = task_handle.create_jobset('Analyzing Modules', len(resources))61for resource in resources:62job_set.started_job(resource.path)63project.pycore.analyze_module(resource)64job_set.finished_job()656667