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