From: Eugene Gagarin Date: Wed, 29 Apr 2009 06:59:48 +0000 (+0400) Subject: Added config X-Git-Url: http://git.maemo.org/git/?p=findit;a=commitdiff_plain;h=de1a44eca95c14953c321cfcd4f7716e9c954ea6 Added config --- diff --git a/src/config.py b/src/config.py new file mode 100755 index 0000000..ffd81c3 --- /dev/null +++ b/src/config.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +# -*-coding: utf-8 -*- +# vim: sw=4 ts=4 expandtab ai +# main.py --search files -o=table -p ". 7" + + +# Dummy config + +Config = {} + +Config['search'] = 'files' +Config['outtype'] = 'outtable' + +# files +Config['ignore_dirs'] = ['/dev', '/proc', '/sys', '/mnt'] +Config['start_path'] = '.' +Config['count'] = 5 + +# # debs +# Config['count'] = 7 + + +# if __name__ == '__main__': +# config = Config +# print config['search'] diff --git a/src/files/search.py b/src/files/search.py index 7e7c7f2..ff4ff83 100755 --- a/src/files/search.py +++ b/src/files/search.py @@ -12,23 +12,25 @@ from misc import * class Control(object): - def __init__(self, ui): - ignore_dirs = ['/dev', '/proc', '/sys', '/mnt'] - start_path = '.' - count = 7 + def __init__(self, ui, config): - print ui if ui == 'cli': - self.present = Cli_Presentation(start_path, count, self.start_search) + self.present = Cli_Presentation(config) elif ui == 'gtk': - self.present = Gtk_Presentation(start_path, count, self.start_search) + self.present = Gtk_Presentation(config, self.start_search) + elif ui == 'hildon': + self.present = Hildon_Presentation(config, self.start_search) - self.abstrac = Abstraction(ignore_dirs, self.present) + # self.present - for updating windows in interactive presentations + self.abstrac = Abstraction(config, self.present) - def start_search(self, get_data, get_stopit, label, kill_func): + # Used only in non-interactive presentations + self.present.start_search(self.start_search) + + 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) @@ -40,16 +42,16 @@ class Control(object): 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 +63,14 @@ 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' - # Проверяем можем ли мы определить размер файла - иначе пропускаем его + # Query only valid files try: - # Возвращаем размер и полный путь файла + # Return results (bytesize, path) yield getsize(flpath), flpath except OSError: continue @@ -77,25 +78,43 @@ class Abstraction(object): #============================================================================== class Cli_Presentation(object): - def __init__(self, start_func): + def __init__(self, config): + 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 start_search(self, start_func): + 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') @@ -105,7 +124,7 @@ class Gtk_Presentation(object): 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') @@ -134,6 +153,9 @@ class Gtk_Presentation(object): hbox.pack_start(self.stop_btn, False, False, 0) for btn in reversed(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 +167,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) @@ -171,11 +193,15 @@ class Gtk_Presentation(object): for btn in self.outtable_rbtn.get_group(): 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 + # Empty because search start by button + def start_search(self, start_func): + pass + #=== Output type selecting ================================================ def show_out_toplevel(self, btn, outtype, results): print 'Entering <' + outtype + '> output mode...' diff --git a/src/main.py b/src/main.py index f988978..60e5bef 100755 --- a/src/main.py +++ b/src/main.py @@ -1,10 +1,12 @@ #!/usr/bin/env python # -*-coding: utf-8 -*- # vim: sw=4 ts=4 expandtab ai -# main.py --search files -o=table -p ". 7" +# main.py --search files -o outtable -p ". 3" import sys +from config import Config + __progname__ = 'FindIT' __version__ = '0.2.0' @@ -12,10 +14,14 @@ __version__ = '0.2.0' class Control(object): def __init__(self): + config = Config ###() + + self.abstrac = Abstraction() + if(len(sys.argv) > 1): - Cli_Presentation() + Cli_Presentation() ### else: - Gtk_Presentation() + Gtk_Presentation(config, self.abstrac) ### #============================================================================== @@ -34,23 +40,32 @@ class Cli_Presentation(object): parser.add_option('--output', '-o', dest='output', type='string') parser.add_option('--params', '-p', dest='params', type='string') (options, args) = parser.parse_args() - print options - print args +# print options +# print args - self.show_search_toplevel(options.search, options.output, options.params) + config = {} + config['search'] = options.search + config['outtype'] = options.output + config['ignore_dirs'] = ['/dev', '/proc', '/sys', '/mnt'] + config['start_path'] = options.params.split(' ')[0] + config['count'] = options.params.split(' ')[1] - def show_search_toplevel(self, searchtype, output, params): - search_module = __import__(searchtype + '.search') - toplevel = search_module.search.Control('cli').run() + self.show_search_toplevel(config) + + def show_search_toplevel(self, config): + search_module = __import__(config['search'] + '.search') + search_toplevel = search_module.search.Control('cli', config).run() #============================================================================== class Gtk_Presentation(object): """Main window class.""" - def __init__(self): + def __init__(self, config, abstrac): import gtk + self.config = config + def _create_menu(): """Create main menu.""" menubar = gtk.MenuBar() @@ -110,7 +125,7 @@ class Gtk_Presentation(object): self.vbox = gtk.VBox(False, 4) self.vbox.pack_start(menu, False, False, 0) self.vbox.pack_start(toolbar, False, False, 0) - self.show_search_toplevel(None, 'files') ### + self.show_search_toplevel(None, self.config['search']) window.add(self.vbox) window.show_all() @@ -125,7 +140,7 @@ class Gtk_Presentation(object): print 'Entering <' + searchtype + '> search mode...' search_module = __import__(searchtype + '.search') - search_toplevel = search_module.search.Control('gtk').run() + search_toplevel = search_module.search.Control('gtk', self.config).run() try: self.vbox.remove(self.vbox.get_children()[2])