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/resources.py
1415 views
1
import os
2
import re
3
4
import rope.base.change
5
import rope.base.fscommands
6
from rope.base import exceptions
7
8
9
class Resource(object):
10
"""Represents files and folders in a project"""
11
12
def __init__(self, project, path):
13
self.project = project
14
self._path = path
15
16
def move(self, new_location):
17
"""Move resource to `new_location`"""
18
self._perform_change(rope.base.change.MoveResource(self, new_location),
19
'Moving <%s> to <%s>' % (self.path, new_location))
20
21
def remove(self):
22
"""Remove resource from the project"""
23
self._perform_change(rope.base.change.RemoveResource(self),
24
'Removing <%s>' % self.path)
25
26
def is_folder(self):
27
"""Return true if the resource is a folder"""
28
29
def create(self):
30
"""Create this resource"""
31
32
def exists(self):
33
return os.path.exists(self.real_path)
34
35
@property
36
def parent(self):
37
parent = '/'.join(self.path.split('/')[0:-1])
38
return self.project.get_folder(parent)
39
40
@property
41
def path(self):
42
"""Return the path of this resource relative to the project root
43
44
The path is the list of parent directories separated by '/' followed
45
by the resource name.
46
"""
47
return self._path
48
49
@property
50
def name(self):
51
"""Return the name of this resource"""
52
return self.path.split('/')[-1]
53
54
@property
55
def real_path(self):
56
"""Return the file system path of this resource"""
57
return self.project._get_resource_path(self.path)
58
59
def __eq__(self, obj):
60
return self.__class__ == obj.__class__ and self.path == obj.path
61
62
def __ne__(self, obj):
63
return not self.__eq__(obj)
64
65
def __hash__(self):
66
return hash(self.path)
67
68
def _perform_change(self, change_, description):
69
changes = rope.base.change.ChangeSet(description)
70
changes.add_change(change_)
71
self.project.do(changes)
72
73
74
class File(Resource):
75
"""Represents a file"""
76
77
def __init__(self, project, name):
78
super(File, self).__init__(project, name)
79
80
def read(self):
81
data = self.read_bytes()
82
try:
83
return rope.base.fscommands.file_data_to_unicode(data)
84
except UnicodeDecodeError, e:
85
raise exceptions.ModuleDecodeError(self.path, e.reason)
86
87
def read_bytes(self):
88
return open(self.real_path, 'rb').read()
89
90
def write(self, contents):
91
try:
92
if contents == self.read():
93
return
94
except IOError:
95
pass
96
self._perform_change(rope.base.change.ChangeContents(self, contents),
97
'Writing file <%s>' % self.path)
98
99
def is_folder(self):
100
return False
101
102
def create(self):
103
self.parent.create_file(self.name)
104
105
106
class Folder(Resource):
107
"""Represents a folder"""
108
109
def __init__(self, project, name):
110
super(Folder, self).__init__(project, name)
111
112
def is_folder(self):
113
return True
114
115
def get_children(self):
116
"""Return the children of this folder"""
117
result = []
118
for name in os.listdir(self.real_path):
119
try:
120
child = self.get_child(name)
121
except exceptions.ResourceNotFoundError:
122
continue
123
if not self.project.is_ignored(child):
124
result.append(self.get_child(name))
125
return result
126
127
def create_file(self, file_name):
128
self._perform_change(
129
rope.base.change.CreateFile(self, file_name),
130
'Creating file <%s>' % self._get_child_path(file_name))
131
return self.get_child(file_name)
132
133
def create_folder(self, folder_name):
134
self._perform_change(
135
rope.base.change.CreateFolder(self, folder_name),
136
'Creating folder <%s>' % self._get_child_path(folder_name))
137
return self.get_child(folder_name)
138
139
def _get_child_path(self, name):
140
if self.path:
141
return self.path + '/' + name
142
else:
143
return name
144
145
def get_child(self, name):
146
return self.project.get_resource(self._get_child_path(name))
147
148
def has_child(self, name):
149
try:
150
self.get_child(name)
151
return True
152
except exceptions.ResourceNotFoundError:
153
return False
154
155
def get_files(self):
156
return [resource for resource in self.get_children()
157
if not resource.is_folder()]
158
159
def get_folders(self):
160
return [resource for resource in self.get_children()
161
if resource.is_folder()]
162
163
def contains(self, resource):
164
if self == resource:
165
return False
166
return self.path == '' or resource.path.startswith(self.path + '/')
167
168
def create(self):
169
self.parent.create_folder(self.name)
170
171
172
class _ResourceMatcher(object):
173
174
def __init__(self):
175
self.patterns = []
176
self._compiled_patterns = []
177
178
def set_patterns(self, patterns):
179
"""Specify which resources to match
180
181
`patterns` is a `list` of `str`\s that can contain ``*`` and
182
``?`` signs for matching resource names.
183
184
"""
185
self._compiled_patterns = None
186
self.patterns = patterns
187
188
def _add_pattern(self, pattern):
189
re_pattern = pattern.replace('.', '\\.').\
190
replace('*', '[^/]*').replace('?', '[^/]').\
191
replace('//', '/(.*/)?')
192
re_pattern = '^(.*/)?' + re_pattern + '(/.*)?$'
193
self.compiled_patterns.append(re.compile(re_pattern))
194
195
def does_match(self, resource):
196
for pattern in self.compiled_patterns:
197
if pattern.match(resource.path):
198
return True
199
path = os.path.join(resource.project.address,
200
*resource.path.split('/'))
201
if os.path.islink(path):
202
return True
203
return False
204
205
@property
206
def compiled_patterns(self):
207
if self._compiled_patterns is None:
208
self._compiled_patterns = []
209
for pattern in self.patterns:
210
self._add_pattern(pattern)
211
return self._compiled_patterns
212
213