Mercurial > urwid-satext
annotate urwid_satext/files_management.py @ 145:d505d9ee792e v0.8.0b1
v0.8.0b1 release
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 18 Jun 2021 13:19:44 +0200 |
parents | 144bdf877d21 |
children | 6689aa54b20c |
rev | line source |
---|---|
21 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
64 | 4 # Urwid SàT extensions |
122 | 5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
64 | 6 # |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Lesser General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 # | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Lesser General Public License for more details. | |
16 # | |
17 # You should have received a copy of the GNU Lesser General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | 19 |
20 import urwid | |
143 | 21 from . import sat_widgets |
21 | 22 import os, os.path |
23 from xml.dom import minidom | |
123
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
24 import logging as log |
23 | 25 from time import time |
92
fdd0543677d4
use of new keys module in files_management
Goffi <goffi@goffi.org>
parents:
71
diff
changeset
|
26 from .keys import action_key_map as a_key |
34
875ff127b2dd
- i18n management: gettext integration + french translation
Goffi <goffi@goffi.org>
parents:
32
diff
changeset
|
27 |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
28 class PathEdit(sat_widgets.AdvancedEdit): |
23 | 29 """AdvancedEdit with manage file paths""" |
66 | 30 |
21 | 31 def keypress(self, size, key): |
143 | 32 if key == '~' and self.edit_pos==0: |
33 expanded = os.path.expanduser('~') | |
34 self.set_edit_text(os.path.normpath(expanded+'/'+self.edit_text)) | |
23 | 35 self.set_edit_pos(len(expanded)+1) |
92
fdd0543677d4
use of new keys module in files_management
Goffi <goffi@goffi.org>
parents:
71
diff
changeset
|
36 elif key == a_key['EDIT_DELETE_LAST_WORD']: |
21 | 37 if self.edit_pos<2: |
38 return | |
39 before = self.edit_text[:self.edit_pos] | |
40 pos = (before[:-1] if before.endswith('/') else before).rfind("/")+1 | |
41 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:]) | |
42 self.set_edit_pos(pos) | |
43 return | |
44 else: | |
66 | 45 return super(PathEdit, self).keypress(size, key) |
21 | 46 |
23 | 47 class FilesViewer(urwid.WidgetWrap): |
48 """List specialised for files""" | |
49 | |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
50 def __init__(self, onPreviousDir, onDirClick, onFileClick = None): |
23 | 51 self.path='' |
52 self.key_cache = '' | |
53 self.key_time = time() | |
54 self.onPreviousDir = onPreviousDir | |
55 self.onDirClick = onDirClick | |
56 self.onFileClick = onFileClick | |
57 self.files_list = urwid.SimpleListWalker([]) | |
66 | 58 self.show_hidden = True |
23 | 59 listbox = urwid.ListBox(self.files_list) |
60 urwid.WidgetWrap.__init__(self, listbox) | |
61 | |
62 def keypress(self, size, key): | |
92
fdd0543677d4
use of new keys module in files_management
Goffi <goffi@goffi.org>
parents:
71
diff
changeset
|
63 if key==a_key['FILES_HIDDEN_HIDE']: |
23 | 64 #(un)hide hidden files |
65 self.show_hidden = not self.show_hidden | |
66 self.showDirectory(self.path) | |
92
fdd0543677d4
use of new keys module in files_management
Goffi <goffi@goffi.org>
parents:
71
diff
changeset
|
67 elif key==a_key['FILES_JUMP_DIRECTORIES']: |
23 | 68 #jump to directories |
69 if self.files_list: | |
70 self._w.set_focus(0) | |
92
fdd0543677d4
use of new keys module in files_management
Goffi <goffi@goffi.org>
parents:
71
diff
changeset
|
71 elif key==a_key['FILES_JUMP_FILES']: |
23 | 72 for idx in range(len(self.files_list)): |
73 if isinstance(self.files_list[idx].base_widget,urwid.Divider): | |
74 if idx<len(self.files_list)-1: | |
75 self._w.set_focus(idx+1) | |
76 break | |
77 elif len(key) == 1: | |
78 if time() - self.key_time > 2: | |
79 self.key_cache=key | |
80 else: | |
81 self.key_cache+=key | |
82 self.key_time = time() | |
83 for idx in range(len(self.files_list)): | |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
84 if isinstance(self.files_list[idx],sat_widgets.ClickableText) and self.files_list[idx].get_text().lower().startswith(self.key_cache.lower()): |
23 | 85 self._w.set_focus(idx) |
86 break | |
87 else: | |
88 return self._w.keypress(size, key) | |
89 | |
90 def showDirectory(self, path): | |
91 self.path = path | |
92 del self.files_list[:] | |
93 directories = [] | |
94 files = [] | |
95 try: | |
96 for filename in os.listdir(path): | |
143 | 97 if not isinstance(filename, str): |
98 log.warning("file [{}] has a badly encode filename, ignoring it".format(filename.decode('utf-8', 'replace'))) | |
123
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
99 continue |
23 | 100 fullpath = os.path.join(path,filename) |
101 if os.path.isdir(fullpath): | |
102 directories.append(filename) | |
103 else: | |
104 files.append(filename) | |
105 except OSError: | |
66 | 106 self.files_list.append(urwid.Text(("warning",_("Impossible to list directory")),'center')) |
23 | 107 directories.sort() |
108 files.sort() | |
143 | 109 if os.path.abspath(path)!='/' and os.path.abspath(path) != '//': |
110 previous_wid = sat_widgets.ClickableText(('directory','..')) | |
23 | 111 urwid.connect_signal(previous_wid,'click',self.onPreviousDir) |
112 self.files_list.append(previous_wid) | |
113 for directory in directories: | |
114 if directory.startswith('.') and not self.show_hidden: | |
115 continue | |
143 | 116 dir_wid = sat_widgets.ClickableText(('directory',directory)) |
23 | 117 urwid.connect_signal(dir_wid,'click',self.onDirClick) |
118 self.files_list.append(dir_wid) | |
143 | 119 self.files_list.append(urwid.AttrMap(urwid.Divider('-'),'separator')) |
23 | 120 for filename in files: |
143 | 121 if filename.startswith('.') and not self.show_hidden: |
23 | 122 continue |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
123 file_wid = sat_widgets.ClickableText(filename) |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
124 if self.onFileClick: |
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
125 urwid.connect_signal(file_wid,'click',self.onFileClick) |
23 | 126 self.files_list.append(file_wid) |
127 | |
128 | |
21 | 129 class FileDialog(urwid.WidgetWrap): |
130 | |
112
b3e8edbe0a1e
FileDialog: a message can now be displayed above the file selector
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
131 def __init__(self, ok_cb, cancel_cb, message=None, title=_("Please select a file"), style=[]): |
23 | 132 """Create file dialog |
112
b3e8edbe0a1e
FileDialog: a message can now be displayed above the file selector
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
133 |
23 | 134 @param title: title of the window/popup |
112
b3e8edbe0a1e
FileDialog: a message can now be displayed above the file selector
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
135 @param message: message to display, or None to only show title and file dialog |
b3e8edbe0a1e
FileDialog: a message can now be displayed above the file selector
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
136 message will be passed to a Text widget, so markup can be used |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
137 @param style: list of string: |
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
138 - 'dir' if a dir path must be selected |
23 | 139 """ |
140 self.ok_cb = ok_cb | |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
141 self._type = 'dir' if 'dir' in style else 'normal' |
143 | 142 self.__home_path = os.path.expanduser('~') |
112
b3e8edbe0a1e
FileDialog: a message can now be displayed above the file selector
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
143 widgets = [] |
b3e8edbe0a1e
FileDialog: a message can now be displayed above the file selector
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
144 if message: |
b3e8edbe0a1e
FileDialog: a message can now be displayed above the file selector
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
145 widgets.append(urwid.Text(message)) |
143 | 146 widgets.append(urwid.Divider('─')) |
147 self.path_wid = PathEdit(_('Path: ')) | |
23 | 148 self.path_wid.setCompletionMethod(self._directory_completion) |
21 | 149 urwid.connect_signal(self.path_wid, 'change', self.onPathChange) |
112
b3e8edbe0a1e
FileDialog: a message can now be displayed above the file selector
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
150 widgets.append(self.path_wid) |
143 | 151 widgets.append(urwid.Divider('─')) |
112
b3e8edbe0a1e
FileDialog: a message can now be displayed above the file selector
Goffi <goffi@goffi.org>
parents:
108
diff
changeset
|
152 header = urwid.Pile(widgets) |
21 | 153 bookm_list = urwid.SimpleListWalker([]) |
154 self.bookmarks = list(self.getBookmarks()) | |
155 self.bookmarks.sort() | |
156 for bookmark in self.bookmarks: | |
157 if bookmark.startswith(self.__home_path): | |
143 | 158 bookmark="~"+bookmark[len(self.__home_path):] |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
159 book_wid = sat_widgets.ClickableText(bookmark) |
21 | 160 urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected) |
161 bookm_list.append(book_wid) | |
143 | 162 bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_('Bookmarks'),'center'),'title')) |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
163 self.files_wid = FilesViewer(self.onPreviousDir, self.onDirClick, self.onFileClick if self._type == 'normal' else None) |
21 | 164 center_row = urwid.Columns([('weight',2,bookm_wid), |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
165 ('weight',8,sat_widgets.VerticalSeparator(self.files_wid))]) |
23 | 166 |
167 buttons = [] | |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
168 if self._type == 'dir': |
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
169 buttons.append(sat_widgets.CustomButton(_('Ok'), self._validateDir)) |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
170 buttons.append(sat_widgets.CustomButton(_('Cancel'),cancel_cb)) |
23 | 171 max_len = max([button.getSize() for button in buttons]) |
172 buttons_wid = urwid.GridFlow(buttons,max_len,1,0,'center') | |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
173 main_frame = sat_widgets.FocusFrame(center_row, header, buttons_wid) |
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
174 decorated = sat_widgets.LabelLine(main_frame, sat_widgets.SurroundedText(title)) |
21 | 175 urwid.WidgetWrap.__init__(self, decorated) |
143 | 176 self.path_wid.set_edit_text(os.getcwd()) |
23 | 177 |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
178 def _validateDir(self, wid): |
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
179 """ call ok callback if current path is a dir """ |
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
180 path = os.path.abspath(self.path_wid.get_edit_text()) |
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
181 if os.path.isdir(path): |
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
182 self.ok_cb(path) |
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
183 |
23 | 184 def _directory_completion(self, path, completion_data): |
143 | 185 assert isinstance(path, str) |
23 | 186 path=os.path.abspath(path) |
187 if not os.path.isdir(path): | |
188 head,dir_start = os.path.split(path) | |
189 else: | |
190 head=path | |
143 | 191 dir_start='' |
23 | 192 try: |
193 filenames = os.listdir(head) | |
123
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
194 to_remove = [] |
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
195 |
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
196 # we remove badly encoded files |
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
197 for filename in filenames: |
143 | 198 if not isinstance(filename, str): |
199 log.warning("file [{}] has a badly encode filename, ignoring it".format(filename.decode('utf-8', 'replace'))) | |
123
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
200 to_remove.append(filename) |
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
201 for filename in to_remove: |
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
202 filenames.remove(filename) |
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
203 |
23 | 204 filenames.sort() |
205 try: | |
206 start_idx=filenames.index(completion_data['last_dir'])+1 | |
207 if start_idx == len(filenames): | |
208 start_idx = 0 | |
209 except (KeyError,ValueError): | |
210 start_idx = 0 | |
143 | 211 for idx in list(range(start_idx,len(filenames))) + list(range(0,start_idx)): |
23 | 212 full_path = os.path.join(head,filenames[idx]) |
213 if filenames[idx].lower().startswith(dir_start.lower()) and os.path.isdir(full_path): | |
214 completion_data['last_dir'] = filenames[idx] | |
66 | 215 return full_path |
23 | 216 except OSError: |
217 pass | |
218 return path | |
21 | 219 |
220 def getBookmarks(self): | |
143 | 221 gtk_bookm = os.path.expanduser("~/.gtk-bookmarks") |
222 kde_bookm = os.path.expanduser("~/.kde/share/apps/kfileplaces/bookmarks.xml") | |
21 | 223 bookmarks = set() |
224 try: | |
23 | 225 with open(gtk_bookm) as gtk_fd: |
226 for bm in gtk_fd.readlines(): | |
21 | 227 if bm.startswith("file:///"): |
123
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
228 bookmarks.add(bm[7:].replace('\n','').decode('utf-8', 'replace')) |
21 | 229 except IOError: |
143 | 230 log.info(_('No GTK bookmarks file found')) |
21 | 231 pass |
66 | 232 |
21 | 233 try: |
234 dom = minidom.parse(kde_bookm) | |
23 | 235 for elem in dom.getElementsByTagName('bookmark'): |
21 | 236 bm = elem.getAttribute("href") |
237 if bm.startswith("file:///"): | |
123
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
238 bookmarks.add(bm[7:].decode('utf-8', 'replace')) |
21 | 239 except IOError: |
123
f2589475269f
file_management: unicode was badly handled. As a quick solution, we handle everything as unicode, and ignore badly encoded filenames, this may change in the future
Goffi <goffi@goffi.org>
parents:
122
diff
changeset
|
240 log.info(_('No KDE bookmarks file found')) |
21 | 241 pass |
66 | 242 |
21 | 243 return bookmarks |
244 | |
245 def onBookmarkSelected(self, button): | |
246 self.path_wid.set_edit_text(os.path.expanduser(button.get_text())) | |
247 | |
23 | 248 def onPathChange(self, edit, path): |
249 if os.path.isdir(path): | |
250 self.files_wid.showDirectory(path) | |
251 | |
252 def onPreviousDir(self, wid): | |
253 path = os.path.abspath(self.path_wid.get_edit_text()) | |
254 if not os.path.isdir(path): | |
93
900014ae36b8
fixed unicode issue invalid method call in files_management
Goffi <goffi@goffi.org>
parents:
92
diff
changeset
|
255 path = os.path.dirname(path) |
23 | 256 self.path_wid.set_edit_text(os.path.split(path)[0]) |
257 | |
258 def onDirClick(self, wid): | |
259 path = os.path.abspath(self.path_wid.get_edit_text()) | |
260 if not os.path.isdir(path): | |
93
900014ae36b8
fixed unicode issue invalid method call in files_management
Goffi <goffi@goffi.org>
parents:
92
diff
changeset
|
261 path = os.path.dirname(path) |
23 | 262 self.path_wid.set_edit_text(os.path.join(path,wid.get_text())) |
66 | 263 |
23 | 264 def onFileClick(self, wid): |
265 self.ok_cb(os.path.abspath(os.path.join(self.files_wid.path,wid.get_text()))) |