22f22e63c1b76332ebd967d5ac9909836c27ab85
[findit] / src / files / search.py
1 #!/usr/bin/env python
2 # -*-coding: utf-8 -*-
3 # vim: sw=4 ts=4 expandtab ai
4
5 from os import walk
6 from os.path import join, abspath, normcase, isdir, getsize
7 from heapq import nlargest
8 from fnmatch import fnmatch
9
10 from misc import size_hum_read, _, NotebookWCloseBtns
11 from config import config
12
13 OUTTYPES = [
14     ('out_table',  _('Table')),
15     ('out_diabar', _('Bar chart')),
16     ('out_diapie', _('Pie chart')),
17     ('out_diaold', _('Old chart')),
18 ]
19
20 #==============================================================================
21
22 class Control(object):
23
24     def __init__(self, ui, params):
25         self.present = eval(ui + '_Presentation(self.start_search, params)')
26         self.abstrac = Abstraction(self.present)
27
28         self.toplevel = self.present.toplevel
29
30     def start_search(self, get_criteria, get_stopit):
31         filelist = []
32         outtype, start_path, count, file_filter = get_criteria()
33         search_func = self.abstrac.filegetter(start_path, file_filter, 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(outtype, filelist)
37
38     def run(self):
39         self.present.run()
40
41 #==============================================================================
42
43 class Abstraction(object):
44
45     def __init__(self, presentation):
46         self.ignore_dirs = config['files']['ignore_dirs']
47         self.presentation = presentation
48
49     def filegetter(self, startdir, file_filter, get_stopit):
50         """Generator of file sizes and paths based on os.walk."""
51         # Walk across directory tree
52         for dirpath, dirnames, fnames in walk(startdir):
53             # Eliminate unnecessary directories
54             ignore_dirs = self.ignore_dirs
55             for ign_dir in ignore_dirs[:]:
56                 for dirname in dirnames[:]:
57                     if ign_dir == normcase(join(abspath(dirpath), dirname)):
58                         dirnames.remove(dirname)
59                         ignore_dirs.remove(ign_dir)
60
61             for fname in fnames:
62                 # Store only necessary files
63                 for mask in file_filter:
64                     if fnmatch(fname, mask):
65                         flpath = abspath(join(dirpath, fname))
66                         # Show current path
67                         self.presentation.show_current_status(flpath)
68                         # Stop search via 'stopit' signal
69                         stopit = get_stopit()
70                         if stopit:
71                             stopit = False
72                             print 'Stopped'
73                             raise StopIteration
74                         # Query only valid files
75                         try:
76                             # Return results (bytesize, path)
77                             yield getsize(flpath), flpath
78                         except OSError:
79                             continue
80
81 #==============================================================================
82
83 class Cli_Presentation(object):
84     def __init__(self, start_func, params):
85         self.start_func = start_func
86
87         self.outtype = params['outtype']
88         self.start_path = params['start_path']
89         self.count = params['count']
90         self.stopit = False
91
92         self.toplevel = None
93
94     def get_data(self):
95         return self.outtype, self.start_path, int(self.count)
96
97     def get_stopit(self):
98         return False
99
100     def show_out_toplevel(self, outtype, results):
101         out_submodule = __import__('files.' + outtype, None, None, outtype)
102         out_submodule.Cli_Presentation(results).toplevel
103
104     def show_current_status(self, current_path):
105         #pass
106         print '|' + '\r',
107         print '/' + '\r',
108         print '-' + '\r',
109         print '\\' + '\r',
110         ### print current_path
111
112     def run(self):
113         self.start_func(self.get_data, self.get_stopit)
114
115 #==============================================================================
116
117 class Gtk_Presentation(object):
118
119     def __init__(self, start_func, __):
120         import gtk
121         global gtk  # for show_current_status()
122
123         self.nb = NotebookWCloseBtns()
124         self.nb.notebook.set_scrollable(True)
125         self.nb.notebook.set_border_width(2)
126
127         #====================
128         # Notebook
129         #====================
130
131         # "Start path" label
132         self.path_label = gtk.Label(_('Path'))
133         # "Start path" entry
134         self.path_entry = gtk.Entry()
135         self.path_entry.set_text(config['files']['start_path'])
136         # "Browse" button
137         self.browse_btn = gtk.Button('Browse...')
138         self.browse_btn.connect('clicked', self.browse_btn_clicked)
139
140         # "Files quantity" label
141         qty_label = gtk.Label(_('Files quantity'))
142         # "Files quantity" spin
143         self.qty_spin = gtk.SpinButton()
144         self.qty_spin.set_numeric(True)
145         self.qty_spin.set_range(0, 65536)
146         self.qty_spin.set_increments(1, 10)
147         self.qty_spin.set_value(config['files']['count'])
148
149         # "Filter" label
150         filter_label = gtk.Label(_('Filter'))
151         # "Filter" entry
152         self.filter_entry = gtk.Entry()
153         self.filter_entry.set_text(config['files']['filter'])
154
155         # "Output" label
156         out_label = gtk.Label(_('Output'))
157         # Output selection
158         btn = gtk.RadioButton(None, OUTTYPES[0][1])
159         btn.set_name(OUTTYPES[0][0])
160         self.out_rbtns = []
161         self.out_rbtns.append(btn)
162         for name, label in OUTTYPES[1:]:
163             btn = gtk.RadioButton(self.out_rbtns[0], label)
164             btn.set_name(name)
165             self.out_rbtns.append(btn)
166
167         # "Start" button
168         self.start_btn = gtk.Button(_('Start'))
169         self.start_btn.connect('released', self.start_btn_released, start_func)
170         # "Stop" button
171         self.stop_btn = gtk.Button(_('Stop'))
172         self.stop_btn.set_sensitive(False)
173         self.stop_btn.connect('clicked', self.stop_btn_clicked)
174
175         path_hbox = gtk.HBox(False, 2)
176         path_hbox.pack_start(self.path_label, False, False, 4)
177         path_hbox.pack_start(self.path_entry, True, True, 0)
178         path_hbox.pack_start(self.browse_btn, False, False, 0)
179
180         qty_hbox = gtk.HBox(False, 2)
181         qty_hbox.pack_start(qty_label, False, False, 4)
182         qty_hbox.pack_start(self.qty_spin, False, False, 0)
183
184         filter_hbox = gtk.HBox(False, 2)
185         filter_hbox.pack_start(filter_label, False, False, 4)
186         filter_hbox.pack_start(self.filter_entry, True, True, 0)
187
188         out_hbox = gtk.HBox(False, 2)
189         out_hbox.pack_start(out_label, False, False, 4)
190         for btn in self.out_rbtns:
191             out_hbox.pack_start(btn, False, False, 0)
192             # Activate radio button
193             if btn.get_name() == config['outtype']:
194                 btn.set_active(True)
195
196         control_hbox = gtk.HBox(True, 2)
197         control_hbox.pack_start(self.start_btn, True, True, 0)
198         control_hbox.pack_start(self.stop_btn, True, True, 0)
199
200         cr_vbox = gtk.VBox(False, 2)
201         cr_vbox.set_border_width(2)
202         cr_vbox.pack_start(path_hbox, False, False, 0)
203         cr_vbox.pack_start(qty_hbox, False, False, 0)
204         cr_vbox.pack_start(filter_hbox, False, False, 0)
205         cr_vbox.pack_start(out_hbox, False, False, 0)
206         cr_vbox.pack_end(control_hbox, False, False, 0)
207
208         self.nb.new_tab(cr_vbox, _('Criteria'), noclose=True)
209
210         #====================
211         # Others
212         #====================
213
214         self.statusbar = gtk.Statusbar()
215         self.statusbar.set_has_resize_grip(False)
216         self.context_id = self.statusbar.get_context_id('Current walked file')
217
218         self.vbox = gtk.VBox()
219         self.vbox.pack_start(self.nb.notebook, True, True, 0)
220         self.vbox.pack_end(self.statusbar, False, False, 0)
221
222 #        self.show_out_toplevel(config['outtype'], [(1, 'path', 'bytesize')])
223
224         self.toplevel = self.vbox
225
226     #=== Functions ============================================================
227     def browse_btn_clicked(self, btn):
228         """Open directory browser. "Browse" button clicked callback."""
229         dialog = gtk.FileChooserDialog(title=_('Choose directory'),
230                                        action='select-folder',
231                                        buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
232                                                 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
233         path = abspath(self.path_entry.get_text())
234         dialog.set_current_folder(path)
235         dialog.show_all()
236         response = dialog.run()
237         if response == gtk.RESPONSE_OK:
238             self.path_entry.set_text(dialog.get_filename())
239         dialog.destroy()
240
241     def start_btn_released(self, btn, start_func):
242         """Start file search. Button "Go" activate callback."""
243         self.stopit = False
244         self.stop_btn.set_sensitive(True)
245         self.start_btn.set_sensitive(False)
246         start_func(self.get_criteria, self.get_stopit)
247         self.stop_btn.set_sensitive(False)
248         self.start_btn.set_sensitive(True)
249
250     def stop_btn_clicked(self, widget):
251         """Stop search. "Stop" button clicked callback."""
252         self.stopit = True
253         self.stop_btn.set_sensitive(False)
254         self.start_btn.set_sensitive(True)
255
256     def get_criteria(self):
257         """Pick search criteria from window."""
258         for btn in self.out_rbtns:
259             if btn.get_active():
260                 out = {}
261                 out['name'] = btn.get_name()
262                 out['label'] = btn.get_label()
263         file_filter = self.filter_entry.get_text().split(';')
264         # If no filter - show all files
265         if file_filter == ['']:
266             file_filter = ['*.*']
267         return out, \
268             self.path_entry.get_text(), int(self.qty_spin.get_value()), \
269             file_filter
270
271     def get_stopit(self):
272         return self.stopit
273
274     def show_current_status(self, current_path):
275         """Show current walked path in statusbar and update window."""
276         self.statusbar.push(self.context_id, current_path)
277         gtk.main_iteration()
278
279     def run(self):
280         pass
281
282     #=== Output type selecting ================================================
283     def show_out_toplevel(self, outtype, results):
284         print 'Entering <' + outtype['name'] + '> output mode...'
285         out_submodule = __import__('files.' + outtype['name'], None, None, outtype)
286         self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
287         self.nb.new_tab(self.out_toplevel, outtype['label'])
288 ###        out_submodule.Gtk_Presentation().show_results(results)
289
290 #==============================================================================
291
292 class Hildon_Presentation(object):
293
294     def __init__(self, start_func):
295         import gtk
296         import hildon
297
298     def run(self):
299         pass