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, _
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, outtype, params):
25 self.present = eval(ui + '_Presentation(self.start_search, outtype, 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)
35 for fsize, fpath in nlargest(count, search_func):
36 filelist.append([int(fsize), fpath, size_hum_read(fsize)])
39 self.present.nothing_founded()
41 results = [filelist, start_path]
42 self.present.show_out_toplevel(outtype, results)
47 #==============================================================================
49 class Abstraction(object):
51 def __init__(self, presentation):
52 self.ignore_dirs = config['files']['ignore_dirs']
53 self.presentation = presentation
55 def filegetter(self, startdir, file_filter, get_stopit):
56 """Generator of file sizes and paths based on os.walk."""
57 # Walk across directory tree
58 for dirpath, dirnames, fnames in walk(startdir):
59 # Eliminate unnecessary directories
60 ignore_dirs = self.ignore_dirs
61 for ign_dir in ignore_dirs[:]:
62 for dirname in dirnames[:]:
63 if ign_dir == normcase(join(abspath(dirpath), dirname)):
64 dirnames.remove(dirname)
65 ignore_dirs.remove(ign_dir)
68 # Store only necessary files
69 for mask in file_filter:
70 if fnmatch(fname, mask):
71 # Crutch for non-unicode names
72 #flpath = unicode(join(dirpath, fname), '1251')
73 flpath = join(dirpath, fname)
75 self.presentation.show_current_status(flpath)
76 # Stop search via 'stopit' signal
82 # Query only valid files
84 # Return results (bytesize, path)
85 yield getsize(flpath), flpath
89 #==============================================================================
91 class Cli_Presentation(object):
92 def __init__(self, start_func, outtype, params):
93 self.start_func = start_func
95 self.outtype = outtype
96 self.start_path = params[0]
97 self.count = params[1]
99 self.file_filter = params[2].split(';')
101 self.file_filter = '*.*'
108 return self.outtype, self.start_path, int(self.count), self.file_filter
110 def get_stopit(self):
113 def show_out_toplevel(self, outtype, results):
114 out_submodule = __import__('files.' + outtype, None, None, outtype)
115 out_submodule.Cli_Presentation(results).toplevel
117 def show_current_status(self, current_path):
123 ### print current_path
125 def nothing_founded(self):
126 print _('Nothing founded!')
129 self.start_func(self.get_data, self.get_stopit)
131 #==============================================================================
133 class Gtk_Presentation(object):
135 def __init__(self, start_func, *unused):
137 global gtk # for show_current_status()
138 from misc import NotebookWCloseBtns
140 self.nb = NotebookWCloseBtns()
141 self.nb.notebook.set_scrollable(True)
142 self.nb.notebook.set_border_width(2)
144 #====================
146 #====================
149 self.path_label = gtk.Label(_('Path'))
151 self.path_entry = gtk.Entry()
152 self.path_entry.set_text(config['files']['start_path'])
154 self.browse_btn = gtk.Button('Browse...')
155 self.browse_btn.connect('clicked', self.browse_btn_clicked)
157 # "Files quantity" label
158 qty_label = gtk.Label(_('Files quantity'))
159 # "Files quantity" spin
160 self.qty_spin = gtk.SpinButton()
161 self.qty_spin.set_numeric(True)
162 self.qty_spin.set_range(0, 65536)
163 self.qty_spin.set_increments(1, 10)
164 self.qty_spin.set_value(config['files']['count'])
167 filter_label = gtk.Label(_('Filter (semicolon separated)'))
169 self.filter_entry = gtk.Entry()
170 self.filter_entry.set_text(config['files']['filter'])
173 out_label = gtk.Label(_('Output'))
175 btn = gtk.RadioButton(None, OUTTYPES[0][1])
176 btn.set_name(OUTTYPES[0][0])
178 self.out_rbtns.append(btn)
179 for name, label in OUTTYPES[1:]:
180 btn = gtk.RadioButton(self.out_rbtns[0], label)
182 self.out_rbtns.append(btn)
185 self.start_btn = gtk.Button(_('Start'))
186 self.start_btn.connect('released', self.start_btn_released, start_func)
188 self.stop_btn = gtk.Button(_('Stop'))
189 self.stop_btn.set_sensitive(False)
190 self.stop_btn.connect('clicked', self.stop_btn_clicked)
192 path_hbox = gtk.HBox(False, 2)
193 path_hbox.pack_start(self.path_label, False, False, 4)
194 path_hbox.pack_start(self.path_entry, True, True, 0)
195 path_hbox.pack_start(self.browse_btn, False, False, 0)
197 qty_hbox = gtk.HBox(False, 2)
198 qty_hbox.pack_start(qty_label, False, False, 4)
199 qty_hbox.pack_start(self.qty_spin, False, False, 0)
201 filter_hbox = gtk.HBox(False, 2)
202 filter_hbox.pack_start(filter_label, False, False, 4)
203 filter_hbox.pack_start(self.filter_entry, True, True, 0)
205 out_hbox = gtk.HBox(False, 2)
206 out_hbox.pack_start(out_label, False, False, 4)
207 for btn in self.out_rbtns:
208 out_hbox.pack_start(btn, False, False, 0)
209 # Activate radio button
210 if btn.get_name() == config['outtype']:
213 control_hbox = gtk.HBox(True, 2)
214 control_hbox.pack_start(self.start_btn, True, True, 0)
215 control_hbox.pack_start(self.stop_btn, True, True, 0)
217 cr_vbox = gtk.VBox(False, 2)
218 cr_vbox.set_border_width(2)
219 cr_vbox.pack_start(path_hbox, False, False, 0)
220 cr_vbox.pack_start(qty_hbox, False, False, 0)
221 cr_vbox.pack_start(filter_hbox, False, False, 0)
222 cr_vbox.pack_start(out_hbox, False, False, 0)
223 cr_vbox.pack_end(control_hbox, False, False, 0)
225 self.nb.new_tab(cr_vbox, _('Criteria'), noclose=True)
227 #====================
229 #====================
231 self.statusbar = gtk.Statusbar()
232 self.statusbar.set_has_resize_grip(False)
233 self.context_id = self.statusbar.get_context_id('Current walked file')
235 self.vbox = gtk.VBox()
236 self.vbox.pack_start(self.nb.notebook, True, True, 0)
237 self.vbox.pack_end(self.statusbar, False, False, 0)
239 # self.show_out_toplevel(config['outtype'], [(1, 'path', 'bytesize')])
241 self.toplevel = self.vbox
243 #=== Functions ============================================================
244 def browse_btn_clicked(self, btn):
245 """Open directory browser. "Browse" button clicked callback."""
246 dialog = gtk.FileChooserDialog(title=_('Choose directory'),
247 action='select-folder',
248 buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
249 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
250 path = abspath(self.path_entry.get_text())
251 dialog.set_current_folder(path)
253 response = dialog.run()
254 if response == gtk.RESPONSE_OK:
255 self.path_entry.set_text(dialog.get_filename())
258 def start_btn_released(self, btn, start_func):
259 """Start file search. Button "Go" activate callback."""
261 self.stop_btn.set_sensitive(True)
262 self.start_btn.set_sensitive(False)
263 start_func(self.get_criteria, self.get_stopit)
264 self.stop_btn.set_sensitive(False)
265 self.start_btn.set_sensitive(True)
267 def stop_btn_clicked(self, widget):
268 """Stop search. "Stop" button clicked callback."""
270 self.stop_btn.set_sensitive(False)
271 self.start_btn.set_sensitive(True)
273 def get_criteria(self):
274 """Pick search criteria from window."""
275 for btn in self.out_rbtns:
278 out['name'] = btn.get_name()
279 out['label'] = btn.get_label()
280 file_filter = self.filter_entry.get_text().split(';')
281 # If no filter - show all files
282 if file_filter == ['']:
283 file_filter = ['*.*']
285 self.path_entry.get_text(), int(self.qty_spin.get_value()), \
288 def get_stopit(self):
291 def show_current_status(self, current_path):
292 """Show current walked path in statusbar and update window."""
293 self.statusbar.push(self.context_id, current_path)
296 def nothing_founded(self):
297 self.statusbar.push(self.context_id, _('Nothing founded!'))
302 #=== Output type selecting ================================================
303 def show_out_toplevel(self, outtype, results):
304 out_submodule = __import__('files.' + outtype['name'], None, None, outtype)
305 self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
306 self.nb.new_tab(self.out_toplevel, outtype['label'])
307 ### out_submodule.Gtk_Presentation().show_results(results)
309 #==============================================================================
311 class Hildon_Presentation(object):
313 def __init__(self, start_func):