Updated translation
[findit] / src / files / search.py
index 24e423b..9510194 100755 (executable)
@@ -5,8 +5,9 @@
 from os import walk
 from os.path import join, abspath, normcase, isdir, getsize
 from heapq import nlargest
+from fnmatch import fnmatch
 
-from misc import size_hum_read, _, NotebookWCloseBtns
+from misc import size_hum_read, _
 from config import config
 
 OUTTYPES = [
@@ -28,11 +29,14 @@ class Control(object):
 
     def start_search(self, get_criteria, get_stopit):
         filelist = []
-        outtype, start_path, count = get_criteria()
-        search_func = self.abstrac.filegetter(start_path, get_stopit)
+        outtype, start_path, count, file_filter = get_criteria()
+        search_func = self.abstrac.filegetter(start_path, file_filter, get_stopit)
+
         for fsize, fpath in nlargest(count, search_func):
             filelist.append([int(fsize), fpath, size_hum_read(fsize)])
-        self.present.show_out_toplevel(outtype, filelist)
+
+        results = [filelist, start_path]
+        self.present.show_out_toplevel(outtype, results)
 
     def run(self):
         self.present.run()
@@ -45,7 +49,7 @@ class Abstraction(object):
         self.ignore_dirs = config['files']['ignore_dirs']
         self.presentation = presentation
 
-    def filegetter(self, startdir, get_stopit):
+    def filegetter(self, startdir, file_filter, get_stopit):
         """Generator of file sizes and paths based on os.walk."""
         # Walk across directory tree
         for dirpath, dirnames, fnames in walk(startdir):
@@ -58,21 +62,26 @@ class Abstraction(object):
                         ignore_dirs.remove(ign_dir)
 
             for fname in fnames:
-                flpath = abspath(join(dirpath, fname))
-                self.presentation.show_current_status(flpath)
-
-                # Stop search via 'stopit' signal
-                stopit = get_stopit()
-                if stopit:
-                    stopit = False
-                    print 'Stopped'
-                    raise StopIteration
-                # Query only valid files
-                try:
-                    # Return results (bytesize, path)
-                    yield getsize(flpath), flpath
-                except OSError:
-                    continue
+                # Store only necessary files
+                for mask in file_filter:
+                    if fnmatch(fname, mask):
+                        # Crutch for non-unicode names
+                        #flpath = unicode(join(dirpath, fname), '1251')
+                        flpath = join(dirpath, fname)
+                        # Show current path
+                        self.presentation.show_current_status(flpath)
+                        # Stop search via 'stopit' signal
+                        stopit = get_stopit()
+                        if stopit:
+                            stopit = False
+                            print 'Stopped'
+                            raise StopIteration
+                        # Query only valid files
+                        try:
+                            # Return results (bytesize, path)
+                            yield getsize(flpath), flpath
+                        except OSError:
+                            continue
 
 #==============================================================================
 
@@ -83,12 +92,13 @@ class Cli_Presentation(object):
         self.outtype = params['outtype']
         self.start_path = params['start_path']
         self.count = params['count']
+        self.file_filter = params['file_filter'].split(';')
         self.stopit = False
 
         self.toplevel = None
 
     def get_data(self):
-        return self.outtype, self.start_path, int(self.count)
+        return self.outtype, self.start_path, int(self.count), self.file_filter
 
     def get_stopit(self):
         return False
@@ -115,6 +125,7 @@ class Gtk_Presentation(object):
     def __init__(self, start_func, __):
         import gtk
         global gtk  # for show_current_status()
+        from misc import NotebookWCloseBtns
 
         self.nb = NotebookWCloseBtns()
         self.nb.notebook.set_scrollable(True)
@@ -142,6 +153,12 @@ class Gtk_Presentation(object):
         self.qty_spin.set_increments(1, 10)
         self.qty_spin.set_value(config['files']['count'])
 
+        # "Filter" label
+        filter_label = gtk.Label(_('Filter (semicolon separated)'))
+        # "Filter" entry
+        self.filter_entry = gtk.Entry()
+        self.filter_entry.set_text(config['files']['filter'])
+
         # "Output" label
         out_label = gtk.Label(_('Output'))
         # Output selection
@@ -162,33 +179,38 @@ class Gtk_Presentation(object):
         self.stop_btn.set_sensitive(False)
         self.stop_btn.connect('clicked', self.stop_btn_clicked)
 
-        hbox1 = gtk.HBox(False, 2)
-        hbox1.pack_start(self.path_label, False, False, 4)
-        hbox1.pack_start(self.path_entry, True, True, 0)
-        hbox1.pack_start(self.browse_btn, False, False, 0)
+        path_hbox = gtk.HBox(False, 2)
+        path_hbox.pack_start(self.path_label, False, False, 4)
+        path_hbox.pack_start(self.path_entry, True, True, 0)
+        path_hbox.pack_start(self.browse_btn, False, False, 0)
+
+        qty_hbox = gtk.HBox(False, 2)
+        qty_hbox.pack_start(qty_label, False, False, 4)
+        qty_hbox.pack_start(self.qty_spin, False, False, 0)
 
-        hbox2 = gtk.HBox(False, 2)
-        hbox2.pack_start(qty_label, False, False, 4)
-        hbox2.pack_start(self.qty_spin, False, False, 0)
+        filter_hbox = gtk.HBox(False, 2)
+        filter_hbox.pack_start(filter_label, False, False, 4)
+        filter_hbox.pack_start(self.filter_entry, True, True, 0)
 
-        hbox3 = gtk.HBox(False, 2)
-        hbox3.pack_start(out_label, False, False, 4)
+        out_hbox = gtk.HBox(False, 2)
+        out_hbox.pack_start(out_label, False, False, 4)
         for btn in self.out_rbtns:
-            hbox3.pack_start(btn, False, False, 0)
+            out_hbox.pack_start(btn, False, False, 0)
             # Activate radio button
             if btn.get_name() == config['outtype']:
                 btn.set_active(True)
 
-        hbox4 = gtk.HBox(True, 2)
-        hbox4.pack_start(self.start_btn, True, True, 0)
-        hbox4.pack_start(self.stop_btn, True, True, 0)
+        control_hbox = gtk.HBox(True, 2)
+        control_hbox.pack_start(self.start_btn, True, True, 0)
+        control_hbox.pack_start(self.stop_btn, True, True, 0)
 
         cr_vbox = gtk.VBox(False, 2)
         cr_vbox.set_border_width(2)
-        cr_vbox.pack_start(hbox1, False, False, 0)
-        cr_vbox.pack_start(hbox2, False, False, 0)
-        cr_vbox.pack_start(hbox3, False, False, 0)
-        cr_vbox.pack_end(hbox4, False, False, 0)
+        cr_vbox.pack_start(path_hbox, False, False, 0)
+        cr_vbox.pack_start(qty_hbox, False, False, 0)
+        cr_vbox.pack_start(filter_hbox, False, False, 0)
+        cr_vbox.pack_start(out_hbox, False, False, 0)
+        cr_vbox.pack_end(control_hbox, False, False, 0)
 
         self.nb.new_tab(cr_vbox, _('Criteria'), noclose=True)
 
@@ -245,7 +267,13 @@ class Gtk_Presentation(object):
                 out = {}
                 out['name'] = btn.get_name()
                 out['label'] = btn.get_label()
-        return out, self.path_entry.get_text(), int(self.qty_spin.get_value())
+        file_filter = self.filter_entry.get_text().split(';')
+        # If no filter - show all files
+        if file_filter == ['']:
+            file_filter = ['*.*']
+        return out, \
+            self.path_entry.get_text(), int(self.qty_spin.get_value()), \
+            file_filter
 
     def get_stopit(self):
         return self.stopit
@@ -255,24 +283,14 @@ class Gtk_Presentation(object):
         self.statusbar.push(self.context_id, current_path)
         gtk.main_iteration()
 
-    def _new_page(self, child, title):
-        """Create new notebook page with search output."""
-        pages = self.nb.notebook.get_n_pages()
-        self.nb.new_tab(child, title)
-        child.show_all()
-        self.nb.notebook.set_current_page(pages)
-
     def run(self):
         pass
 
     #=== Output type selecting ================================================
     def show_out_toplevel(self, outtype, results):
-        print 'Entering <' + outtype['name'] + '> output mode...'
         out_submodule = __import__('files.' + outtype['name'], None, None, outtype)
-
         self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel
-
-        self._new_page(self.out_toplevel, outtype['label'])
+        self.nb.new_tab(self.out_toplevel, outtype['label'])
 ###        out_submodule.Gtk_Presentation().show_results(results)
 
 #==============================================================================