3 # vim: sw=4 ts=4 expandtab ai
6 from os.path import join, abspath, normcase, isdir, getsize
7 from heapq import nlargest
9 from misc import size_hum_read, _, NotebookWCloseBtns
10 from config import config
13 ('out_table', _('Table')),
14 ('out_diabar', _('Bar chart')),
15 ('out_diapie', _('Pie chart')),
16 ('out_diaold', _('Old chart')),
19 #==============================================================================
21 class Control(object):
23 def __init__(self, ui, params):
24 self.present = eval(ui + '_Presentation(self.start_search, params)')
25 self.abstrac = Abstraction(self.present)
27 self.toplevel = self.present.toplevel
29 def start_search(self, get_criteria, get_stopit):
31 outtype, start_path, count = get_criteria()
32 search_func = self.abstrac.filegetter(start_path, get_stopit)
33 for fsize, fpath in nlargest(count, search_func):
34 filelist.append([int(fsize), fpath, size_hum_read(fsize)])
35 self.present.show_out_toplevel(outtype, filelist)
40 #==============================================================================
42 class Abstraction(object):
44 def __init__(self, presentation):
45 self.ignore_dirs = config['files']['ignore_dirs']
46 self.presentation = presentation
48 def filegetter(self, startdir, get_stopit):
49 """Generator of file sizes and paths based on os.walk."""
50 # Walk across directory tree
51 for dirpath, dirnames, fnames in walk(startdir):
52 # Eliminate unnecessary directories
53 ignore_dirs = self.ignore_dirs
54 for ign_dir in ignore_dirs[:]:
55 for dirname in dirnames[:]:
56 if ign_dir == normcase(join(abspath(dirpath), dirname)):
57 dirnames.remove(dirname)
58 ignore_dirs.remove(ign_dir)
61 flpath = abspath(join(dirpath, fname))
62 self.presentation.show_current_status(flpath)
64 # Stop search via 'stopit' signal
70 # Query only valid files
72 # Return results (bytesize, path)
73 yield getsize(flpath), flpath
77 #==============================================================================
79 class Cli_Presentation(object):
80 def __init__(self, start_func, params):
81 self.start_func = start_func
83 self.outtype = params['outtype']
84 self.start_path = params['start_path']
85 self.count = params['count']
91 return self.outtype, self.start_path, int(self.count)
96 def show_out_toplevel(self, outtype, results):
97 out_submodule = __import__('files.' + outtype, None, None, outtype)
98 out_submodule.Cli_Presentation(results).toplevel
100 def show_current_status(self, current_path):
106 ### print current_path
109 self.start_func(self.get_data, self.get_stopit)
111 #==============================================================================
113 class Gtk_Presentation(object):
115 def __init__(self, start_func, __):
117 global gtk # for show_current_status()
119 self.nb = NotebookWCloseBtns()
120 self.nb.notebook.set_scrollable(True)
121 self.nb.notebook.set_border_width(2)
123 #====================
125 #====================
128 self.path_label = gtk.Label(_('Path'))
130 self.path_entry = gtk.Entry()
131 self.path_entry.set_text(config['files']['start_path'])
133 self.browse_btn = gtk.Button('Browse...')
134 self.browse_btn.connect('clicked', self.browse_btn_clicked)
136 # "Files quantity" label
137 qty_label = gtk.Label(_('Files quantity'))
138 # "Files quantity" spin
139 self.qty_spin = gtk.SpinButton()
140 self.qty_spin.set_numeric(True)
141 self.qty_spin.set_range(0, 65536)
142 self.qty_spin.set_increments(1, 10)
143 self.qty_spin.set_value(config['files']['count'])
146 out_label = gtk.Label(_('Output'))
148 btn = gtk.RadioButton(None, OUTTYPES[0][1])
149 btn.set_name(OUTTYPES[0][0])
151 self.out_rbtns.append(btn)
152 for name, label in OUTTYPES[1:]:
153 btn = gtk.RadioButton(self.out_rbtns[0], label)
155 self.out_rbtns.append(btn)
158 self.start_btn = gtk.Button(_('Start'))
159 self.start_btn.connect('released', self.start_btn_released, start_func)
161 self.stop_btn = gtk.Button(_('Stop'))
162 self.stop_btn.set_sensitive(False)
163 self.stop_btn.connect('clicked', self.stop_btn_clicked)
165 hbox1 = gtk.HBox(False, 2)
166 hbox1.pack_start(self.path_label, False, False, 4)
167 hbox1.pack_start(self.path_entry, True, True, 0)
168 hbox1.pack_start(self.browse_btn, False, False, 0)
170 hbox2 = gtk.HBox(False, 2)
171 hbox2.pack_start(qty_label, False, False, 4)
172 hbox2.pack_start(self.qty_spin, False, False, 0)
174 hbox3 = gtk.HBox(False, 2)
175 hbox3.pack_start(out_label, False, False, 4)
176 for btn in self.out_rbtns:
177 hbox3.pack_start(btn, False, False, 0)
178 # Activate radio button
179 if btn.get_name() == config['outtype']:
182 hbox4 = gtk.HBox(True, 2)
183 hbox4.pack_start(self.start_btn, True, True, 0)
184 hbox4.pack_start(self.stop_btn, True, True, 0)
186 cr_vbox = gtk.VBox(False, 2)
187 cr_vbox.set_border_width(2)
188 cr_vbox.pack_start(hbox1, False, False, 0)
189 cr_vbox.pack_start(hbox2, False, False, 0)
190 cr_vbox.pack_start(hbox3, False, False, 0)
191 cr_vbox.pack_end(hbox4, False, False, 0)
193 self.nb.new_tab(cr_vbox, _('Criteria'), noclose=True)
195 #====================
197 #====================
199 self.statusbar = gtk.Statusbar()
200 self.statusbar.set_has_resize_grip(False)
201 self.context_id = self.statusbar.get_context_id('Current walked file')
203 self.vbox = gtk.VBox()
204 self.vbox.pack_start(self.nb.notebook, True, True, 0)
205 self.vbox.pack_end(self.statusbar, False, False, 0)
207 # self.show_out_toplevel(config['outtype'], [(1, 'path', 'bytesize')])
209 self.toplevel = self.vbox
211 #=== Functions ============================================================
212 def browse_btn_clicked(self, btn):
213 """Open directory browser. "Browse" button clicked callback."""
214 dialog = gtk.FileChooserDialog(title=_('Choose directory'),
215 action='select-folder',
216 buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
217 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
218 path = abspath(self.path_entry.get_text())
219 dialog.set_current_folder(path)
221 response = dialog.run()
222 if response == gtk.RESPONSE_OK:
223 self.path_entry.set_text(dialog.get_filename())
226 def start_btn_released(self, btn, start_func):
227 """Start file search. Button "Go" activate callback."""
229 self.stop_btn.set_sensitive(True)
230 self.start_btn.set_sensitive(False)
231 start_func(self.get_criteria, self.get_stopit)
232 self.stop_btn.set_sensitive(False)
233 self.start_btn.set_sensitive(True)
235 def stop_btn_clicked(self, widget):
236 """Stop search. "Stop" button clicked callback."""
238 self.stop_btn.set_sensitive(False)
239 self.start_btn.set_sensitive(True)
241 def get_criteria(self):
242 """Pick search criteria from window."""
243 for btn in self.out_rbtns:
246 out['name'] = btn.get_name()
247 out['label'] = btn.get_label()
248 return out, self.path_entry.get_text(), int(self.qty_spin.get_value())
250 def get_stopit(self):
253 def show_current_status(self, current_path):
254 """Show current walked path in statusbar and update window."""
255 self.statusbar.push(self.context_id, current_path)
261 #=== Output type selecting ================================================
262 def show_out_toplevel(self, outtype, results):
263 print 'Entering <' + outtype['name'] + '> output mode...'
264 out_submodule = __import__('files.' + outtype['name'], None, None, outtype)
265 self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
266 self.nb.new_tab(self.out_toplevel, outtype['label'])
267 ### out_submodule.Gtk_Presentation().show_results(results)
269 #==============================================================================
271 class Hildon_Presentation(object):
273 def __init__(self, start_func):