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, _
10 from config import config
12 #==============================================================================
14 class Control(object):
16 def __init__(self, ui):
19 self.present = Cli_Presentation()
21 self.present = Gtk_Presentation(self.start_search)
23 self.present = Hildon_Presentation(self.start_search)
25 # self.present - for updating windows in interactive presentations
26 self.abstrac = Abstraction(self.present)
28 # Used only in non-interactive presentations
29 self.present.start_search(self.start_search)
31 def start_search(self, get_data, get_stopit):
33 outtype, start_path, count = get_data()
34 search_func = self.abstrac.filegetter(start_path, get_stopit)
35 for fsize, fpath in nlargest(count, search_func):
36 filelist.append([int(fsize), fpath, size_hum_read(fsize)])
37 self.present.show_out_toplevel(None, outtype, filelist)
40 return self.present.toplevel
42 #==============================================================================
44 class Abstraction(object):
46 def __init__(self, presentation):
47 self.ignore_dirs = config['files']['ignore_dirs']
48 self.presentation = presentation
50 def filegetter(self, startdir, get_stopit):
51 """Generator of file sizes and paths based on os.walk."""
53 # Walk across directory tree
54 for dirpath, dirnames, fnames in walk(startdir):
55 # Eliminate unnecessary directories
56 ignore_dirs = self.ignore_dirs
57 for ign_dir in ignore_dirs[:]:
58 for dirname in dirnames[:]:
59 if ign_dir == normcase(join(abspath(dirpath), dirname)):
60 dirnames.remove(dirname)
61 ignore_dirs.remove(ign_dir)
64 flpath = abspath(join(dirpath, fname))
65 self.presentation.show_current_status(flpath)
67 # Stop search via 'stopit' signal
73 # Query only valid files
75 # Return results (bytesize, path)
76 yield getsize(flpath), flpath
80 #==============================================================================
82 class Cli_Presentation(object):
84 self.outtype = config['files']['outtype']
85 self.start_path = config['files']['start_path']
86 self.count = config['files']['count']
92 return self.outtype, self.start_path, int(self.count)
97 def show_out_toplevel(self, _, outtype, results):
98 out_submodule = __import__('files.' + outtype, None, None, outtype)
99 out_submodule.Cli_Presentation(results).toplevel
101 def show_current_status(self, current_path):
105 def start_search(self, start_func):
106 start_func(self.get_data, self.get_stopit)
108 #==============================================================================
110 class Gtk_Presentation(object):
112 def __init__(self, start_func):
116 self.path_entry = gtk.Entry()
117 self.path_entry.set_text(config['files']['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(config['files']['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 outtable_rbtn = gtk.RadioButton(None, _('Table'))
140 outtable_rbtn.set_name('outtable')
141 outdiagram_rbtn = gtk.RadioButton(outtable_rbtn, _('Diagram'))
142 outdiagram_rbtn.set_name('outdiagram')
143 out1_rbtn = gtk.RadioButton(outtable_rbtn, 'Another 1')
144 out1_rbtn.set_name('outanother1')
145 self.out_rbtns = [outtable_rbtn, outdiagram_rbtn, out1_rbtn]
147 hbox = gtk.HBox(False, 4)
148 hbox.pack_start(qty_label, False, False, 0)
149 hbox.pack_start(self.qty_spin, False, False, 0)
150 hbox.pack_start(self.start_btn, False, False, 0)
151 hbox.pack_start(self.stop_btn, False, False, 0)
152 for btn in reversed(self.out_rbtns):
153 hbox.pack_end(btn, False, False, 0)
154 # Activate radio button
155 if btn.get_name() == config['outtype']:
158 self.statusbar = gtk.Statusbar()
159 self.context_id = self.statusbar.get_context_id('Current walked file')
161 self.vbox = gtk.VBox(False, 4)
162 self.vbox.pack_start(self.path_entry, False, False, 0)
163 self.vbox.pack_start(hbox, False, False, 0)
164 self.vbox.pack_end(self.statusbar, False, False, 0)
166 self.toplevel = self.vbox
168 # For importing gtk only once (lambda not work)
169 def show_current_status(current_path):
170 self.statusbar.push(self.context_id, current_path)
172 self.show_current_status = show_current_status
174 self.show_out_toplevel(None, config['outtype'], [(1, 'path', 'bytesize')])
176 #=== Functions ============================================================
177 def start_btn_released(self, btn, start_func):
179 self.stop_btn.set_sensitive(True)
180 self.start_btn.set_sensitive(False)
181 start_func(self.get_data, self.get_stopit)
182 self.stop_btn.set_sensitive(False)
183 self.start_btn.set_sensitive(True)
185 def stop_btn_clicked(self, widget):
187 self.stop_btn.set_sensitive(False)
188 self.start_btn.set_sensitive(True)
191 for btn in self.out_rbtns:
194 return out, self.path_entry.get_text(), int(self.qty_spin.get_value())
196 def get_stopit(self):
199 # Empty because search start by button
200 def start_search(self, start_func):
203 #=== Output type selecting ================================================
204 def show_out_toplevel(self, btn, outtype, results):
205 print 'Entering <' + outtype + '> output mode...'
206 out_submodule = __import__('files.' + outtype, None, None, outtype)
209 self.out_toplevel.destroy()
213 self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
214 self.vbox.add(self.out_toplevel)
215 self.out_toplevel.show_all()
216 ### out_submodule.Gtk_Presentation().show_results(results)
218 #==============================================================================
220 class Hildon_Presentation(object):
222 def __init__(self, start_func):
226 def start_search(self, start_func):