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
10 from misc import size_hum_read, _, NotebookWCloseBtns
11 from config import config
14 ('out_table', _('Table')),
15 ('out_diabar', _('Bar chart')),
16 ('out_diapie', _('Pie chart')),
17 ('out_diaold', _('Old chart')),
20 #==============================================================================
22 class Control(object):
24 def __init__(self, ui, params):
25 self.present = eval(ui + '_Presentation(self.start_search, params)')
26 self.abstrac = Abstraction(self.present)
28 self.toplevel = self.present.toplevel
30 def start_search(self, get_criteria, get_stopit):
32 outtype, start_path, count, file_filter = get_criteria()
33 search_func = self.abstrac.filegetter(start_path, file_filter, get_stopit)
34 for fsize, fpath in nlargest(count, search_func):
35 filelist.append([int(fsize), fpath, size_hum_read(fsize)])
36 self.present.show_out_toplevel(outtype, filelist)
41 #==============================================================================
43 class Abstraction(object):
45 def __init__(self, presentation):
46 self.ignore_dirs = config['files']['ignore_dirs']
47 self.presentation = presentation
49 def filegetter(self, startdir, file_filter, get_stopit):
50 """Generator of file sizes and paths based on os.walk."""
51 # Walk across directory tree
52 for dirpath, dirnames, fnames in walk(startdir):
53 # Eliminate unnecessary directories
54 ignore_dirs = self.ignore_dirs
55 for ign_dir in ignore_dirs[:]:
56 for dirname in dirnames[:]:
57 if ign_dir == normcase(join(abspath(dirpath), dirname)):
58 dirnames.remove(dirname)
59 ignore_dirs.remove(ign_dir)
62 # Store only necessary files
63 for mask in file_filter:
64 if fnmatch(fname, mask):
65 flpath = abspath(join(dirpath, fname))
67 self.presentation.show_current_status(flpath)
68 # Stop search via 'stopit' signal
74 # Query only valid files
76 # Return results (bytesize, path)
77 yield getsize(flpath), flpath
81 #==============================================================================
83 class Cli_Presentation(object):
84 def __init__(self, start_func, params):
85 self.start_func = start_func
87 self.outtype = params['outtype']
88 self.start_path = params['start_path']
89 self.count = params['count']
95 return self.outtype, self.start_path, int(self.count)
100 def show_out_toplevel(self, outtype, results):
101 out_submodule = __import__('files.' + outtype, None, None, outtype)
102 out_submodule.Cli_Presentation(results).toplevel
104 def show_current_status(self, current_path):
110 ### print current_path
113 self.start_func(self.get_data, self.get_stopit)
115 #==============================================================================
117 class Gtk_Presentation(object):
119 def __init__(self, start_func, __):
121 global gtk # for show_current_status()
123 self.nb = NotebookWCloseBtns()
124 self.nb.notebook.set_scrollable(True)
125 self.nb.notebook.set_border_width(2)
127 #====================
129 #====================
132 self.path_label = gtk.Label(_('Path'))
134 self.path_entry = gtk.Entry()
135 self.path_entry.set_text(config['files']['start_path'])
137 self.browse_btn = gtk.Button('Browse...')
138 self.browse_btn.connect('clicked', self.browse_btn_clicked)
140 # "Files quantity" label
141 qty_label = gtk.Label(_('Files quantity'))
142 # "Files quantity" spin
143 self.qty_spin = gtk.SpinButton()
144 self.qty_spin.set_numeric(True)
145 self.qty_spin.set_range(0, 65536)
146 self.qty_spin.set_increments(1, 10)
147 self.qty_spin.set_value(config['files']['count'])
150 filter_label = gtk.Label(_('Filter'))
152 self.filter_entry = gtk.Entry()
153 self.filter_entry.set_text(config['files']['filter'])
156 out_label = gtk.Label(_('Output'))
158 btn = gtk.RadioButton(None, OUTTYPES[0][1])
159 btn.set_name(OUTTYPES[0][0])
161 self.out_rbtns.append(btn)
162 for name, label in OUTTYPES[1:]:
163 btn = gtk.RadioButton(self.out_rbtns[0], label)
165 self.out_rbtns.append(btn)
168 self.start_btn = gtk.Button(_('Start'))
169 self.start_btn.connect('released', self.start_btn_released, start_func)
171 self.stop_btn = gtk.Button(_('Stop'))
172 self.stop_btn.set_sensitive(False)
173 self.stop_btn.connect('clicked', self.stop_btn_clicked)
175 path_hbox = gtk.HBox(False, 2)
176 path_hbox.pack_start(self.path_label, False, False, 4)
177 path_hbox.pack_start(self.path_entry, True, True, 0)
178 path_hbox.pack_start(self.browse_btn, False, False, 0)
180 qty_hbox = gtk.HBox(False, 2)
181 qty_hbox.pack_start(qty_label, False, False, 4)
182 qty_hbox.pack_start(self.qty_spin, False, False, 0)
184 filter_hbox = gtk.HBox(False, 2)
185 filter_hbox.pack_start(filter_label, False, False, 4)
186 filter_hbox.pack_start(self.filter_entry, True, True, 0)
188 out_hbox = gtk.HBox(False, 2)
189 out_hbox.pack_start(out_label, False, False, 4)
190 for btn in self.out_rbtns:
191 out_hbox.pack_start(btn, False, False, 0)
192 # Activate radio button
193 if btn.get_name() == config['outtype']:
196 control_hbox = gtk.HBox(True, 2)
197 control_hbox.pack_start(self.start_btn, True, True, 0)
198 control_hbox.pack_start(self.stop_btn, True, True, 0)
200 cr_vbox = gtk.VBox(False, 2)
201 cr_vbox.set_border_width(2)
202 cr_vbox.pack_start(path_hbox, False, False, 0)
203 cr_vbox.pack_start(qty_hbox, False, False, 0)
204 cr_vbox.pack_start(filter_hbox, False, False, 0)
205 cr_vbox.pack_start(out_hbox, False, False, 0)
206 cr_vbox.pack_end(control_hbox, False, False, 0)
208 self.nb.new_tab(cr_vbox, _('Criteria'), noclose=True)
210 #====================
212 #====================
214 self.statusbar = gtk.Statusbar()
215 self.statusbar.set_has_resize_grip(False)
216 self.context_id = self.statusbar.get_context_id('Current walked file')
218 self.vbox = gtk.VBox()
219 self.vbox.pack_start(self.nb.notebook, True, True, 0)
220 self.vbox.pack_end(self.statusbar, False, False, 0)
222 # self.show_out_toplevel(config['outtype'], [(1, 'path', 'bytesize')])
224 self.toplevel = self.vbox
226 #=== Functions ============================================================
227 def browse_btn_clicked(self, btn):
228 """Open directory browser. "Browse" button clicked callback."""
229 dialog = gtk.FileChooserDialog(title=_('Choose directory'),
230 action='select-folder',
231 buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
232 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
233 path = abspath(self.path_entry.get_text())
234 dialog.set_current_folder(path)
236 response = dialog.run()
237 if response == gtk.RESPONSE_OK:
238 self.path_entry.set_text(dialog.get_filename())
241 def start_btn_released(self, btn, start_func):
242 """Start file search. Button "Go" activate callback."""
244 self.stop_btn.set_sensitive(True)
245 self.start_btn.set_sensitive(False)
246 start_func(self.get_criteria, self.get_stopit)
247 self.stop_btn.set_sensitive(False)
248 self.start_btn.set_sensitive(True)
250 def stop_btn_clicked(self, widget):
251 """Stop search. "Stop" button clicked callback."""
253 self.stop_btn.set_sensitive(False)
254 self.start_btn.set_sensitive(True)
256 def get_criteria(self):
257 """Pick search criteria from window."""
258 for btn in self.out_rbtns:
261 out['name'] = btn.get_name()
262 out['label'] = btn.get_label()
263 file_filter = self.filter_entry.get_text().split(';')
264 # If no filter - show all files
265 if file_filter == ['']:
266 file_filter = ['*.*']
268 self.path_entry.get_text(), int(self.qty_spin.get_value()), \
271 def get_stopit(self):
274 def show_current_status(self, current_path):
275 """Show current walked path in statusbar and update window."""
276 self.statusbar.push(self.context_id, current_path)
282 #=== Output type selecting ================================================
283 def show_out_toplevel(self, outtype, results):
284 print 'Entering <' + outtype['name'] + '> output mode...'
285 out_submodule = __import__('files.' + outtype['name'], None, None, outtype)
286 self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
287 self.nb.new_tab(self.out_toplevel, outtype['label'])
288 ### out_submodule.Gtk_Presentation().show_results(results)
290 #==============================================================================
292 class Hildon_Presentation(object):
294 def __init__(self, start_func):