/
resources.py
271 lines (211 loc) · 7.75 KB
/
resources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""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.
"""
import os
import re
import warnings
from rope.base import change
from rope.base import exceptions
from rope.base import fscommands
from pathlib import Path
class Resource:
"""Represents files and folders in a project"""
def __init__(self, project, path):
self.project = project
self._path = path
def __repr__(self):
return '<{}.{} "{}" at {}>'.format(
self.__class__.__module__,
self.__class__.__name__,
self.path,
hex(id(self)),
)
def move(self, new_location):
"""Move resource to `new_location`"""
self._perform_change(
change.MoveResource(self, new_location),
f"Moving <{self.path}> to <{new_location}>",
)
def remove(self):
"""Remove resource from the project"""
self._perform_change(change.RemoveResource(self), "Removing <%s>" % self.path)
def is_folder(self):
"""Return true if the resource is a folder"""
def create(self):
"""Create this resource"""
def exists(self):
return os.path.exists(self.real_path)
@property
def parent(self):
parent = "/".join(self.path.split("/")[0:-1])
return self.project.get_folder(parent)
@property
def path(self):
"""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.
"""
return self._path
@property
def name(self):
"""Return the name of this resource"""
return self.path.split("/")[-1]
@property
def real_path(self):
"""Return the file system path of this resource"""
return self.project._get_resource_path(self.path)
@property
def pathlib(self):
"""Return the file as a pathlib path."""
return Path(self.real_path)
def __eq__(self, obj):
return self.__class__ == obj.__class__ and self.path == obj.path
def __ne__(self, obj):
return not self.__eq__(obj)
def __hash__(self):
return hash(self.path)
def _perform_change(self, change_, description):
changes = change.ChangeSet(description)
changes.add_change(change_)
self.project.do(changes)
class File(Resource):
"""Represents a file"""
def __init__(self, project, name):
self.newlines = None
super().__init__(project, name)
def read(self):
data = self.read_bytes()
try:
content, self.newlines = fscommands.file_data_to_unicode(data)
return content
except UnicodeDecodeError as e:
raise exceptions.ModuleDecodeError(self.path, e.reason)
def read_bytes(self):
if not hasattr(self.project.fscommands, "read"):
warnings.warn(
"FileSystemCommands should implement read() method",
DeprecationWarning,
stacklevel=2,
)
with open(self.real_path, "rb") as handle:
return handle.read()
return self.project.fscommands.read(self.real_path)
def write(self, contents):
try:
if contents == self.read():
return
except OSError:
pass
self._perform_change(
change.ChangeContents(self, contents), "Writing file <%s>" % self.path
)
def is_folder(self):
return False
def create(self):
self.parent.create_file(self.name)
class Folder(Resource):
"""Represents a folder"""
def __init__(self, project, name):
super().__init__(project, name)
def is_folder(self):
return True
def get_children(self):
"""Return the children of this folder"""
try:
children = os.listdir(self.real_path)
except OSError:
return []
result = []
for name in children:
try:
child = self.get_child(name)
except exceptions.ResourceNotFoundError:
continue
if not self.project.is_ignored(child):
result.append(self.get_child(name))
return result
def create_file(self, file_name):
self._perform_change(
change.CreateFile(self, file_name),
"Creating file <%s>" % self._get_child_path(file_name),
)
return self.get_child(file_name)
def create_folder(self, folder_name):
self._perform_change(
change.CreateFolder(self, folder_name),
"Creating folder <%s>" % self._get_child_path(folder_name),
)
return self.get_child(folder_name)
def _get_child_path(self, name):
if self.path:
return self.path + "/" + name
else:
return name
def get_child(self, name):
return self.project.get_resource(self._get_child_path(name))
def has_child(self, name):
try:
self.get_child(name)
return True
except exceptions.ResourceNotFoundError:
return False
def get_files(self):
return [
resource for resource in self.get_children() if not resource.is_folder()
]
def get_folders(self):
return [resource for resource in self.get_children() if resource.is_folder()]
def contains(self, resource):
if self == resource:
return False
return self.path == "" or resource.path.startswith(self.path + "/")
def create(self):
self.parent.create_folder(self.name)
class _ResourceMatcher:
def __init__(self):
self.patterns = []
self._compiled_patterns = []
def set_patterns(self, patterns):
"""Specify which resources to match
`patterns` is a `list` of `str` that can contain ``*`` and
``?`` signs for matching resource names.
"""
self._compiled_patterns = None
self.patterns = patterns
def _add_pattern(self, pattern):
re_pattern = (
pattern.replace(".", "\\.")
.replace("*", "[^/]*")
.replace("?", "[^/]")
.replace("//", "/(.*/)?")
)
re_pattern = "^(.*/)?" + re_pattern + "(/.*)?$"
self.compiled_patterns.append(re.compile(re_pattern))
def does_match(self, resource):
for pattern in self.compiled_patterns:
if pattern.match(resource.path):
return True
path = os.path.join(resource.project.address, *resource.path.split("/"))
return os.path.islink(path)
@property
def compiled_patterns(self):
if self._compiled_patterns is None:
self._compiled_patterns = []
for pattern in self.patterns:
self._add_pattern(pattern)
return self._compiled_patterns