Mercurial > urwid-satext
comparison urwid_satext/files_management.py @ 143:144bdf877d21
python 3 port (using 2to3)
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 08:55:41 +0200 |
parents | 1970df98643d |
children | 6689aa54b20c |
comparison
equal
deleted
inserted
replaced
142:2855123621a0 | 143:144bdf877d21 |
---|---|
16 # | 16 # |
17 # You should have received a copy of the GNU Lesser General Public License | 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/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 import urwid | 20 import urwid |
21 import sat_widgets | 21 from . import sat_widgets |
22 import os, os.path | 22 import os, os.path |
23 from xml.dom import minidom | 23 from xml.dom import minidom |
24 import logging as log | 24 import logging as log |
25 from time import time | 25 from time import time |
26 from .keys import action_key_map as a_key | 26 from .keys import action_key_map as a_key |
27 | 27 |
28 class PathEdit(sat_widgets.AdvancedEdit): | 28 class PathEdit(sat_widgets.AdvancedEdit): |
29 """AdvancedEdit with manage file paths""" | 29 """AdvancedEdit with manage file paths""" |
30 | 30 |
31 def keypress(self, size, key): | 31 def keypress(self, size, key): |
32 if key == u'~' and self.edit_pos==0: | 32 if key == '~' and self.edit_pos==0: |
33 expanded = os.path.expanduser(u'~') | 33 expanded = os.path.expanduser('~') |
34 self.set_edit_text(os.path.normpath(expanded+u'/'+self.edit_text)) | 34 self.set_edit_text(os.path.normpath(expanded+'/'+self.edit_text)) |
35 self.set_edit_pos(len(expanded)+1) | 35 self.set_edit_pos(len(expanded)+1) |
36 elif key == a_key['EDIT_DELETE_LAST_WORD']: | 36 elif key == a_key['EDIT_DELETE_LAST_WORD']: |
37 if self.edit_pos<2: | 37 if self.edit_pos<2: |
38 return | 38 return |
39 before = self.edit_text[:self.edit_pos] | 39 before = self.edit_text[:self.edit_pos] |
92 del self.files_list[:] | 92 del self.files_list[:] |
93 directories = [] | 93 directories = [] |
94 files = [] | 94 files = [] |
95 try: | 95 try: |
96 for filename in os.listdir(path): | 96 for filename in os.listdir(path): |
97 if not isinstance(filename, unicode): | 97 if not isinstance(filename, str): |
98 log.warning(u"file [{}] has a badly encode filename, ignoring it".format(filename.decode('utf-8', 'replace'))) | 98 log.warning("file [{}] has a badly encode filename, ignoring it".format(filename.decode('utf-8', 'replace'))) |
99 continue | 99 continue |
100 fullpath = os.path.join(path,filename) | 100 fullpath = os.path.join(path,filename) |
101 if os.path.isdir(fullpath): | 101 if os.path.isdir(fullpath): |
102 directories.append(filename) | 102 directories.append(filename) |
103 else: | 103 else: |
104 files.append(filename) | 104 files.append(filename) |
105 except OSError: | 105 except OSError: |
106 self.files_list.append(urwid.Text(("warning",_("Impossible to list directory")),'center')) | 106 self.files_list.append(urwid.Text(("warning",_("Impossible to list directory")),'center')) |
107 directories.sort() | 107 directories.sort() |
108 files.sort() | 108 files.sort() |
109 if os.path.abspath(path)!=u'/' and os.path.abspath(path) != u'//': | 109 if os.path.abspath(path)!='/' and os.path.abspath(path) != '//': |
110 previous_wid = sat_widgets.ClickableText((u'directory',u'..')) | 110 previous_wid = sat_widgets.ClickableText(('directory','..')) |
111 urwid.connect_signal(previous_wid,'click',self.onPreviousDir) | 111 urwid.connect_signal(previous_wid,'click',self.onPreviousDir) |
112 self.files_list.append(previous_wid) | 112 self.files_list.append(previous_wid) |
113 for directory in directories: | 113 for directory in directories: |
114 if directory.startswith('.') and not self.show_hidden: | 114 if directory.startswith('.') and not self.show_hidden: |
115 continue | 115 continue |
116 dir_wid = sat_widgets.ClickableText((u'directory',directory)) | 116 dir_wid = sat_widgets.ClickableText(('directory',directory)) |
117 urwid.connect_signal(dir_wid,'click',self.onDirClick) | 117 urwid.connect_signal(dir_wid,'click',self.onDirClick) |
118 self.files_list.append(dir_wid) | 118 self.files_list.append(dir_wid) |
119 self.files_list.append(urwid.AttrMap(urwid.Divider(u'-'),'separator')) | 119 self.files_list.append(urwid.AttrMap(urwid.Divider('-'),'separator')) |
120 for filename in files: | 120 for filename in files: |
121 if filename.startswith(u'.') and not self.show_hidden: | 121 if filename.startswith('.') and not self.show_hidden: |
122 continue | 122 continue |
123 file_wid = sat_widgets.ClickableText(filename) | 123 file_wid = sat_widgets.ClickableText(filename) |
124 if self.onFileClick: | 124 if self.onFileClick: |
125 urwid.connect_signal(file_wid,'click',self.onFileClick) | 125 urwid.connect_signal(file_wid,'click',self.onFileClick) |
126 self.files_list.append(file_wid) | 126 self.files_list.append(file_wid) |
137 @param style: list of string: | 137 @param style: list of string: |
138 - 'dir' if a dir path must be selected | 138 - 'dir' if a dir path must be selected |
139 """ | 139 """ |
140 self.ok_cb = ok_cb | 140 self.ok_cb = ok_cb |
141 self._type = 'dir' if 'dir' in style else 'normal' | 141 self._type = 'dir' if 'dir' in style else 'normal' |
142 self.__home_path = os.path.expanduser(u'~') | 142 self.__home_path = os.path.expanduser('~') |
143 widgets = [] | 143 widgets = [] |
144 if message: | 144 if message: |
145 widgets.append(urwid.Text(message)) | 145 widgets.append(urwid.Text(message)) |
146 widgets.append(urwid.Divider(u'─')) | 146 widgets.append(urwid.Divider('─')) |
147 self.path_wid = PathEdit(_(u'Path: ')) | 147 self.path_wid = PathEdit(_('Path: ')) |
148 self.path_wid.setCompletionMethod(self._directory_completion) | 148 self.path_wid.setCompletionMethod(self._directory_completion) |
149 urwid.connect_signal(self.path_wid, 'change', self.onPathChange) | 149 urwid.connect_signal(self.path_wid, 'change', self.onPathChange) |
150 widgets.append(self.path_wid) | 150 widgets.append(self.path_wid) |
151 widgets.append(urwid.Divider(u'─')) | 151 widgets.append(urwid.Divider('─')) |
152 header = urwid.Pile(widgets) | 152 header = urwid.Pile(widgets) |
153 bookm_list = urwid.SimpleListWalker([]) | 153 bookm_list = urwid.SimpleListWalker([]) |
154 self.bookmarks = list(self.getBookmarks()) | 154 self.bookmarks = list(self.getBookmarks()) |
155 self.bookmarks.sort() | 155 self.bookmarks.sort() |
156 for bookmark in self.bookmarks: | 156 for bookmark in self.bookmarks: |
157 if bookmark.startswith(self.__home_path): | 157 if bookmark.startswith(self.__home_path): |
158 bookmark=u"~"+bookmark[len(self.__home_path):] | 158 bookmark="~"+bookmark[len(self.__home_path):] |
159 book_wid = sat_widgets.ClickableText(bookmark) | 159 book_wid = sat_widgets.ClickableText(bookmark) |
160 urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected) | 160 urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected) |
161 bookm_list.append(book_wid) | 161 bookm_list.append(book_wid) |
162 bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_(u'Bookmarks'),'center'),'title')) | 162 bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_('Bookmarks'),'center'),'title')) |
163 self.files_wid = FilesViewer(self.onPreviousDir, self.onDirClick, self.onFileClick if self._type == 'normal' else None) | 163 self.files_wid = FilesViewer(self.onPreviousDir, self.onDirClick, self.onFileClick if self._type == 'normal' else None) |
164 center_row = urwid.Columns([('weight',2,bookm_wid), | 164 center_row = urwid.Columns([('weight',2,bookm_wid), |
165 ('weight',8,sat_widgets.VerticalSeparator(self.files_wid))]) | 165 ('weight',8,sat_widgets.VerticalSeparator(self.files_wid))]) |
166 | 166 |
167 buttons = [] | 167 buttons = [] |
171 max_len = max([button.getSize() for button in buttons]) | 171 max_len = max([button.getSize() for button in buttons]) |
172 buttons_wid = urwid.GridFlow(buttons,max_len,1,0,'center') | 172 buttons_wid = urwid.GridFlow(buttons,max_len,1,0,'center') |
173 main_frame = sat_widgets.FocusFrame(center_row, header, buttons_wid) | 173 main_frame = sat_widgets.FocusFrame(center_row, header, buttons_wid) |
174 decorated = sat_widgets.LabelLine(main_frame, sat_widgets.SurroundedText(title)) | 174 decorated = sat_widgets.LabelLine(main_frame, sat_widgets.SurroundedText(title)) |
175 urwid.WidgetWrap.__init__(self, decorated) | 175 urwid.WidgetWrap.__init__(self, decorated) |
176 self.path_wid.set_edit_text(os.getcwdu()) | 176 self.path_wid.set_edit_text(os.getcwd()) |
177 | 177 |
178 def _validateDir(self, wid): | 178 def _validateDir(self, wid): |
179 """ call ok callback if current path is a dir """ | 179 """ call ok callback if current path is a dir """ |
180 path = os.path.abspath(self.path_wid.get_edit_text()) | 180 path = os.path.abspath(self.path_wid.get_edit_text()) |
181 if os.path.isdir(path): | 181 if os.path.isdir(path): |
182 self.ok_cb(path) | 182 self.ok_cb(path) |
183 | 183 |
184 def _directory_completion(self, path, completion_data): | 184 def _directory_completion(self, path, completion_data): |
185 assert isinstance(path, unicode) | 185 assert isinstance(path, str) |
186 path=os.path.abspath(path) | 186 path=os.path.abspath(path) |
187 if not os.path.isdir(path): | 187 if not os.path.isdir(path): |
188 head,dir_start = os.path.split(path) | 188 head,dir_start = os.path.split(path) |
189 else: | 189 else: |
190 head=path | 190 head=path |
191 dir_start=u'' | 191 dir_start='' |
192 try: | 192 try: |
193 filenames = os.listdir(head) | 193 filenames = os.listdir(head) |
194 to_remove = [] | 194 to_remove = [] |
195 | 195 |
196 # we remove badly encoded files | 196 # we remove badly encoded files |
197 for filename in filenames: | 197 for filename in filenames: |
198 if not isinstance(filename, unicode): | 198 if not isinstance(filename, str): |
199 log.warning(u"file [{}] has a badly encode filename, ignoring it".format(filename.decode('utf-8', 'replace'))) | 199 log.warning("file [{}] has a badly encode filename, ignoring it".format(filename.decode('utf-8', 'replace'))) |
200 to_remove.append(filename) | 200 to_remove.append(filename) |
201 for filename in to_remove: | 201 for filename in to_remove: |
202 filenames.remove(filename) | 202 filenames.remove(filename) |
203 | 203 |
204 filenames.sort() | 204 filenames.sort() |
206 start_idx=filenames.index(completion_data['last_dir'])+1 | 206 start_idx=filenames.index(completion_data['last_dir'])+1 |
207 if start_idx == len(filenames): | 207 if start_idx == len(filenames): |
208 start_idx = 0 | 208 start_idx = 0 |
209 except (KeyError,ValueError): | 209 except (KeyError,ValueError): |
210 start_idx = 0 | 210 start_idx = 0 |
211 for idx in range(start_idx,len(filenames)) + range(0,start_idx): | 211 for idx in list(range(start_idx,len(filenames))) + list(range(0,start_idx)): |
212 full_path = os.path.join(head,filenames[idx]) | 212 full_path = os.path.join(head,filenames[idx]) |
213 if filenames[idx].lower().startswith(dir_start.lower()) and os.path.isdir(full_path): | 213 if filenames[idx].lower().startswith(dir_start.lower()) and os.path.isdir(full_path): |
214 completion_data['last_dir'] = filenames[idx] | 214 completion_data['last_dir'] = filenames[idx] |
215 return full_path | 215 return full_path |
216 except OSError: | 216 except OSError: |
217 pass | 217 pass |
218 return path | 218 return path |
219 | 219 |
220 def getBookmarks(self): | 220 def getBookmarks(self): |
221 gtk_bookm = os.path.expanduser(u"~/.gtk-bookmarks") | 221 gtk_bookm = os.path.expanduser("~/.gtk-bookmarks") |
222 kde_bookm = os.path.expanduser(u"~/.kde/share/apps/kfileplaces/bookmarks.xml") | 222 kde_bookm = os.path.expanduser("~/.kde/share/apps/kfileplaces/bookmarks.xml") |
223 bookmarks = set() | 223 bookmarks = set() |
224 try: | 224 try: |
225 with open(gtk_bookm) as gtk_fd: | 225 with open(gtk_bookm) as gtk_fd: |
226 for bm in gtk_fd.readlines(): | 226 for bm in gtk_fd.readlines(): |
227 if bm.startswith("file:///"): | 227 if bm.startswith("file:///"): |
228 bookmarks.add(bm[7:].replace('\n','').decode('utf-8', 'replace')) | 228 bookmarks.add(bm[7:].replace('\n','').decode('utf-8', 'replace')) |
229 except IOError: | 229 except IOError: |
230 log.info(_(u'No GTK bookmarks file found')) | 230 log.info(_('No GTK bookmarks file found')) |
231 pass | 231 pass |
232 | 232 |
233 try: | 233 try: |
234 dom = minidom.parse(kde_bookm) | 234 dom = minidom.parse(kde_bookm) |
235 for elem in dom.getElementsByTagName('bookmark'): | 235 for elem in dom.getElementsByTagName('bookmark'): |