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