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, 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()
122 from misc import NotebookWCloseBtns
124 self.nb = NotebookWCloseBtns()
125 self.nb.notebook.set_scrollable(True)
126 self.nb.notebook.set_border_width(2)
128 #====================
130 #====================
133 self.path_label = gtk.Label(_('Path'))
135 self.path_entry = gtk.Entry()
136 self.path_entry.set_text(config['files']['start_path'])
138 self.browse_btn = gtk.Button('Browse...')
139 self.browse_btn.connect('clicked', self.browse_btn_clicked)
141 # "Files quantity" label
142 qty_label = gtk.Label(_('Files quantity'))
143 # "Files quantity" spin
144 self.qty_spin = gtk.SpinButton()
145 self.qty_spin.set_numeric(True)
146 self.qty_spin.set_range(0, 65536)
147 self.qty_spin.set_increments(1, 10)
148 self.qty_spin.set_value(config['files']['count'])
151 filter_label = gtk.Label(_('Filter'))
153 self.filter_entry = gtk.Entry()
154 self.filter_entry.set_text(config['files']['filter'])
157 out_label = gtk.Label(_('Output'))
159 btn = gtk.RadioButton(None, OUTTYPES[0][1])
160 btn.set_name(OUTTYPES[0][0])
162 self.out_rbtns.append(btn)
163 for name, label in OUTTYPES[1:]:
164 btn = gtk.RadioButton(self.out_rbtns[0], label)
166 self.out_rbtns.append(btn)
169 self.start_btn = gtk.Button(_('Start'))
170 self.start_btn.connect('released', self.start_btn_released, start_func)
172 self.stop_btn = gtk.Button(_('Stop'))
173 self.stop_btn.set_sensitive(False)
174 self.stop_btn.connect('clicked', self.stop_btn_clicked)
176 path_hbox = gtk.HBox(False, 2)
177 path_hbox.pack_start(self.path_label, False, False, 4)
178 path_hbox.pack_start(self.path_entry, True, True, 0)
179 path_hbox.pack_start(self.browse_btn, False, False, 0)
181 qty_hbox = gtk.HBox(False, 2)
182 qty_hbox.pack_start(qty_label, False, False, 4)
183 qty_hbox.pack_start(self.qty_spin, False, False, 0)
185 filter_hbox = gtk.HBox(False, 2)
186 filter_hbox.pack_start(filter_label, False, False, 4)
187 filter_hbox.pack_start(self.filter_entry, True, True, 0)
189 out_hbox = gtk.HBox(False, 2)
190 out_hbox.pack_start(out_label, False, False, 4)
191 for btn in self.out_rbtns:
192 out_hbox.pack_start(btn, False, False, 0)
193 # Activate radio button
194 if btn.get_name() == config['outtype']:
197 control_hbox = gtk.HBox(True, 2)
198 control_hbox.pack_start(self.start_btn, True, True, 0)
199 control_hbox.pack_start(self.stop_btn, True, True, 0)
201 cr_vbox = gtk.VBox(False, 2)
202 cr_vbox.set_border_width(2)
203 cr_vbox.pack_start(path_hbox, False, False, 0)
204 cr_vbox.pack_start(qty_hbox, False, False, 0)
205 cr_vbox.pack_start(filter_hbox, False, False, 0)
206 cr_vbox.pack_start(out_hbox, False, False, 0)
207 cr_vbox.pack_end(control_hbox, False, False, 0)
209 self.nb.new_tab(cr_vbox, _('Criteria'), noclose=True)
211 #====================
213 #====================
215 self.statusbar = gtk.Statusbar()
216 self.statusbar.set_has_resize_grip(False)
217 self.context_id = self.statusbar.get_context_id('Current walked file')
219 self.vbox = gtk.VBox()
220 self.vbox.pack_start(self.nb.notebook, True, True, 0)
221 self.vbox.pack_end(self.statusbar, False, False, 0)
223 # self.show_out_toplevel(config['outtype'], [(1, 'path', 'bytesize')])
225 self.toplevel = self.vbox
227 #=== Functions ============================================================
228 def browse_btn_clicked(self, btn):
229 """Open directory browser. "Browse" button clicked callback."""
230 dialog = gtk.FileChooserDialog(title=_('Choose directory'),
231 action='select-folder',
232 buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
233 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
234 path = abspath(self.path_entry.get_text())
235 dialog.set_current_folder(path)
237 response = dialog.run()
238 if response == gtk.RESPONSE_OK:
239 self.path_entry.set_text(dialog.get_filename())
242 def start_btn_released(self, btn, start_func):
243 """Start file search. Button "Go" activate callback."""
245 self.stop_btn.set_sensitive(True)
246 self.start_btn.set_sensitive(False)
247 start_func(self.get_criteria, self.get_stopit)
248 self.stop_btn.set_sensitive(False)
249 self.start_btn.set_sensitive(True)
251 def stop_btn_clicked(self, widget):
252 """Stop search. "Stop" button clicked callback."""
254 self.stop_btn.set_sensitive(False)
255 self.start_btn.set_sensitive(True)
257 def get_criteria(self):
258 """Pick search criteria from window."""
259 for btn in self.out_rbtns:
262 out['name'] = btn.get_name()
263 out['label'] = btn.get_label()
264 file_filter = self.filter_entry.get_text().split(';')
265 # If no filter - show all files
266 if file_filter == ['']:
267 file_filter = ['*.*']
269 self.path_entry.get_text(), int(self.qty_spin.get_value()), \
272 def get_stopit(self):
275 def show_current_status(self, current_path):
276 """Show current walked path in statusbar and update window."""
277 self.statusbar.push(self.context_id, current_path)
283 #=== Output type selecting ================================================
284 def show_out_toplevel(self, outtype, results):
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):