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 = eval(ui + '_Presentation(config, self.start_search)')
19 self.abstrac = Abstraction(self.config, self.present)
21 self.toplevel = self.present.toplevel
23 def start_search(self, get_data, get_stopit):
25 outtype, start_path, count = get_data()
26 search_func = self.abstrac.filegetter(start_path, get_stopit)
27 for fsize, fpath in nlargest(count, search_func):
28 filelist.append([int(fsize), fpath, size_hum_read(fsize)])
29 self.present.show_out_toplevel(None, outtype, filelist)
34 #==============================================================================
36 class Abstraction(object):
38 def __init__(self, config, presentation):
39 self.ignore_dirs = config['ignore_dirs']
40 self.presentation = presentation
42 def filegetter(self, startdir, get_stopit):
43 """Generator of file sizes and paths based on os.walk."""
45 # Walk across directory tree
46 for dirpath, dirnames, fnames in walk(startdir):
47 # Eliminate unnecessary directories
48 ignore_dirs = self.ignore_dirs
49 for ign_dir in ignore_dirs[:]:
50 for dirname in dirnames[:]:
51 if ign_dir == normcase(join(abspath(dirpath), dirname)):
52 dirnames.remove(dirname)
53 ignore_dirs.remove(ign_dir)
56 flpath = abspath(join(dirpath, fname))
57 self.presentation.show_current_status(flpath)
59 # Stop search via 'stopit' signal
65 # Query only valid files
67 # Return results (bytesize, path)
68 yield getsize(flpath), flpath
72 #==============================================================================
74 class Cli_Presentation(object):
75 def __init__(self, config, start_func):
76 self.start_func = start_func
78 self.outtype = config['outtype']
79 self.start_path = config['start_path']
80 self.count = config['count']
86 return self.outtype, self.start_path, int(self.count)
91 def show_out_toplevel(self, _, outtype, results):
92 out_submodule = __import__('files.' + outtype, None, None, outtype)
93 out_submodule.Cli_Presentation(results).toplevel
95 def show_current_status(self, current_path):
101 ### print current_path
104 self.start_func(self.get_data, self.get_stopit)
106 #==============================================================================
108 class Gtk_Presentation(object):
110 def __init__(self, config, start_func):
116 self.path_entry = gtk.Entry()
117 self.path_entry.set_text(self.config['start_path'])
119 # "Files quantity" label
120 qty_label = gtk.Label(_('Files quantity'))
122 # "Files quantity" spin
123 self.qty_spin = gtk.SpinButton()
124 self.qty_spin.set_numeric(True)
125 self.qty_spin.set_range(0, 65536)
126 self.qty_spin.set_increments(1, 10)
127 self.qty_spin.set_value(self.config['count'])
130 self.start_btn = gtk.Button(_('Start'))
131 self.start_btn.connect('released', self.start_btn_released, start_func)
134 self.stop_btn = gtk.Button(_('Stop'))
135 self.stop_btn.set_sensitive(False)
136 self.stop_btn.connect('clicked', self.stop_btn_clicked)
139 out_table_rbtn = gtk.RadioButton(None, _('Table'))
140 out_table_rbtn.set_name('out_table')
141 out_diabar_rbtn = gtk.RadioButton(out_table_rbtn, _('Bar chart'))
142 out_diabar_rbtn.set_name('out_diabar')
143 out_diapie_rbtn = gtk.RadioButton(out_table_rbtn, _('Pie chart'))
144 out_diapie_rbtn.set_name('out_diapie')
145 out_diaold_rbtn = gtk.RadioButton(out_table_rbtn, _('Old chart'))
146 out_diaold_rbtn.set_name('out_diaold')
148 out_table_rbtn, out_diabar_rbtn, out_diapie_rbtn, out_diaold_rbtn
151 hbox = gtk.HBox(False, 4)
152 hbox.pack_start(qty_label, False, False, 0)
153 hbox.pack_start(self.qty_spin, False, False, 0)
154 hbox.pack_start(self.start_btn, False, False, 0)
155 hbox.pack_start(self.stop_btn, False, False, 0)
156 for btn in reversed(self.out_rbtns):
157 hbox.pack_end(btn, False, False, 0)
158 # Activate radio button
159 if btn.get_name() == self.config['outtype']:
162 self.statusbar = gtk.Statusbar()
163 self.context_id = self.statusbar.get_context_id('Current walked file')
165 self.vbox = gtk.VBox(False, 4)
166 self.vbox.pack_start(self.path_entry, False, False, 0)
167 self.vbox.pack_start(hbox, False, False, 0)
168 self.vbox.pack_end(self.statusbar, False, False, 0)
170 self.toplevel = self.vbox
172 # For importing gtk only once (lambda not work)
173 def show_current_status(current_path):
174 self.statusbar.push(self.context_id, current_path)
176 self.show_current_status = show_current_status
178 self.show_out_toplevel(None, self.config['outtype'], [(1, 'path', 'bytesize')])
180 #=== Functions ============================================================
181 def start_btn_released(self, btn, start_func):
183 self.stop_btn.set_sensitive(True)
184 self.start_btn.set_sensitive(False)
185 start_func(self.get_data, self.get_stopit)
186 self.stop_btn.set_sensitive(False)
187 self.start_btn.set_sensitive(True)
189 def stop_btn_clicked(self, widget):
191 self.stop_btn.set_sensitive(False)
192 self.start_btn.set_sensitive(True)
195 for btn in self.out_rbtns:
198 return out, self.path_entry.get_text(), int(self.qty_spin.get_value())
200 def get_stopit(self):
206 #=== Output type selecting ================================================
207 def show_out_toplevel(self, btn, outtype, results):
208 print 'Entering <' + outtype + '> output mode...'
209 out_submodule = __import__('files.' + outtype, None, None, outtype)
212 self.out_toplevel.destroy()
216 self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
217 self.vbox.add(self.out_toplevel)
218 self.out_toplevel.show_all()
219 ### out_submodule.Gtk_Presentation().show_results(results)
221 #==============================================================================
223 class Hildon_Presentation(object):
225 def __init__(self, config, start_func):