Added file filter to files.search
authorEugene Gagarin <mosfet07@ya.ru>
Thu, 14 May 2009 10:53:34 +0000 (14:53 +0400)
committerEugene Gagarin <mosfet07@ya.ru>
Thu, 14 May 2009 10:53:34 +0000 (14:53 +0400)
src/config.py
src/files/search.py

index 41943a9..c7db425 100755 (executable)
@@ -11,6 +11,7 @@ outtype = 'out_diabar'
 ignore_dirs = ['/dev', '/proc', '/sys', '/mnt']
 start_path = '..'
 count = 7
+filter = *.pyc;*.pyo
 
 [debs]
 count = 12
index 11cb0cd..22f22e6 100755 (executable)
@@ -5,6 +5,7 @@
 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 config import config
@@ -28,8 +29,8 @@ 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)
@@ -45,7 +46,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 +59,24 @@ 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):
+                        flpath = abspath(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
 
 #==============================================================================
 
@@ -142,6 +146,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'))
+        # "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 +172,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 +260,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