Objects updates
[findit] / src / files / search.py
index 2223540..4c7a54c 100755 (executable)
@@ -5,40 +5,44 @@
 from os import walk
 from os.path import join, abspath, normcase, basename, isdir, getsize
 from heapq import nlargest
-import gtk ###
 
 from misc import *
 
 #==============================================================================
 
 class Control(object):
-    
-    def __init__(self):
+
+    def __init__(self, ui):
         ignore_dirs = ['/dev', '/proc', '/sys', '/mnt']
         start_path = '.'
         count = 7
-        self.search_abstrac = Abstraction(ignore_dirs)
-        self.search_present = Gtk_Presentation(start_path, count, self.start_search)
+
+        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.abstrac = Abstraction(ignore_dirs, self.present)
 
     def start_search(self, get_data, get_stopit, label, kill_func):
         filelist = []
         start_path, count = get_data()
-        search_func = self.search_abstrac.filegetter(start_path, get_stopit, label)
+        search_func = self.abstrac.filegetter(start_path, get_stopit, label)
         for fsize, fpath in nlargest(count, search_func):
             filelist.append([fpath, size_hum_read(fsize), int(fsize)])
-        if not get_stopit():
-            #kill_func()
-            self.search_present.show_out_toplevel(None, 'outtable', filelist)
+        self.present.show_out_toplevel(None, 'outtable', filelist)
 
     def run(self):
-        return self.search_present.get_toplevel()
+        return self.present.toplevel
+
 
 #==============================================================================
 
 class Abstraction(object):
-    
-    def __init__(self, ignore_dirs):
+
+    def __init__(self, ignore_dirs, presentation):
         self.ignore_dirs = ignore_dirs
+        self.presentation = presentation
 
     def filegetter(self, startdir, get_stopit, label):
         """Generator of file sizes and paths based on os.walk."""
@@ -55,15 +59,15 @@ class Abstraction(object):
 
             for fname in fnames:
                 flpath = abspath(join(dirpath, fname))
-                # Выводим текущий опрашиваемый файл в строку статуса
-                #label.set_text(flpath)
-                # обновляем окно
-                gtk.main_iteration() ###
+                #self.presentation.show_current_status(flpath)
+                self.presentation.update_window(flpath)
+
                 # Останавливаем цикл по нажатию кнопки стоп
                 stopit = get_stopit()
                 if stopit:
                     stopit = False
                     raise StopIteration
+                    print 'Stopped'
                 # Проверяем можем ли мы определить размер файла - иначе пропускаем его
                 try:
                     # Возвращаем размер и полный путь файла
@@ -71,14 +75,20 @@ class Abstraction(object):
                 except OSError:
                     continue
 
+
 #==============================================================================
 
 class Cli_Presentation(object):
-    def __init__(self):
+    def __init__(self, start_func):
         self.stopit = False
+                  #     get_data,      get_stopit, label, kill_func)
         start_func(self.get_data, self.get_stopit, self.kill_wind)
         pass
 
+    def show_current_status(self, current_path):
+        print current_path
+
+
 #==============================================================================
 
 class Gtk_Presentation(object):
@@ -124,20 +134,23 @@ class Gtk_Presentation(object):
         for btn in reversed(out_rbtns):
             hbox.pack_end(btn, False, False, 0)
 
-        statusbar = gtk.Statusbar()
-        context_id = statusbar.get_context_id('Current walked file')
-        statusbar.push(context_id, 'test')
+        self.statusbar = gtk.Statusbar()
+        self.context_id = self.statusbar.get_context_id('Current walked file')
 
         self.vbox = gtk.VBox(False, 4)
-        self.vbox.set_border_width(4)
         self.vbox.pack_start(self.path_entry, False, False, 0)
         self.vbox.pack_start(hbox, False, False, 0)
-        self.vbox.pack_end(statusbar, False, False, 0)
+        self.vbox.pack_end(self.statusbar, False, False, 0)
+
+        self.toplevel = self.vbox
 
-#        self.show_out_toplevel(None, 'outtable')
-        self.frame = gtk.Frame('Files search')
-        self.frame.add(self.vbox)
-        self.frame.show_all()
+        # 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.update_window = show_current_status
+
+#        self.show_out_toplevel(None, 'outtable', [(11, 22, 33)])
 
     #=== Functions ============================================================
     def start_btn_released(self, widget, start_func):
@@ -145,6 +158,8 @@ class Gtk_Presentation(object):
         self.stop_btn.set_sensitive(True)
         self.start_btn.set_sensitive(False)
         start_func(self.get_data, self.get_stopit, None, None)
+        self.stop_btn.set_sensitive(False)
+        self.start_btn.set_sensitive(True)
 
     def stop_btn_clicked(self, widget):
         self.stopit = True
@@ -157,16 +172,12 @@ class Gtk_Presentation(object):
     def get_stopit(self):
         return self.stopit
 
-    #=== Toplevel widget for embedding to main window =========================
-    def get_toplevel(self):
-        return self.frame ###
-
     #=== Output type selecting ================================================
     def show_out_toplevel(self, btn, outtype, results):
         print 'Entering <' + outtype + '> output mode...'
-        out_present = __import__('files.' + outtype, None, None, outtype)
-        toplevel = out_present.Gtk_Presentation().get_toplevel()
-        ###self.vbox.remove(self.vbox.get_children()[2])
-        #self.vbox.add(toplevel)
-        self.vbox.pack_start(toplevel)
-        print results
+        out_submodule = __import__('files.' + outtype, None, None, outtype)
+        out_toplevel = out_submodule.Gtk_Presentation(results)
+
+###        self.vbox.remove(self.vbox.get_children()[2])
+        self.vbox.add(out_toplevel)
+#        out_submodule.Gtk_Presentation().show_results(results)