Mercurial > urwid-satext
annotate urwid_satext/files_management.py @ 81:0ebd13729039
fixes default values for methods selectValue(s) of class List
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 07 May 2014 19:49:07 +0200 |
parents | eddb8369ba06 |
children | fdd0543677d4 |
rev | line source |
---|---|
21 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
64 | 4 # Urwid SàT extensions |
71 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 | |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
21 import sat_widgets |
21 | 22 import os, os.path |
23 from xml.dom import minidom | |
24 from logging import debug, info, error | |
23 | 25 from time import time |
34
875ff127b2dd
- i18n management: gettext integration + french translation
Goffi <goffi@goffi.org>
parents:
32
diff
changeset
|
26 |
875ff127b2dd
- i18n management: gettext integration + french translation
Goffi <goffi@goffi.org>
parents:
32
diff
changeset
|
27 import gettext |
875ff127b2dd
- i18n management: gettext integration + french translation
Goffi <goffi@goffi.org>
parents:
32
diff
changeset
|
28 gettext.install('urwid_satext', unicode=True) |
21 | 29 |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
30 class PathEdit(sat_widgets.AdvancedEdit): |
23 | 31 """AdvancedEdit with manage file paths""" |
66 | 32 |
21 | 33 def keypress(self, size, key): |
23 | 34 if key == '~' and self.edit_pos==0: |
35 expanded = os.path.expanduser('~') | |
36 self.set_edit_text(os.path.normpath(expanded+'/'+self.edit_text)) | |
37 self.set_edit_pos(len(expanded)+1) | |
38 elif key == 'ctrl w': | |
21 | 39 if self.edit_pos<2: |
40 return | |
41 before = self.edit_text[:self.edit_pos] | |
42 pos = (before[:-1] if before.endswith('/') else before).rfind("/")+1 | |
43 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:]) | |
44 self.set_edit_pos(pos) | |
45 return | |
46 else: | |
66 | 47 return super(PathEdit, self).keypress(size, key) |
21 | 48 |
23 | 49 class FilesViewer(urwid.WidgetWrap): |
50 """List specialised for files""" | |
51 | |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
52 def __init__(self, onPreviousDir, onDirClick, onFileClick = None): |
23 | 53 self.path='' |
54 self.key_cache = '' | |
55 self.key_time = time() | |
56 self.onPreviousDir = onPreviousDir | |
57 self.onDirClick = onDirClick | |
58 self.onFileClick = onFileClick | |
59 self.files_list = urwid.SimpleListWalker([]) | |
66 | 60 self.show_hidden = True |
23 | 61 listbox = urwid.ListBox(self.files_list) |
62 urwid.WidgetWrap.__init__(self, listbox) | |
63 | |
64 def keypress(self, size, key): | |
65 if key=='meta h': | |
66 #(un)hide hidden files | |
67 self.show_hidden = not self.show_hidden | |
68 self.showDirectory(self.path) | |
69 if key=='meta d': | |
70 #jump to directories | |
71 if self.files_list: | |
72 self._w.set_focus(0) | |
73 elif key=='meta f': | |
74 for idx in range(len(self.files_list)): | |
75 if isinstance(self.files_list[idx].base_widget,urwid.Divider): | |
76 if idx<len(self.files_list)-1: | |
77 self._w.set_focus(idx+1) | |
78 break | |
79 elif len(key) == 1: | |
80 if time() - self.key_time > 2: | |
81 self.key_cache=key | |
82 else: | |
83 self.key_cache+=key | |
84 self.key_time = time() | |
85 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
|
86 if isinstance(self.files_list[idx],sat_widgets.ClickableText) and self.files_list[idx].get_text().lower().startswith(self.key_cache.lower()): |
23 | 87 self._w.set_focus(idx) |
88 break | |
89 else: | |
90 return self._w.keypress(size, key) | |
91 | |
92 def showDirectory(self, path): | |
93 self.path = path | |
94 del self.files_list[:] | |
95 directories = [] | |
96 files = [] | |
97 try: | |
98 for filename in os.listdir(path): | |
99 fullpath = os.path.join(path,filename) | |
100 if os.path.isdir(fullpath): | |
101 directories.append(filename) | |
102 else: | |
103 files.append(filename) | |
104 except OSError: | |
66 | 105 self.files_list.append(urwid.Text(("warning",_("Impossible to list directory")),'center')) |
23 | 106 directories.sort() |
107 files.sort() | |
108 if os.path.abspath(path)!='/' and os.path.abspath(path) != '//': | |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
109 previous_wid = sat_widgets.ClickableText(('directory','..')) |
23 | 110 urwid.connect_signal(previous_wid,'click',self.onPreviousDir) |
111 self.files_list.append(previous_wid) | |
112 for directory in directories: | |
113 if directory.startswith('.') and not self.show_hidden: | |
114 continue | |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
115 dir_wid = sat_widgets.ClickableText(('directory',directory)) |
23 | 116 urwid.connect_signal(dir_wid,'click',self.onDirClick) |
117 self.files_list.append(dir_wid) | |
118 self.files_list.append(urwid.AttrMap(urwid.Divider('-'),'separator')) | |
119 for filename in files: | |
120 if filename.startswith('.') and not self.show_hidden: | |
121 continue | |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
122 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
|
123 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
|
124 urwid.connect_signal(file_wid,'click',self.onFileClick) |
23 | 125 self.files_list.append(file_wid) |
126 | |
127 | |
128 | |
21 | 129 class FileDialog(urwid.WidgetWrap): |
130 | |
23 | 131 def __init__(self, ok_cb, cancel_cb, title=_("Please select a file"), style=[]): |
132 """Create file dialog | |
133 @param title: title of the window/popup | |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
134 @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
|
135 - 'dir' if a dir path must be selected |
23 | 136 """ |
137 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
|
138 self._type = 'dir' if 'dir' in style else 'normal' |
21 | 139 self.__home_path = os.path.expanduser('~') |
23 | 140 self.path_wid = PathEdit(_('Path: ')) |
141 self.path_wid.setCompletionMethod(self._directory_completion) | |
21 | 142 urwid.connect_signal(self.path_wid, 'change', self.onPathChange) |
143 header = urwid.Pile([self.path_wid, urwid.Divider(u'─')]) | |
144 bookm_list = urwid.SimpleListWalker([]) | |
145 self.bookmarks = list(self.getBookmarks()) | |
146 self.bookmarks.sort() | |
147 for bookmark in self.bookmarks: | |
148 if bookmark.startswith(self.__home_path): | |
149 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
|
150 book_wid = sat_widgets.ClickableText(bookmark) |
21 | 151 urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected) |
152 bookm_list.append(book_wid) | |
153 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
|
154 self.files_wid = FilesViewer(self.onPreviousDir, self.onDirClick, self.onFileClick if self._type == 'normal' else None) |
21 | 155 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
|
156 ('weight',8,sat_widgets.VerticalSeparator(self.files_wid))]) |
23 | 157 |
158 buttons = [] | |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
159 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
|
160 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
|
161 buttons.append(sat_widgets.CustomButton(_('Cancel'),cancel_cb)) |
23 | 162 max_len = max([button.getSize() for button in buttons]) |
163 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
|
164 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
|
165 decorated = sat_widgets.LabelLine(main_frame, sat_widgets.SurroundedText(title)) |
21 | 166 urwid.WidgetWrap.__init__(self, decorated) |
23 | 167 self.path_wid.set_edit_text(os.getcwdu()) |
168 | |
58
7155b81ffdd5
new 'dir' style in FileDialog (get a dir path instead of a file
Goffi <goffi@goffi.org>
parents:
34
diff
changeset
|
169 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
|
170 """ 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
|
171 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
|
172 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
|
173 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
|
174 |
23 | 175 def _directory_completion(self, path, completion_data): |
176 path=os.path.abspath(path) | |
177 if not os.path.isdir(path): | |
178 head,dir_start = os.path.split(path) | |
179 else: | |
180 head=path | |
181 dir_start='' | |
182 try: | |
183 filenames = os.listdir(head) | |
184 filenames.sort() | |
185 try: | |
186 start_idx=filenames.index(completion_data['last_dir'])+1 | |
187 if start_idx == len(filenames): | |
188 start_idx = 0 | |
189 except (KeyError,ValueError): | |
190 start_idx = 0 | |
191 for idx in range(start_idx,len(filenames)) + range(0,start_idx): | |
192 full_path = os.path.join(head,filenames[idx]) | |
193 if filenames[idx].lower().startswith(dir_start.lower()) and os.path.isdir(full_path): | |
194 completion_data['last_dir'] = filenames[idx] | |
66 | 195 return full_path |
23 | 196 except OSError: |
197 pass | |
198 return path | |
21 | 199 |
200 def getBookmarks(self): | |
23 | 201 gtk_bookm = os.path.expanduser("~/.gtk-bookmarks") |
202 kde_bookm = os.path.expanduser("~/.kde/share/apps/kfileplaces/bookmarks.xml") | |
21 | 203 bookmarks = set() |
204 try: | |
23 | 205 with open(gtk_bookm) as gtk_fd: |
206 for bm in gtk_fd.readlines(): | |
21 | 207 if bm.startswith("file:///"): |
208 bookmarks.add(bm[7:].replace('\n','')) | |
209 except IOError: | |
23 | 210 info(_('No GTK bookmarks file found')) |
21 | 211 pass |
66 | 212 |
21 | 213 try: |
214 dom = minidom.parse(kde_bookm) | |
23 | 215 for elem in dom.getElementsByTagName('bookmark'): |
21 | 216 bm = elem.getAttribute("href") |
217 if bm.startswith("file:///"): | |
218 bookmarks.add(bm[7:]) | |
219 except IOError: | |
220 info(_('No KDE bookmarks file found')) | |
221 pass | |
66 | 222 |
21 | 223 return bookmarks |
224 | |
225 def onBookmarkSelected(self, button): | |
226 self.path_wid.set_edit_text(os.path.expanduser(button.get_text())) | |
227 | |
23 | 228 def onPathChange(self, edit, path): |
229 if os.path.isdir(path): | |
230 self.files_wid.showDirectory(path) | |
231 | |
232 def onPreviousDir(self, wid): | |
233 path = os.path.abspath(self.path_wid.get_edit_text()) | |
234 if not os.path.isdir(path): | |
235 path = dirname(path) | |
236 self.path_wid.set_edit_text(os.path.split(path)[0]) | |
237 | |
238 def onDirClick(self, wid): | |
239 path = os.path.abspath(self.path_wid.get_edit_text()) | |
240 if not os.path.isdir(path): | |
241 path = dirname(path) | |
242 self.path_wid.set_edit_text(os.path.join(path,wid.get_text())) | |
66 | 243 |
23 | 244 def onFileClick(self, wid): |
245 self.ok_cb(os.path.abspath(os.path.join(self.files_wid.path,wid.get_text()))) |