annotate urwid_satext/files_management.py @ 130:406c310e52b5 0.7.0a1

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