3 # vim: sw=4 ts=4 expandtab ai
6 from os.path import join, abspath, normcase, basename, isdir, getsize
7 from heapq import nlargest
9 from misc import size_hum_read, _
11 #==============================================================================
13 class Control(object):
15 def __init__(self, ui, config):
18 self.present = Cli_Presentation(config)
20 self.present = Gtk_Presentation(config, self.start_search)
22 self.present = Hildon_Presentation(config, self.start_search)
24 # self.present - for updating windows in interactive presentations
25 self.abstrac = Abstraction(config, self.present)
27 # Used only in non-interactive presentations
28 self.present.start_search(self.start_search)
30 def start_search(self, get_data, get_stopit):
32 outtype, start_path, count = get_data()
33 search_func = self.abstrac.filegetter(start_path, 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(None, outtype, filelist)
39 return self.present.toplevel
41 #==============================================================================
43 class Abstraction(object):
45 def __init__(self, config, presentation):
46 self.ignore_dirs = config['ignore_dirs']
47 self.presentation = presentation
49 def filegetter(self, startdir, get_stopit):
50 """Generator of file sizes and paths based on os.walk."""
52 # Walk across directory tree
53 for dirpath, dirnames, fnames in walk(startdir):
54 # Eliminate unnecessary directories
55 ignore_dirs = self.ignore_dirs
56 for ign_dir in ignore_dirs[:]:
57 for dirname in dirnames[:]:
58 if ign_dir == normcase(join(abspath(dirpath), dirname)):
59 dirnames.remove(dirname)
60 ignore_dirs.remove(ign_dir)
63 flpath = abspath(join(dirpath, fname))
64 self.presentation.show_current_status(flpath)
66 # Stop search via 'stopit' signal
72 # Query only valid files
74 # Return results (bytesize, path)
75 yield getsize(flpath), flpath
79 #==============================================================================
81 class Cli_Presentation(object):
82 def __init__(self, config):
83 self.outtype = config['outtype']
84 self.start_path = config['start_path']
85 self.count = config['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):
104 def start_search(self, start_func):
105 start_func(self.get_data, self.get_stopit)
107 #==============================================================================
109 class Gtk_Presentation(object):
111 def __init__(self, config, start_func):
117 self.path_entry = gtk.Entry()
118 self.path_entry.set_text(self.config['start_path'])
120 # "Files quantity" label
121 qty_label = gtk.Label(_('Files quantity'))
123 # "Files quantity" spin
124 self.qty_spin = gtk.SpinButton()
125 self.qty_spin.set_numeric(True)
126 self.qty_spin.set_range(0, 65536)
127 self.qty_spin.set_increments(1, 10)
128 self.qty_spin.set_value(self.config['count'])
131 self.start_btn = gtk.Button(_('Start'))
132 self.start_btn.connect('released', self.start_btn_released, start_func)
135 self.stop_btn = gtk.Button(_('Stop'))
136 self.stop_btn.set_sensitive(False)
137 self.stop_btn.connect('clicked', self.stop_btn_clicked)
140 outtable_rbtn = gtk.RadioButton(None, _('Table'))
141 outtable_rbtn.set_name('outtable')
142 outdiagram_rbtn = gtk.RadioButton(outtable_rbtn, _('Diagram'))
143 outdiagram_rbtn.set_name('outdiagram')
144 out1_rbtn = gtk.RadioButton(outtable_rbtn, 'Another 1')
145 out1_rbtn.set_name('outanother1')
146 self.out_rbtns = [outtable_rbtn, outdiagram_rbtn, out1_rbtn]
148 hbox = gtk.HBox(False, 4)
149 hbox.pack_start(qty_label, False, False, 0)
150 hbox.pack_start(self.qty_spin, False, False, 0)
151 hbox.pack_start(self.start_btn, False, False, 0)
152 hbox.pack_start(self.stop_btn, False, False, 0)
153 for btn in reversed(self.out_rbtns):
154 hbox.pack_end(btn, False, False, 0)
155 # Activate radio button
156 if btn.get_name() == self.config['outtype']:
159 self.statusbar = gtk.Statusbar()
160 self.context_id = self.statusbar.get_context_id('Current walked file')
162 self.vbox = gtk.VBox(False, 4)
163 self.vbox.pack_start(self.path_entry, False, False, 0)
164 self.vbox.pack_start(hbox, False, False, 0)
165 self.vbox.pack_end(self.statusbar, False, False, 0)
167 self.toplevel = self.vbox
169 # For importing gtk only once (lambda not work)
170 def show_current_status(current_path):
171 self.statusbar.push(self.context_id, current_path)
173 self.show_current_status = show_current_status
175 self.show_out_toplevel(None, self.config['outtype'], [(1, 'path', 'bytesize')])
177 #=== Functions ============================================================
178 def start_btn_released(self, btn, start_func):
180 self.stop_btn.set_sensitive(True)
181 self.start_btn.set_sensitive(False)
182 start_func(self.get_data, self.get_stopit)
183 self.stop_btn.set_sensitive(False)
184 self.start_btn.set_sensitive(True)
186 def stop_btn_clicked(self, widget):
188 self.stop_btn.set_sensitive(False)
189 self.start_btn.set_sensitive(True)
192 for btn in self.out_rbtns:
195 return out, self.path_entry.get_text(), int(self.qty_spin.get_value())
197 def get_stopit(self):
200 # Empty because search start by button
201 def start_search(self, start_func):
204 #=== Output type selecting ================================================
205 def show_out_toplevel(self, btn, outtype, results):
206 print 'Entering <' + outtype + '> output mode...'
207 out_submodule = __import__('files.' + outtype, None, None, outtype)
210 self.out_toplevel.destroy()
214 self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
215 self.vbox.add(self.out_toplevel)
216 self.out_toplevel.show_all()
217 ### out_submodule.Gtk_Presentation().show_results(results)
219 #==============================================================================
221 class Hildon_Presentation(object):
223 def __init__(self, config, start_func):
229 def start_search(self, start_func):