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)
35 for fsize, fpath in nlargest(count, search_func):
36 filelist.append([int(fsize), fpath, size_hum_read(fsize)])
38 results = [filelist, start_path]
39 self.present.show_out_toplevel(outtype, results)
44 #==============================================================================
46 class Abstraction(object):
48 def __init__(self, presentation):
49 self.ignore_dirs = config['files']['ignore_dirs']
50 self.presentation = presentation
52 def filegetter(self, startdir, file_filter, get_stopit):
53 """Generator of file sizes and paths based on os.walk."""
54 # Walk across directory tree
55 for dirpath, dirnames, fnames in walk(startdir):
56 # Eliminate unnecessary directories
57 ignore_dirs = self.ignore_dirs
58 for ign_dir in ignore_dirs[:]:
59 for dirname in dirnames[:]:
60 if ign_dir == normcase(join(abspath(dirpath), dirname)):
61 dirnames.remove(dirname)
62 ignore_dirs.remove(ign_dir)
65 # Store only necessary files
66 for mask in file_filter:
67 if fnmatch(fname, mask):
68 # Crutch for non-unicode names
69 #flpath = unicode(join(dirpath, fname), '1251')
70 flpath = join(dirpath, fname)
72 self.presentation.show_current_status(flpath)
73 # Stop search via 'stopit' signal
79 # Query only valid files
81 # Return results (bytesize, path)
82 yield getsize(flpath), flpath
86 #==============================================================================
88 class Cli_Presentation(object):
89 def __init__(self, start_func, params):
90 self.start_func = start_func
92 self.outtype = params['outtype']
93 self.start_path = params['start_path']
94 self.count = params['count']
95 self.file_filter = params['file_filter'].split(';')
101 return self.outtype, self.start_path, int(self.count), self.file_filter
103 def get_stopit(self):
106 def show_out_toplevel(self, outtype, results):
107 out_submodule = __import__('files.' + outtype, None, None, outtype)
108 out_submodule.Cli_Presentation(results).toplevel
110 def show_current_status(self, current_path):
116 ### print current_path
119 self.start_func(self.get_data, self.get_stopit)
121 #==============================================================================
123 class Gtk_Presentation(object):
125 def __init__(self, start_func, __):
127 global gtk # for show_current_status()
128 from misc import NotebookWCloseBtns
130 self.nb = NotebookWCloseBtns()
131 self.nb.notebook.set_scrollable(True)
132 self.nb.notebook.set_border_width(2)
134 #====================
136 #====================
139 self.path_label = gtk.Label(_('Path'))
141 self.path_entry = gtk.Entry()
142 self.path_entry.set_text(config['files']['start_path'])
144 self.browse_btn = gtk.Button('Browse...')
145 self.browse_btn.connect('clicked', self.browse_btn_clicked)
147 # "Files quantity" label
148 qty_label = gtk.Label(_('Files quantity'))
149 # "Files quantity" spin
150 self.qty_spin = gtk.SpinButton()
151 self.qty_spin.set_numeric(True)
152 self.qty_spin.set_range(0, 65536)
153 self.qty_spin.set_increments(1, 10)
154 self.qty_spin.set_value(config['files']['count'])
157 filter_label = gtk.Label(_('Filter (semicolon separated)'))
159 self.filter_entry = gtk.Entry()
160 self.filter_entry.set_text(config['files']['filter'])
163 out_label = gtk.Label(_('Output'))
165 btn = gtk.RadioButton(None, OUTTYPES[0][1])
166 btn.set_name(OUTTYPES[0][0])
168 self.out_rbtns.append(btn)
169 for name, label in OUTTYPES[1:]:
170 btn = gtk.RadioButton(self.out_rbtns[0], label)
172 self.out_rbtns.append(btn)
175 self.start_btn = gtk.Button(_('Start'))
176 self.start_btn.connect('released', self.start_btn_released, start_func)
178 self.stop_btn = gtk.Button(_('Stop'))
179 self.stop_btn.set_sensitive(False)
180 self.stop_btn.connect('clicked', self.stop_btn_clicked)
182 path_hbox = gtk.HBox(False, 2)
183 path_hbox.pack_start(self.path_label, False, False, 4)
184 path_hbox.pack_start(self.path_entry, True, True, 0)
185 path_hbox.pack_start(self.browse_btn, False, False, 0)
187 qty_hbox = gtk.HBox(False, 2)
188 qty_hbox.pack_start(qty_label, False, False, 4)
189 qty_hbox.pack_start(self.qty_spin, False, False, 0)
191 filter_hbox = gtk.HBox(False, 2)
192 filter_hbox.pack_start(filter_label, False, False, 4)
193 filter_hbox.pack_start(self.filter_entry, True, True, 0)
195 out_hbox = gtk.HBox(False, 2)
196 out_hbox.pack_start(out_label, False, False, 4)
197 for btn in self.out_rbtns:
198 out_hbox.pack_start(btn, False, False, 0)
199 # Activate radio button
200 if btn.get_name() == config['outtype']:
203 control_hbox = gtk.HBox(True, 2)
204 control_hbox.pack_start(self.start_btn, True, True, 0)
205 control_hbox.pack_start(self.stop_btn, True, True, 0)
207 cr_vbox = gtk.VBox(False, 2)
208 cr_vbox.set_border_width(2)
209 cr_vbox.pack_start(path_hbox, False, False, 0)
210 cr_vbox.pack_start(qty_hbox, False, False, 0)
211 cr_vbox.pack_start(filter_hbox, False, False, 0)
212 cr_vbox.pack_start(out_hbox, False, False, 0)
213 cr_vbox.pack_end(control_hbox, False, False, 0)
215 self.nb.new_tab(cr_vbox, _('Criteria'), noclose=True)
217 #====================
219 #====================
221 self.statusbar = gtk.Statusbar()
222 self.statusbar.set_has_resize_grip(False)
223 self.context_id = self.statusbar.get_context_id('Current walked file')
225 self.vbox = gtk.VBox()
226 self.vbox.pack_start(self.nb.notebook, True, True, 0)
227 self.vbox.pack_end(self.statusbar, False, False, 0)
229 # self.show_out_toplevel(config['outtype'], [(1, 'path', 'bytesize')])
231 self.toplevel = self.vbox
233 #=== Functions ============================================================
234 def browse_btn_clicked(self, btn):
235 """Open directory browser. "Browse" button clicked callback."""
236 dialog = gtk.FileChooserDialog(title=_('Choose directory'),
237 action='select-folder',
238 buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
239 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
240 path = abspath(self.path_entry.get_text())
241 dialog.set_current_folder(path)
243 response = dialog.run()
244 if response == gtk.RESPONSE_OK:
245 self.path_entry.set_text(dialog.get_filename())
248 def start_btn_released(self, btn, start_func):
249 """Start file search. Button "Go" activate callback."""
251 self.stop_btn.set_sensitive(True)
252 self.start_btn.set_sensitive(False)
253 start_func(self.get_criteria, self.get_stopit)
254 self.stop_btn.set_sensitive(False)
255 self.start_btn.set_sensitive(True)
257 def stop_btn_clicked(self, widget):
258 """Stop search. "Stop" button clicked callback."""
260 self.stop_btn.set_sensitive(False)
261 self.start_btn.set_sensitive(True)
263 def get_criteria(self):
264 """Pick search criteria from window."""
265 for btn in self.out_rbtns:
268 out['name'] = btn.get_name()
269 out['label'] = btn.get_label()
270 file_filter = self.filter_entry.get_text().split(';')
271 # If no filter - show all files
272 if file_filter == ['']:
273 file_filter = ['*.*']
275 self.path_entry.get_text(), int(self.qty_spin.get_value()), \
278 def get_stopit(self):
281 def show_current_status(self, current_path):
282 """Show current walked path in statusbar and update window."""
283 self.statusbar.push(self.context_id, current_path)
289 #=== Output type selecting ================================================
290 def show_out_toplevel(self, outtype, results):
291 out_submodule = __import__('files.' + outtype['name'], None, None, outtype)
292 self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
293 self.nb.new_tab(self.out_toplevel, outtype['label'])
294 ### out_submodule.Gtk_Presentation().show_results(results)
296 #==============================================================================
298 class Hildon_Presentation(object):
300 def __init__(self, start_func):