comparison urwid_satext/files_management.py @ 151:6689aa54b20c default tip

refactoring from camelCase -> snake_case: This libraries was using camelCase due for historical reasons (related to the use of Twisted in the initial project). This patch fixes it by using PEP8 compliant snake_case
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 15:38:18 +0200
parents 144bdf877d21
children
comparison
equal deleted inserted replaced
150:aa8f46b43a71 151:6689aa54b20c
45 return super(PathEdit, self).keypress(size, key) 45 return super(PathEdit, self).keypress(size, key)
46 46
47 class FilesViewer(urwid.WidgetWrap): 47 class FilesViewer(urwid.WidgetWrap):
48 """List specialised for files""" 48 """List specialised for files"""
49 49
50 def __init__(self, onPreviousDir, onDirClick, onFileClick = None): 50 def __init__(self, on_previous_dir, on_dir_click, on_file_click = None):
51 self.path='' 51 self.path=''
52 self.key_cache = '' 52 self.key_cache = ''
53 self.key_time = time() 53 self.key_time = time()
54 self.onPreviousDir = onPreviousDir 54 self.on_previous_dir = on_previous_dir
55 self.onDirClick = onDirClick 55 self.on_dir_click = on_dir_click
56 self.onFileClick = onFileClick 56 self.on_file_click = on_file_click
57 self.files_list = urwid.SimpleListWalker([]) 57 self.files_list = urwid.SimpleListWalker([])
58 self.show_hidden = True 58 self.show_hidden = True
59 listbox = urwid.ListBox(self.files_list) 59 listbox = urwid.ListBox(self.files_list)
60 urwid.WidgetWrap.__init__(self, listbox) 60 urwid.WidgetWrap.__init__(self, listbox)
61 61
62 def keypress(self, size, key): 62 def keypress(self, size, key):
63 if key==a_key['FILES_HIDDEN_HIDE']: 63 if key==a_key['FILES_HIDDEN_HIDE']:
64 #(un)hide hidden files 64 #(un)hide hidden files
65 self.show_hidden = not self.show_hidden 65 self.show_hidden = not self.show_hidden
66 self.showDirectory(self.path) 66 self.show_directory(self.path)
67 elif key==a_key['FILES_JUMP_DIRECTORIES']: 67 elif key==a_key['FILES_JUMP_DIRECTORIES']:
68 #jump to directories 68 #jump to directories
69 if self.files_list: 69 if self.files_list:
70 self._w.set_focus(0) 70 self._w.set_focus(0)
71 elif key==a_key['FILES_JUMP_FILES']: 71 elif key==a_key['FILES_JUMP_FILES']:
85 self._w.set_focus(idx) 85 self._w.set_focus(idx)
86 break 86 break
87 else: 87 else:
88 return self._w.keypress(size, key) 88 return self._w.keypress(size, key)
89 89
90 def showDirectory(self, path): 90 def show_directory(self, path):
91 self.path = path 91 self.path = path
92 del self.files_list[:] 92 del self.files_list[:]
93 directories = [] 93 directories = []
94 files = [] 94 files = []
95 try: 95 try:
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)!='/' and os.path.abspath(path) != '//': 109 if os.path.abspath(path)!='/' and os.path.abspath(path) != '//':
110 previous_wid = sat_widgets.ClickableText(('directory','..')) 110 previous_wid = sat_widgets.ClickableText(('directory','..'))
111 urwid.connect_signal(previous_wid,'click',self.onPreviousDir) 111 urwid.connect_signal(previous_wid,'click',self.on_previous_dir)
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(('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.on_dir_click)
118 self.files_list.append(dir_wid) 118 self.files_list.append(dir_wid)
119 self.files_list.append(urwid.AttrMap(urwid.Divider('-'),'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('.') 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.on_file_click:
125 urwid.connect_signal(file_wid,'click',self.onFileClick) 125 urwid.connect_signal(file_wid,'click',self.on_file_click)
126 self.files_list.append(file_wid) 126 self.files_list.append(file_wid)
127 127
128 128
129 class FileDialog(urwid.WidgetWrap): 129 class FileDialog(urwid.WidgetWrap):
130 130
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('─')) 146 widgets.append(urwid.Divider('─'))
147 self.path_wid = PathEdit(_('Path: ')) 147 self.path_wid = PathEdit(_('Path: '))
148 self.path_wid.setCompletionMethod(self._directory_completion) 148 self.path_wid.set_completion_method(self._directory_completion)
149 urwid.connect_signal(self.path_wid, 'change', self.onPathChange) 149 urwid.connect_signal(self.path_wid, 'change', self.on_path_change)
150 widgets.append(self.path_wid) 150 widgets.append(self.path_wid)
151 widgets.append(urwid.Divider('─')) 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.get_bookmarks())
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="~"+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.on_bookmark_selected)
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(_('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.on_previous_dir, self.on_dir_click, self.on_file_click 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 = []
168 if self._type == 'dir': 168 if self._type == 'dir':
169 buttons.append(sat_widgets.CustomButton(_('Ok'), self._validateDir)) 169 buttons.append(sat_widgets.CustomButton(_('Ok'), self._validate_dir))
170 buttons.append(sat_widgets.CustomButton(_('Cancel'),cancel_cb)) 170 buttons.append(sat_widgets.CustomButton(_('Cancel'),cancel_cb))
171 max_len = max([button.getSize() for button in buttons]) 171 max_len = max([button.get_size() 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.getcwd()) 176 self.path_wid.set_edit_text(os.getcwd())
177 177
178 def _validateDir(self, wid): 178 def _validate_dir(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
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 get_bookmarks(self):
221 gtk_bookm = os.path.expanduser("~/.gtk-bookmarks") 221 gtk_bookm = os.path.expanduser("~/.gtk-bookmarks")
222 kde_bookm = os.path.expanduser("~/.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:
240 log.info(_('No KDE bookmarks file found')) 240 log.info(_('No KDE bookmarks file found'))
241 pass 241 pass
242 242
243 return bookmarks 243 return bookmarks
244 244
245 def onBookmarkSelected(self, button): 245 def on_bookmark_selected(self, button):
246 self.path_wid.set_edit_text(os.path.expanduser(button.get_text())) 246 self.path_wid.set_edit_text(os.path.expanduser(button.get_text()))
247 247
248 def onPathChange(self, edit, path): 248 def on_path_change(self, edit, path):
249 if os.path.isdir(path): 249 if os.path.isdir(path):
250 self.files_wid.showDirectory(path) 250 self.files_wid.show_directory(path)
251 251
252 def onPreviousDir(self, wid): 252 def on_previous_dir(self, wid):
253 path = os.path.abspath(self.path_wid.get_edit_text()) 253 path = os.path.abspath(self.path_wid.get_edit_text())
254 if not os.path.isdir(path): 254 if not os.path.isdir(path):
255 path = os.path.dirname(path) 255 path = os.path.dirname(path)
256 self.path_wid.set_edit_text(os.path.split(path)[0]) 256 self.path_wid.set_edit_text(os.path.split(path)[0])
257 257
258 def onDirClick(self, wid): 258 def on_dir_click(self, wid):
259 path = os.path.abspath(self.path_wid.get_edit_text()) 259 path = os.path.abspath(self.path_wid.get_edit_text())
260 if not os.path.isdir(path): 260 if not os.path.isdir(path):
261 path = os.path.dirname(path) 261 path = os.path.dirname(path)
262 self.path_wid.set_edit_text(os.path.join(path,wid.get_text())) 262 self.path_wid.set_edit_text(os.path.join(path,wid.get_text()))
263 263
264 def onFileClick(self, wid): 264 def on_file_click(self, wid):
265 self.ok_cb(os.path.abspath(os.path.join(self.files_wid.path,wid.get_text()))) 265 self.ok_cb(os.path.abspath(os.path.join(self.files_wid.path,wid.get_text())))