Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
marvel
GitHub Repository: marvel/qnf
Path: blob/master/elisp/emacs-for-python/rope-dist/rope/base/libutils.py
1415 views
1
"""A few useful functions for using rope as a library"""
2
import os.path
3
4
import rope.base.project
5
import rope.base.pycore
6
from rope.base import taskhandle
7
8
9
def path_to_resource(project, path, type=None):
10
"""Get the resource at path
11
12
You only need to specify `type` if `path` does not exist. It can
13
be either 'file' or 'folder'. If the type is `None` it is assumed
14
that the resource already exists.
15
16
Note that this function uses `Project.get_resource()`,
17
`Project.get_file()`, and `Project.get_folder()` methods.
18
19
"""
20
project_path = relative(project.address, path)
21
if project_path is None:
22
project_path = rope.base.project._realpath(path)
23
project = rope.base.project.get_no_project()
24
if type is None:
25
return project.get_resource(project_path)
26
if type == 'file':
27
return project.get_file(project_path)
28
if type == 'folder':
29
return project.get_folder(project_path)
30
return None
31
32
def relative(root, path):
33
root = rope.base.project._realpath(root).replace(os.path.sep, '/')
34
path = rope.base.project._realpath(path).replace(os.path.sep, '/')
35
if path == root:
36
return ''
37
if path.startswith(root + '/'):
38
return path[len(root) + 1:]
39
40
def report_change(project, path, old_content):
41
"""Report that the contents of file at `path` was changed
42
43
The new contents of file is retrieved by reading the file.
44
45
"""
46
resource = path_to_resource(project, path)
47
if resource is None:
48
return
49
for observer in list(project.observers):
50
observer.resource_changed(resource)
51
if project.pycore.automatic_soa:
52
rope.base.pycore.perform_soa_on_changed_scopes(project, resource,
53
old_content)
54
55
def analyze_modules(project, task_handle=taskhandle.NullTaskHandle()):
56
"""Perform static object analysis on all python files in the project
57
58
Note that this might be really time consuming.
59
"""
60
resources = project.pycore.get_python_files()
61
job_set = task_handle.create_jobset('Analyzing Modules', len(resources))
62
for resource in resources:
63
job_set.started_job(resource.path)
64
project.pycore.analyze_module(resource)
65
job_set.finished_job()
66
67