-
Notifications
You must be signed in to change notification settings - Fork 1
/
filemanager.py
312 lines (286 loc) · 9.99 KB
/
filemanager.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from curses import KEY_LEFT, KEY_RIGHT
import re
import os
import commands
from interface import *
from manipulatefiles import *
import config
class FileManager(ManipulateDirContents):
def __init__(self, win, dir=''):
if dir:
if dir[0] != '/':
dir = os.getcwd() + os.sep + dir
if not os.path.isdir(dir):
dir = os.getcwd()
else:
dir = os.getcwd()
ManipulateDirContents.__init__(self, dir)
self.win = win
self.view = ViewDirContents(win)
keys = {
KEY_LEFT: self.back,
KEY_RIGHT: self.next,
ord(';'): self.open,
ord(':'): self.execute,
ord('o'): self.setOption,
ord('/'): self.setRegex,
ord('h'): self.home,
ord('g'): self.goto,
ord('m'): self.move,
ord('d'): self.delete,
ord('c'): self.copy,
ord('x'): self.selectGoDown,
ord('X'): self.selectGoUp,
ord('a'): self.selectAllItems,
ord('n'): self.createDir,
ord('r'): self.refresh,
ord('?'): self.help
}
self.view.addKey(keys)
self.programs = config.commands
self.refresh()
def showContents(self, index=0):
j = 0; attr = {}; text = [];
for info, name in self.getContents():
text.append([])
f = self.contents[j]
if f.isBinary():
type = 'bin'
else:
type = f.type()
if j in self.selected:
text[j].append(self.view.select_char)
attr[(j, 0)] = self.view.select_attr
else:
text[j].append(' ')
if 'l' in self.option:
i = 2
text[j].extend([info, name])
attr[(j, i)] = self.view.file_color[type]
else:
i = 1
text[j].append(name)
attr[(j, i)] = self.view.file_color[type]
j += 1
self.view.setInfoSecondLine(self.cwd+os.sep,self.option,self.regex)
self.view.setText(text, attr)
self.view.write(index)
def refresh(self):
self.setContents()
self.showContents(self.view.y)
def back(self):
if self.cwd:
actualdir = self.cwd
try:
self.cwd = os.path.dirname(self.cwd).rstrip(os.sep)
self.selected = []
self.setContents()
name = os.path.basename(actualdir)
if name in self.file_names:
i = self.file_names.index(os.path.basename(actualdir))
if i > self.view.max_y-1:
self.view.Y = (self.view.max_y-1)/2
else:
self.view.Y = i
else:
i = 0
self.view.Y = 0
self.showContents(i)
except OSError, error:
self.cwd = actualdir
self.view.message(str(error),
config.attribute('error_message'))
def next(self):
if self.contents:
item_under_cursor = self.contents[self.view.y]
actualdir = self.cwd
if item_under_cursor.type() == 'd':
try:
self.cwd = item_under_cursor.dir.rstrip(os.sep)+\
os.sep+item_under_cursor.name
self.selected = []
self.setContents()
self.showContents()
except OSError, error:
self.cwd = actualdir
self.view.message(str(error),
config.attribute('error_message'))
def home(self):
userhome = os.path.expanduser('~').rstrip(os.sep)
if self.cwd != userhome:
self.cwd = userhome
self.selected = []
self.setContents()
self.showContents()
def goto(self):
gotodir = self.view.input('Go to: ')
if not gotodir:
self.refresh()
return None
if gotodir[0] != os.sep:
gotodir = self.cwd + os.sep + gotodir.rstrip(os.sep)
if os.access(gotodir, os.F_OK):
actualdir = self.cwd
try:
self.cwd = gotodir.rstrip(os.sep)
self.selected = []
self.setContents()
self.showContents()
except OSError, error:
self.cwd = actualdir
self.view.message(str(error),
config.attribute('error_message'))
else:
self.view.message("Not a directory: '%s'" % gotodir,
config.attribute('error_message'))
def loop(self):
self.view.loop()
def setOption(self):
option = self.view.input('Option [latr]: ')
if re.match('[latr]+', option) or option == '':
self.option = option
else:
self.option = 'l'
self.setContents()
self.showContents(self.view.y)
def setRegex(self):
regex = self.view.input('regex: ')
if regex:
self.regex = regex
self.pattern = re.compile(r'%s'%regex)
self.setContents()
if self.contents:
self.showContents()
else:
self.regex = ''
self.setContents()
self.showContents(self.view.y)
self.view.message("Pattern not found: '%s'" % regex)
else:
self.regex = ''
self.refresh()
def selectGoDown(self):
if self.contents:
self.select(self.view.y)
if self.view.y in self.selected:
self.view.selectLine()
else:
self.view.unselectLine()
self.view.move_down()
def selectGoUp(self):
if self.contents:
self.select(self.view.y)
if self.view.y in self.selected:
self.view.selectLine()
else:
self.view.unselectLine()
self.view.move_up()
def selectAllItems(self):
if self.contents:
self.selectAll()
self.showContents(self.view.y)
def delete(self):
if self.contents:
if not self.selected:
self.select(self.view.y)
input = self.view.input('Remove [y/n]? ')
if input.lower() == 'y':
try:
self.deleteSelected()
self.refresh()
except Exception, e:
self.refresh()
self.view.message(str(e),
config.attribute('error_message'))
else:
self.refresh()
def copy(self):
if self.contents:
if not self.selected:
self.select(self.view.y)
input = self.view.input('Copy name: ')
if input:
try:
self.copySelected(input)
self.refresh()
except Exception, e:
self.refresh()
self.view.message(str(e),
config.attribute('error_message'))
else:
self.refresh()
def move(self):
if self.contents:
if not self.selected:
self.select(self.view.y)
input = self.view.input('Move to: ')
if input:
try:
self.moveSelected(input)
self.refresh()
except Exception, e:
self.refresh()
self.view.message(str(e),
config.attribute('error_message'))
else:
self.refresh()
def createDir(self):
dirname = self.view.input('New directory: ')
if dirname:
try:
self.makeDir(dirname)
self.refresh()
except Exception, e:
self.refresh()
self.view.message(str(e),
config.attribute('error_message'))
else:
self.refresh()
def open(self):
if self.contents:
if not self.selected:
self.selected.append(self.view.y)
for i in self.selected[:]:
item = self.contents[i]
ext = item.extension
if item.type() == '-' and ext in self.programs:
fname = item.dir+os.sep+item.name
cmd = '%s "%s" &>/dev/null &'%(self.programs[ext], fname)
os.system(cmd)
self.selected.remove(i)
self.refresh()
def execute(self):
current_dir = os.getcwd()
os.chdir(self.cwd+os.sep)
cmd = self.view.input('Command: ')
if cmd[0] == '!':
cmd = cmd[1:]
if cmd:
try:
os.system(cmd)
self.refresh()
except OsError, error:
self.view.message(str(error),
config.attribute('error_message'))
else:
self.refresh()
elif cmd and self.contents:
if not self.selected:
self.selected.append(self.view.y)
for i in self.selected[:]:
item = self.contents[i]
name = item.dir+os.sep+item.name
execute = '%s "%s"' % (cmd, name)
stat, out = commands.getstatusoutput(execute)
self.selected.remove(i)
self.refresh()
else:
self.refresh()
os.chdir(current_dir)
def help(self):
show_help_screen(self.win)
self.win.clear()
self.setContents()
self.showContents(self.view.y)