Added config
[findit] / src / files / search.py
index 7e7c7f2..ff4ff83 100755 (executable)
@@ -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...'