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
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# Copyright (C) 2009, 2010 Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
CONTAINER_EXTENSIONS = 'rar zip tar gz bz bz2 tgz 7z iso cab'.split()
DOCUMENT_EXTENSIONS = 'pdf doc ppt odt'.split()
DOCUMENT_BASENAMES = 'README TODO LICENSE COPYING INSTALL'.split()
DOCUMENT_EXTENSIONS = ()
DOCUMENT_BASENAMES = ()
import stat
import os
from time import time
from subprocess import Popen, PIPE
from os.path import abspath, basename, dirname, realpath
from . import T_FILE, T_DIRECTORY, T_UNKNOWN, T_NONEXISTANT, BAD_INFO
from ranger.shared import MimeTypeAware, FileManagerAware
from ranger.ext.shell_escape import shell_escape
from ranger.ext.human_readable import human_readable
class FileSystemObject(MimeTypeAware, FileManagerAware):
is_file = False
is_directory = False
content_loaded = False
force_load = False
path = None
basename = None
basename_lower = None
_shell_escaped_basename = None
_filetype = None
dirname = None
extension = None
exists = False
accessible = False
marked = False
tagged = False
loaded = False
runnable = False
is_link = False
is_device = False
is_socket = False
is_fifo = False
readlink = None
stat = None
infostring = None
permissions = None
type = T_UNKNOWN
size = 0
last_used = None
stopped = False
video = False
image = False
audio = False
media = False
document = False
container = False
mimetype_tuple = ()
def __init__(self, path):
MimeTypeAware.__init__(self)
path = abspath(path)
self.path = path
self.basename = basename(path)
self.basename_lower = self.basename.lower()
self.dirname = dirname(path)
self.realpath = self.path
try:
lastdot = self.basename.rindex('.') + 1
self.extension = self.basename[lastdot:].lower()
except ValueError:
self.extension = None
self.set_mimetype()
self.use()
def __repr__(self):
return "<{0} {1}>".format(self.__class__.__name__, self.path)
@property
def shell_escaped_basename(self):
if self._shell_escaped_basename is None:
self._shell_escaped_basename = shell_escape(self.basename)
return self._shell_escaped_basename
@property
def filetype(self):
if self._filetype is None:
try:
got = Popen(["file", '-Lb', '--mime-type', self.path],
stdout=PIPE).communicate()[0]
except OSError:
self._filetype = ''
else:
self._filetype = got
return self._filetype
def get_description(self):
return "Loading " + str(self)
def __str__(self):
"""returns a string containing the absolute path"""
return str(self.path)
def use(self):
"""mark the filesystem-object as used at the current time"""
self.last_used = time()
def is_older_than(self, seconds):
"""returns whether this object wasn't use()d in the last n seconds"""
if seconds < 0:
return True
return self.last_used + seconds < time()
def set_mimetype(self):
"""assign attributes such as self.video according to the mimetype"""
self.mimetype = self.mimetypes.guess_type(self.basename, False)[0]
if self.mimetype is None:
self.mimetype = ''
self.video = self.mimetype.startswith('video')
self.image = self.mimetype.startswith('image')
self.audio = self.mimetype.startswith('audio')
self.media = self.video or self.image or self.audio
self.document = self.mimetype.startswith('text') \
or (self.extension in DOCUMENT_EXTENSIONS) \
or (self.basename in DOCUMENT_BASENAMES)
self.container = self.extension in CONTAINER_EXTENSIONS
keys = ('video', 'audio', 'image', 'media', 'document', 'container')
self.mimetype_tuple = tuple(key for key in keys if getattr(self, key))
if self.mimetype == '':
self.mimetype = None
def mark(self, boolean):
directory = self.env.get_directory(self.dirname)
directory.mark_item(self)
def _mark(self, boolean):
"""Called by directory.mark_item() and similar functions"""
self.marked = bool(boolean)
def determine_infostring(self):
self.size = 0
if self.is_device:
self.infostring = 'dev'
elif self.is_fifo:
self.infostring = 'fifo'
elif self.is_socket:
self.infostring = 'sock'
elif self.is_directory:
try:
self.size = len(os.listdir(self.path))
self.infostring = " %d" % self.size
self.accessible = True
self.runnable = True
except OSError:
self.infostring = BAD_INFO
self.accessible = False
elif self.is_file:
self.size = self.stat.st_size
self.infostring = ' ' + human_readable(self.size)
if self.is_link:
self.infostring = '->'
def load(self):
"""
reads useful information about the filesystem-object from the
filesystem and caches it for later use
"""
self.loaded = True
try:
self.stat = os.lstat(self.path)
except OSError:
self.stat = None
self.is_link = False
self.accessible = False
else:
self.is_link = stat.S_ISLNK(self.stat.st_mode)
if self.is_link:
try: # try to resolve the link
self.readlink = os.readlink(self.path)
self.realpath = realpath(self.path)
self.stat = os.stat(self.path)
except: # it failed, so it must be a broken link
pass
mode = self.stat.st_mode
self.is_device = bool(stat.S_ISCHR(mode) or stat.S_ISBLK(mode))
self.is_socket = bool(stat.S_ISSOCK(mode))
self.is_fifo = bool(stat.S_ISFIFO(mode))
self.accessible = True
if self.accessible and os.access(self.path, os.F_OK):
self.exists = True
self.accessible = True
if os.path.isdir(self.path):
self.type = T_DIRECTORY
self.runnable = bool(mode & stat.S_IXUSR)
elif os.path.isfile(self.path):
self.type = T_FILE
else:
self.type = T_UNKNOWN
else:
self.type = T_NONEXISTANT
self.exists = False
self.runnable = False
self.determine_infostring()
def get_permission_string(self):
if self.permissions is not None:
return self.permissions
try:
mode = self.stat.st_mode
except:
return '----??----'
if stat.S_ISDIR(mode):
perms = ['d']
elif stat.S_ISLNK(mode):
perms = ['l']
else:
perms = ['-']
for who in ("USR", "GRP", "OTH"):
for what in "RWX":
if mode & getattr(stat, "S_I" + what + who):
perms.append(what.lower())
else:
perms.append('-')
self.permissions = ''.join(perms)
return self.permissions
def load_once(self):
"""calls load() if it has not been called at least once yet"""
if not self.loaded:
self.load()
return True
return False
def go(self):
"""enter the directory if the filemanager is running"""
if self.fm:
return self.fm.enter_dir(self.path)
return False
def load_if_outdated(self):
"""
Calls load() if the currently cached information is outdated
or nonexistant.
"""
if self.load_once():
return True
try:
real_mtime = os.lstat(self.path).st_mtime
except OSError:
real_mtime = None
if self.stat:
cached_mtime = self.stat.st_mtime
else:
cached_mtime = 0
if real_mtime != cached_mtime:
self.load()
return True
return False
|