Mercurial > urwid-satext
annotate urwid_satext/files_management.py @ 35:5a8e2200ddc0
using glob for setup.py
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 28 Dec 2010 22:08:34 +0100 |
parents | 875ff127b2dd |
children | 7155b81ffdd5 |
rev | line source |
---|---|
21 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Primitivus: a SAT frontend | |
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 import urwid | |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
23 import sat_widgets |
21 | 24 import os, os.path |
25 from xml.dom import minidom | |
26 from logging import debug, info, error | |
23 | 27 from time import time |
34
875ff127b2dd
- i18n management: gettext integration + french translation
Goffi <goffi@goffi.org>
parents:
32
diff
changeset
|
28 |
875ff127b2dd
- i18n management: gettext integration + french translation
Goffi <goffi@goffi.org>
parents:
32
diff
changeset
|
29 import gettext |
875ff127b2dd
- i18n management: gettext integration + french translation
Goffi <goffi@goffi.org>
parents:
32
diff
changeset
|
30 gettext.install('urwid_satext', unicode=True) |
21 | 31 |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
32 class PathEdit(sat_widgets.AdvancedEdit): |
23 | 33 """AdvancedEdit with manage file paths""" |
21 | 34 |
35 def keypress(self, size, key): | |
23 | 36 if key == '~' and self.edit_pos==0: |
37 expanded = os.path.expanduser('~') | |
38 self.set_edit_text(os.path.normpath(expanded+'/'+self.edit_text)) | |
39 self.set_edit_pos(len(expanded)+1) | |
40 elif key == 'ctrl w': | |
21 | 41 if self.edit_pos<2: |
42 return | |
43 before = self.edit_text[:self.edit_pos] | |
44 pos = (before[:-1] if before.endswith('/') else before).rfind("/")+1 | |
45 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:]) | |
46 self.set_edit_pos(pos) | |
47 return | |
48 else: | |
49 return super(PathEdit, self).keypress(size, key) | |
50 | |
23 | 51 class FilesViewer(urwid.WidgetWrap): |
52 """List specialised for files""" | |
53 | |
54 def __init__(self, onPreviousDir, onDirClick, onFileClick): | |
55 self.path='' | |
56 self.key_cache = '' | |
57 self.key_time = time() | |
58 self.onPreviousDir = onPreviousDir | |
59 self.onDirClick = onDirClick | |
60 self.onFileClick = onFileClick | |
61 self.files_list = urwid.SimpleListWalker([]) | |
62 self.show_hidden = True | |
63 listbox = urwid.ListBox(self.files_list) | |
64 urwid.WidgetWrap.__init__(self, listbox) | |
65 | |
66 def keypress(self, size, key): | |
67 if key=='meta h': | |
68 #(un)hide hidden files | |
69 self.show_hidden = not self.show_hidden | |
70 self.showDirectory(self.path) | |
71 if key=='meta d': | |
72 #jump to directories | |
73 if self.files_list: | |
74 self._w.set_focus(0) | |
75 elif key=='meta f': | |
76 for idx in range(len(self.files_list)): | |
77 if isinstance(self.files_list[idx].base_widget,urwid.Divider): | |
78 if idx<len(self.files_list)-1: | |
79 self._w.set_focus(idx+1) | |
80 break | |
81 elif len(key) == 1: | |
82 if time() - self.key_time > 2: | |
83 self.key_cache=key | |
84 else: | |
85 self.key_cache+=key | |
86 self.key_time = time() | |
87 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
|
88 if isinstance(self.files_list[idx],sat_widgets.ClickableText) and self.files_list[idx].get_text().lower().startswith(self.key_cache.lower()): |
23 | 89 self._w.set_focus(idx) |
90 break | |
91 else: | |
92 return self._w.keypress(size, key) | |
93 | |
94 def showDirectory(self, path): | |
95 self.path = path | |
96 del self.files_list[:] | |
97 directories = [] | |
98 files = [] | |
99 try: | |
100 for filename in os.listdir(path): | |
101 fullpath = os.path.join(path,filename) | |
102 if os.path.isdir(fullpath): | |
103 directories.append(filename) | |
104 else: | |
105 files.append(filename) | |
106 except OSError: | |
107 self.files_list.append(urwid.Text(("warning",_("Impossible to list directory")),'center')) | |
108 directories.sort() | |
109 files.sort() | |
110 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
|
111 previous_wid = sat_widgets.ClickableText(('directory','..')) |
23 | 112 urwid.connect_signal(previous_wid,'click',self.onPreviousDir) |
113 self.files_list.append(previous_wid) | |
114 for directory in directories: | |
115 if directory.startswith('.') and not self.show_hidden: | |
116 continue | |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
117 dir_wid = sat_widgets.ClickableText(('directory',directory)) |
23 | 118 urwid.connect_signal(dir_wid,'click',self.onDirClick) |
119 self.files_list.append(dir_wid) | |
120 self.files_list.append(urwid.AttrMap(urwid.Divider('-'),'separator')) | |
121 for filename in files: | |
122 if filename.startswith('.') and not self.show_hidden: | |
123 continue | |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
124 file_wid = sat_widgets.ClickableText(filename) |
23 | 125 urwid.connect_signal(file_wid,'click',self.onFileClick) |
126 self.files_list.append(file_wid) | |
127 | |
128 | |
129 | |
21 | 130 class FileDialog(urwid.WidgetWrap): |
131 | |
23 | 132 def __init__(self, ok_cb, cancel_cb, title=_("Please select a file"), style=[]): |
133 """Create file dialog | |
134 @param title: title of the window/popup | |
135 @param style: NOT USED YET #FIXME | |
136 """ | |
137 self.ok_cb = ok_cb | |
21 | 138 self.__home_path = os.path.expanduser('~') |
23 | 139 self.path_wid = PathEdit(_('Path: ')) |
140 self.path_wid.setCompletionMethod(self._directory_completion) | |
21 | 141 urwid.connect_signal(self.path_wid, 'change', self.onPathChange) |
142 header = urwid.Pile([self.path_wid, urwid.Divider(u'─')]) | |
143 bookm_list = urwid.SimpleListWalker([]) | |
144 self.bookmarks = list(self.getBookmarks()) | |
145 self.bookmarks.sort() | |
146 for bookmark in self.bookmarks: | |
147 if bookmark.startswith(self.__home_path): | |
148 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
|
149 book_wid = sat_widgets.ClickableText(bookmark) |
21 | 150 urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected) |
151 bookm_list.append(book_wid) | |
152 bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_('Bookmarks'),'center'),'title')) | |
23 | 153 self.files_wid = FilesViewer(self.onPreviousDir, self.onDirClick, self.onFileClick) |
21 | 154 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
|
155 ('weight',8,sat_widgets.VerticalSeparator(self.files_wid))]) |
23 | 156 |
157 buttons = [] | |
30
1aeb3540aa49
files reorganisation after project separation. new README, and COPYING files
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
158 buttons.append(sat_widgets.CustomButton(_('Cancel'),cancel_cb)) |
23 | 159 max_len = max([button.getSize() for button in buttons]) |
160 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
|
161 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
|
162 decorated = sat_widgets.LabelLine(main_frame, sat_widgets.SurroundedText(title)) |
21 | 163 urwid.WidgetWrap.__init__(self, decorated) |
23 | 164 self.path_wid.set_edit_text(os.getcwdu()) |
165 | |
166 def _directory_completion(self, path, completion_data): | |
167 path=os.path.abspath(path) | |
168 if not os.path.isdir(path): | |
169 head,dir_start = os.path.split(path) | |
170 else: | |
171 head=path | |
172 dir_start='' | |
173 try: | |
174 filenames = os.listdir(head) | |
175 filenames.sort() | |
176 try: | |
177 start_idx=filenames.index(completion_data['last_dir'])+1 | |
178 if start_idx == len(filenames): | |
179 start_idx = 0 | |
180 except (KeyError,ValueError): | |
181 start_idx = 0 | |
182 for idx in range(start_idx,len(filenames)) + range(0,start_idx): | |
183 full_path = os.path.join(head,filenames[idx]) | |
184 if filenames[idx].lower().startswith(dir_start.lower()) and os.path.isdir(full_path): | |
185 completion_data['last_dir'] = filenames[idx] | |
186 return full_path | |
187 except OSError: | |
188 pass | |
189 return path | |
21 | 190 |
191 def getBookmarks(self): | |
23 | 192 gtk_bookm = os.path.expanduser("~/.gtk-bookmarks") |
193 kde_bookm = os.path.expanduser("~/.kde/share/apps/kfileplaces/bookmarks.xml") | |
21 | 194 bookmarks = set() |
195 try: | |
23 | 196 with open(gtk_bookm) as gtk_fd: |
197 for bm in gtk_fd.readlines(): | |
21 | 198 if bm.startswith("file:///"): |
199 bookmarks.add(bm[7:].replace('\n','')) | |
200 except IOError: | |
23 | 201 info(_('No GTK bookmarks file found')) |
21 | 202 pass |
203 | |
204 try: | |
205 dom = minidom.parse(kde_bookm) | |
23 | 206 for elem in dom.getElementsByTagName('bookmark'): |
21 | 207 bm = elem.getAttribute("href") |
208 if bm.startswith("file:///"): | |
209 bookmarks.add(bm[7:]) | |
210 except IOError: | |
211 info(_('No KDE bookmarks file found')) | |
212 pass | |
213 | |
214 return bookmarks | |
215 | |
216 def onBookmarkSelected(self, button): | |
217 self.path_wid.set_edit_text(os.path.expanduser(button.get_text())) | |
218 | |
23 | 219 def onPathChange(self, edit, path): |
220 if os.path.isdir(path): | |
221 self.files_wid.showDirectory(path) | |
222 | |
223 def onPreviousDir(self, wid): | |
224 path = os.path.abspath(self.path_wid.get_edit_text()) | |
225 if not os.path.isdir(path): | |
226 path = dirname(path) | |
227 self.path_wid.set_edit_text(os.path.split(path)[0]) | |
228 | |
229 def onDirClick(self, wid): | |
230 path = os.path.abspath(self.path_wid.get_edit_text()) | |
231 if not os.path.isdir(path): | |
232 path = dirname(path) | |
233 self.path_wid.set_edit_text(os.path.join(path,wid.get_text())) | |
234 | |
235 def onFileClick(self, wid): | |
236 self.ok_cb(os.path.abspath(os.path.join(self.files_wid.path,wid.get_text()))) |