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
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)
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):
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
#==============================================================================
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
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)
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