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