3 # vim: sw=4 ts=4 expandtab ai
6 from os.path import join, abspath, normcase, isdir, getsize
7 from heapq import nlargest
8 from fnmatch import fnmatch
9 from sys import platform
11 from misc import size_hum_read, _
12 from config import config
15 ('out_table', _('Table')),
16 ('out_diabar', _('Bar chart')),
17 ('out_diapie', _('Pie chart')),
18 ('out_diaold', _('Old chart')),
21 #==============================================================================
23 class Control(object):
25 def __init__(self, ui, outtype, params):
26 self.present = eval(ui + '_Presentation(self.start_search, outtype, params)')
27 self.abstrac = Abstraction(self.present)
29 self.toplevel = self.present.toplevel
31 def start_search(self, get_criteria, get_stopit):
33 outtype, start_path, count, file_filter = get_criteria()
34 search_func = self.abstrac.filegetter(start_path, file_filter, get_stopit)
36 for fsize, fpath in nlargest(count, search_func):
37 filelist.append([int(fsize), fpath, size_hum_read(fsize)])
40 self.present.nothing_founded()
42 results = [filelist, start_path]
43 self.present.show_out_toplevel(outtype, results)
48 #==============================================================================
50 class Abstraction(object):
52 def __init__(self, presentation):
53 self.ignore_dirs = config['files']['ignore_dirs']
54 self.presentation = presentation
56 def filegetter(self, startdir, file_filter, get_stopit):
57 """Generator of file sizes and paths based on os.walk."""
58 # Walk across directory tree
59 for dirpath, dirnames, fnames in walk(startdir):
60 # Eliminate unnecessary directories
61 ignore_dirs = self.ignore_dirs
62 for ign_dir in ignore_dirs[:]:
63 for dirname in dirnames[:]:
64 if ign_dir == normcase(join(abspath(dirpath), dirname)):
65 dirnames.remove(dirname)
66 ignore_dirs.remove(ign_dir)
69 # Store only necessary files
70 for mask in file_filter:
71 if fnmatch(fname, mask):
72 if platform == 'win32':
73 # Crutch for non-unicode names
74 flpath = unicode(join(dirpath, fname), '1251')
76 flpath = join(dirpath, fname)
78 self.presentation.show_current_status(flpath)
79 # Stop search via 'stopit' signal
85 # Query only valid files
87 # Return results (bytesize, path)
88 yield getsize(flpath), flpath
92 #==============================================================================
94 class Cli_Presentation(object):
95 def __init__(self, start_func, outtype, params):
96 self.start_func = start_func
98 self.outtype = outtype
99 self.start_path = params[0]
100 self.count = params[1]
102 self.file_filter = params[2].split(';')
104 self.file_filter = '*.*'
111 return self.outtype, self.start_path, int(self.count), self.file_filter
113 def get_stopit(self):
116 def show_out_toplevel(self, outtype, results):
117 out_submodule = __import__('files.' + outtype, None, None, outtype)
118 out_submodule.Cli_Presentation(results).toplevel
120 def show_current_status(self, current_path):
126 ### print current_path
128 def nothing_founded(self):
129 print _('Nothing founded!')
132 self.start_func(self.get_data, self.get_stopit)
134 #==============================================================================
136 class Gtk_Presentation(object):
138 def __init__(self, start_func, *unused):
140 global gtk # for show_current_status()
141 from misc import NotebookWCloseBtns
143 self.nb = NotebookWCloseBtns()
144 self.nb.notebook.set_scrollable(True)
145 self.nb.notebook.set_border_width(2)
147 #====================
149 #====================
152 self.path_label = gtk.Label(_('Path'))
154 self.path_entry = gtk.Entry()
155 self.path_entry.set_text(config['files']['start_path'])
157 self.browse_btn = gtk.Button('Browse...')
158 self.browse_btn.connect('clicked', self.browse_btn_clicked)
160 # "Files quantity" label
161 qty_label = gtk.Label(_('Files quantity'))
162 # "Files quantity" spin
163 self.qty_spin = gtk.SpinButton()
164 self.qty_spin.set_numeric(True)
165 self.qty_spin.set_range(0, 65536)
166 self.qty_spin.set_increments(1, 10)
167 self.qty_spin.set_value(config['files']['count'])
170 filter_label = gtk.Label(_('Filter (semicolon separated)'))
172 self.filter_entry = gtk.Entry()
173 self.filter_entry.set_text(config['files']['filter'])
176 out_label = gtk.Label(_('Output'))
178 btn = gtk.RadioButton(None, OUTTYPES[0][1])
179 btn.set_name(OUTTYPES[0][0])
181 self.out_rbtns.append(btn)
182 for name, label in OUTTYPES[1:]:
183 btn = gtk.RadioButton(self.out_rbtns[0], label)
185 self.out_rbtns.append(btn)
188 self.start_btn = gtk.Button(_('Start'))
189 self.start_btn.connect('released', self.start_btn_released, start_func)
191 self.stop_btn = gtk.Button(_('Stop'))
192 self.stop_btn.set_sensitive(False)
193 self.stop_btn.connect('clicked', self.stop_btn_clicked)
195 path_hbox = gtk.HBox(False, 2)
196 path_hbox.pack_start(self.path_label, False, False, 4)
197 path_hbox.pack_start(self.path_entry, True, True, 0)
198 path_hbox.pack_start(self.browse_btn, False, False, 0)
200 qty_hbox = gtk.HBox(False, 2)
201 qty_hbox.pack_start(qty_label, False, False, 4)
202 qty_hbox.pack_start(self.qty_spin, False, False, 0)
204 filter_hbox = gtk.HBox(False, 2)
205 filter_hbox.pack_start(filter_label, False, False, 4)
206 filter_hbox.pack_start(self.filter_entry, True, True, 0)
208 out_hbox = gtk.HBox(False, 2)
209 out_hbox.pack_start(out_label, False, False, 4)
210 for btn in self.out_rbtns:
211 out_hbox.pack_start(btn, False, False, 0)
212 # Activate radio button
213 if btn.get_name() == config['outtype']:
216 control_hbox = gtk.HBox(True, 2)
217 control_hbox.pack_start(self.start_btn, True, True, 0)
218 control_hbox.pack_start(self.stop_btn, True, True, 0)
220 cr_vbox = gtk.VBox(False, 2)
221 cr_vbox.set_border_width(2)
222 cr_vbox.pack_start(path_hbox, False, False, 0)
223 cr_vbox.pack_start(qty_hbox, False, False, 0)
224 cr_vbox.pack_start(filter_hbox, False, False, 0)
225 cr_vbox.pack_start(out_hbox, False, False, 0)
226 cr_vbox.pack_end(control_hbox, False, False, 0)
228 self.nb.new_tab(cr_vbox, _('Criteria'), noclose=True)
230 #====================
232 #====================
234 self.statusbar = gtk.Statusbar()
235 self.statusbar.set_has_resize_grip(False)
236 self.context_id = self.statusbar.get_context_id('Current walked file')
238 self.vbox = gtk.VBox()
239 self.vbox.pack_start(self.nb.notebook, True, True, 0)
240 self.vbox.pack_end(self.statusbar, False, False, 0)
242 # self.show_out_toplevel(config['outtype'], [(1, 'path', 'bytesize')])
244 self.toplevel = self.vbox
246 #=== Functions ============================================================
247 def browse_btn_clicked(self, btn):
248 """Open directory browser. "Browse" button clicked callback."""
249 dialog = gtk.FileChooserDialog(title=_('Choose directory'),
250 action='select-folder',
251 buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
252 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
253 path = abspath(self.path_entry.get_text())
254 dialog.set_current_folder(path)
256 response = dialog.run()
257 if response == gtk.RESPONSE_OK:
258 self.path_entry.set_text(dialog.get_filename())
261 def start_btn_released(self, btn, start_func):
262 """Start file search. Button "Go" activate callback."""
264 self.stop_btn.set_sensitive(True)
265 self.start_btn.set_sensitive(False)
266 start_func(self.get_criteria, self.get_stopit)
267 self.stop_btn.set_sensitive(False)
268 self.start_btn.set_sensitive(True)
270 def stop_btn_clicked(self, widget):
271 """Stop search. "Stop" button clicked callback."""
273 self.stop_btn.set_sensitive(False)
274 self.start_btn.set_sensitive(True)
276 def get_criteria(self):
277 """Pick search criteria from window."""
278 for btn in self.out_rbtns:
281 out['name'] = btn.get_name()
282 out['label'] = btn.get_label()
283 file_filter = self.filter_entry.get_text().split(';')
284 # If no filter - show all files
285 if file_filter == ['']:
286 file_filter = ['*.*']
288 self.path_entry.get_text(), int(self.qty_spin.get_value()), \
291 def get_stopit(self):
294 def show_current_status(self, current_path):
295 """Show current walked path in statusbar and update window."""
296 self.statusbar.push(self.context_id, current_path)
299 def nothing_founded(self):
300 self.statusbar.push(self.context_id, _('Nothing founded!'))
305 #=== Output type selecting ================================================
306 def show_out_toplevel(self, outtype, results):
307 out_submodule = __import__('files.' + outtype['name'], None, None, outtype)
308 self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
309 self.nb.new_tab(self.out_toplevel, outtype['label'])
310 ### out_submodule.Gtk_Presentation().show_results(results)
312 #==============================================================================
314 class Hildon_Presentation(object):
316 def __init__(self, start_func):