X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Ffiles%2Fsearch.py;h=8215d354214070d71fef5ea6714467dfbb672a25;hb=070b7f21be12b1fdc823f0893d930fbb98dbc875;hp=7e7c7f23d5f216cfb0c28dedb060509e3fd3916f;hpb=6867b5bee29aa86451bf1353cf046d6e07c2e7f2;p=findit diff --git a/src/files/search.py b/src/files/search.py index 7e7c7f2..8215d35 100755 --- a/src/files/search.py +++ b/src/files/search.py @@ -6,50 +6,45 @@ from os import walk from os.path import join, abspath, normcase, basename, isdir, getsize from heapq import nlargest -from misc import * +from misc import size_hum_read, _ #============================================================================== class Control(object): - def __init__(self, ui): - ignore_dirs = ['/dev', '/proc', '/sys', '/mnt'] - start_path = '.' - count = 7 + def __init__(self, ui, config): + self.config = config - print ui - if ui == 'cli': - self.present = Cli_Presentation(start_path, count, self.start_search) - elif ui == 'gtk': - self.present = Gtk_Presentation(start_path, count, self.start_search) + self.present = eval(ui + '_Presentation(config, self.start_search)') + self.abstrac = Abstraction(self.config, self.present) - self.abstrac = Abstraction(ignore_dirs, self.present) + self.toplevel = self.present.toplevel - def start_search(self, get_data, get_stopit, label, kill_func): + def start_search(self, get_data, get_stopit): filelist = [] - start_path, count, outtype = get_data() - search_func = self.abstrac.filegetter(start_path, get_stopit, label) + outtype, start_path, count = get_data() + search_func = self.abstrac.filegetter(start_path, get_stopit) for fsize, fpath in nlargest(count, search_func): filelist.append([int(fsize), fpath, size_hum_read(fsize)]) self.present.show_out_toplevel(None, outtype, filelist) def run(self): - return self.present.toplevel + self.present.run() #============================================================================== class Abstraction(object): - def __init__(self, ignore_dirs, presentation): - self.ignore_dirs = ignore_dirs + def __init__(self, config, presentation): + self.ignore_dirs = config['ignore_dirs'] self.presentation = presentation - def filegetter(self, startdir, get_stopit, label): + def filegetter(self, startdir, get_stopit): """Generator of file sizes and paths based on os.walk.""" - # Проходим по всем папкам вглубь от заданного пути - self.full_dir_size = 0 + + # Walk across directory tree for dirpath, dirnames, fnames in walk(startdir): - # Исключаем каталоги из поиска в соответствии со списком исключений + # Eliminate unnecessary directories ignore_dirs = self.ignore_dirs for ign_dir in ignore_dirs[:]: for dirname in dirnames[:]: @@ -61,15 +56,15 @@ class Abstraction(object): flpath = abspath(join(dirpath, fname)) self.presentation.show_current_status(flpath) - # Останавливаем цикл по нажатию кнопки стоп + # Stop search via 'stopit' signal stopit = get_stopit() if stopit: stopit = False - raise StopIteration print 'Stopped' - # Проверяем можем ли мы определить размер файла - иначе пропускаем его + raise StopIteration + # Query only valid files try: - # Возвращаем размер и полный путь файла + # Return results (bytesize, path) yield getsize(flpath), flpath except OSError: continue @@ -77,63 +72,84 @@ class Abstraction(object): #============================================================================== class Cli_Presentation(object): - def __init__(self, start_func): + def __init__(self, config, start_func): + self.start_func = start_func + + self.outtype = config['outtype'] + self.start_path = config['start_path'] + self.count = config['count'] self.stopit = False - # get_data, get_stopit, label, kill_func) - start_func(self.get_data, self.get_stopit, self.kill_wind) - pass + + self.toplevel = None + + def get_data(self): + return self.outtype, self.start_path, int(self.count) + + def get_stopit(self): + return False + + def show_out_toplevel(self, _, outtype, results): + out_submodule = __import__('files.' + outtype, None, None, outtype) + out_submodule.Cli_Presentation(results).toplevel def show_current_status(self, current_path): - print current_path + pass + ### print current_path + + def run(self): + self.start_func(self.get_data, self.get_stopit) #============================================================================== class Gtk_Presentation(object): - def __init__(self, start_path, count, start_func): + def __init__(self, config, start_func): import gtk + self.config = config + # "Start path" entry self.path_entry = gtk.Entry() - self.path_entry.set_text(start_path) + self.path_entry.set_text(self.config['start_path']) # "Files quantity" label - qty_label = gtk.Label('Files quantity') + qty_label = gtk.Label(_('Files quantity')) # "Files quantity" spin self.qty_spin = gtk.SpinButton() self.qty_spin.set_numeric(True) self.qty_spin.set_range(0, 65536) self.qty_spin.set_increments(1, 10) - self.qty_spin.set_value(count) + self.qty_spin.set_value(self.config['count']) # "Start" button - self.start_btn = gtk.Button('Start') + self.start_btn = gtk.Button(_('Start')) self.start_btn.connect('released', self.start_btn_released, start_func) # "Stop" button - self.stop_btn = gtk.Button('Stop') + self.stop_btn = gtk.Button(_('Stop')) self.stop_btn.set_sensitive(False) self.stop_btn.connect('clicked', self.stop_btn_clicked) # Output selection - self.outtable_rbtn = gtk.RadioButton(None, 'Table') - self.outtable_rbtn.set_name('outtable') - outdiagram_rbtn = gtk.RadioButton(self.outtable_rbtn, 'Diagram') + outtable_rbtn = gtk.RadioButton(None, _('Table')) + outtable_rbtn.set_name('outtable') + outdiagram_rbtn = gtk.RadioButton(outtable_rbtn, _('Diagram')) outdiagram_rbtn.set_name('outdiagram') - out1_rbtn = gtk.RadioButton(self.outtable_rbtn, 'Another 1') + out1_rbtn = gtk.RadioButton(outtable_rbtn, 'Another 1') out1_rbtn.set_name('outanother1') - out2_rbtn = gtk.RadioButton(self.outtable_rbtn, 'Another 2') - out2_rbtn.set_name('outanother2') - out_rbtns = [self.outtable_rbtn, outdiagram_rbtn, out1_rbtn, out2_rbtn] + self.out_rbtns = [outtable_rbtn, outdiagram_rbtn, out1_rbtn] hbox = gtk.HBox(False, 4) hbox.pack_start(qty_label, False, False, 0) hbox.pack_start(self.qty_spin, False, False, 0) hbox.pack_start(self.start_btn, False, False, 0) hbox.pack_start(self.stop_btn, False, False, 0) - for btn in reversed(out_rbtns): + for btn in reversed(self.out_rbtns): hbox.pack_end(btn, False, False, 0) + # Activate radio button + if btn.get_name() == self.config['outtype']: + btn.set_active(True) self.statusbar = gtk.Statusbar() self.context_id = self.statusbar.get_context_id('Current walked file') @@ -145,20 +161,20 @@ class Gtk_Presentation(object): self.toplevel = self.vbox - # for importing gtk only once (lambda not work) + # For importing gtk only once (lambda not work) def show_current_status(current_path): self.statusbar.push(self.context_id, current_path) gtk.main_iteration() self.show_current_status = show_current_status -# self.show_out_toplevel(None, 'outtable', [(11, 22, 33)]) + self.show_out_toplevel(None, self.config['outtype'], [(1, 'path', 'bytesize')]) #=== Functions ============================================================ def start_btn_released(self, btn, start_func): self.stopit = False self.stop_btn.set_sensitive(True) self.start_btn.set_sensitive(False) - start_func(self.get_data, self.get_stopit, None, None) + start_func(self.get_data, self.get_stopit) self.stop_btn.set_sensitive(False) self.start_btn.set_sensitive(True) @@ -168,26 +184,41 @@ class Gtk_Presentation(object): self.start_btn.set_sensitive(True) def get_data(self): - for btn in self.outtable_rbtn.get_group(): + for btn in self.out_rbtns: if btn.get_active(): out = btn.get_name() - return self.path_entry.get_text(), int(self.qty_spin.get_value()), out + return out, self.path_entry.get_text(), int(self.qty_spin.get_value()) def get_stopit(self): return self.stopit + def run(self): + pass + #=== Output type selecting ================================================ def show_out_toplevel(self, btn, outtype, results): print 'Entering <' + outtype + '> output mode...' out_submodule = __import__('files.' + outtype, None, None, outtype) try: - self.current_outtoplevel.destroy() + self.out_toplevel.destroy() except: pass - out_toplevel = out_submodule.Gtk_Presentation(results).toplevel - self.current_outtoplevel = out_toplevel - self.vbox.add(out_toplevel) - out_toplevel.show_all() -# out_submodule.Gtk_Presentation().show_results(results) + self.out_toplevel = out_submodule.Gtk_Presentation(results).toplevel + self.vbox.add(self.out_toplevel) + self.out_toplevel.show_all() +### out_submodule.Gtk_Presentation().show_results(results) + +#============================================================================== + +class Hildon_Presentation(object): + + def __init__(self, config, start_func): + import gtk + import hildon + + self.config = config + + def run(self): + pass