implemented draft of config globalization Wall/master
authorWall <wall@gmail.com>
Fri, 1 May 2009 14:40:17 +0000 (14:40 +0000)
committerWall <wall@gmail.com>
Fri, 1 May 2009 14:40:17 +0000 (14:40 +0000)
src/config.py
src/debs/search.py
src/files/search.py
src/main.py

index ffd81c3..8147fb7 100755 (executable)
@@ -3,23 +3,51 @@
 # vim: sw=4 ts=4 expandtab ai
 # main.py --search files -o=table -p ". 7"
 
+CONF = '''
+[DEFAULT]
+search = 'files'
+outtype = 'outtable'
+
+[files]
+ignore_dirs = ['/dev', '/proc', '/sys', '/mnt']
+start_path = '.'
+count = 5
+
+[debs]
+count = 12
+'''
+
+from ConfigParser import ConfigParser, NoOptionError
+
+class FindITConfig(ConfigParser):
+    def __init__(self, config):
+        ConfigParser.__init__(self)
+        if isinstance(config, basestring):
+            self.read(config)
+        else:
+            self.readfp(config)
+        self._section = None
+
+    def __getitem__(self, item):
+        try:
+            return self.get('DEFAULT', item)
+        except NoOptionError:
+            if self.has_section(item):
+                self._section = item
+                return self
+            else:
+                return self.get(self._section, item)
+
+    def __setitem__(self, item, value):
+        return self.set(self._section, item, str(value))
+
+    def get(self, section, option):
+        value = ConfigParser.get(self, section, option)
+        try:
+            return eval(value)
+        except SyntaxError:
+            return value
+
+from StringIO import StringIO
+config = FindITConfig(StringIO(CONF))
 
-# 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']
index 2408e9a..e2a1e48 100755 (executable)
@@ -6,13 +6,14 @@ import apt_pkg
 from heapq import nlargest
 
 from misc import size_hum_read, _
+from config import config
 
 #==============================================================================
 
 class Control(object):
 
     def __init__(self, ui):
-        count = 12
+        count = config['debs']['count']
 
         print ui
         if ui == 'cli':
index 6b15c4b..b4feac9 100755 (executable)
@@ -7,22 +7,23 @@ from os.path import join, abspath, normcase, basename, isdir, getsize
 from heapq import nlargest
 
 from misc import size_hum_read, _
+from config import config
 
 #==============================================================================
 
 class Control(object):
 
-    def __init__(self, ui, config):
+    def __init__(self, ui):
 
         if ui == 'cli':
-            self.present = Cli_Presentation(config)
+            self.present = Cli_Presentation()
         elif ui == 'gtk':
-            self.present = Gtk_Presentation(config, self.start_search)
+            self.present = Gtk_Presentation(self.start_search)
         elif ui == 'hildon':
-            self.present = Hildon_Presentation(config, self.start_search)
+            self.present = Hildon_Presentation(self.start_search)
 
         # self.present - for updating windows in interactive presentations
-        self.abstrac = Abstraction(config, self.present)
+        self.abstrac = Abstraction(self.present)
 
         # Used only in non-interactive presentations
         self.present.start_search(self.start_search)
@@ -42,8 +43,8 @@ class Control(object):
 
 class Abstraction(object):
 
-    def __init__(self, config, presentation):
-        self.ignore_dirs = config['ignore_dirs']
+    def __init__(self, presentation):
+        self.ignore_dirs = config['files']['ignore_dirs']
         self.presentation = presentation
 
     def filegetter(self, startdir, get_stopit):
@@ -79,10 +80,10 @@ class Abstraction(object):
 #==============================================================================
 
 class Cli_Presentation(object):
-    def __init__(self, config):
-        self.outtype = config['outtype']
-        self.start_path = config['start_path']
-        self.count = config['count']
+    def __init__(self):
+        self.outtype = config['files']['outtype']
+        self.start_path = config['files']['start_path']
+        self.count = config['files']['count']
         self.stopit = False
 
         self.toplevel = None
@@ -108,14 +109,12 @@ class Cli_Presentation(object):
 
 class Gtk_Presentation(object):
 
-    def __init__(self, config, start_func):
+    def __init__(self, start_func):
         import gtk
 
-        self.config = config
-
         # "Start path" entry
         self.path_entry = gtk.Entry()
-        self.path_entry.set_text(self.config['start_path'])
+        self.path_entry.set_text(config['files']['start_path'])
 
         # "Files quantity" label
         qty_label = gtk.Label(_('Files quantity'))
@@ -125,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(self.config['count'])
+        self.qty_spin.set_value(config['files']['count'])
 
         # "Start" button
         self.start_btn = gtk.Button(_('Start'))
@@ -153,7 +152,7 @@ class Gtk_Presentation(object):
         for btn in reversed(self.out_rbtns):
             hbox.pack_end(btn, False, False, 0)
             # Activate radio button
-            if btn.get_name() == self.config['outtype']:
+            if btn.get_name() == config['outtype']:
                 btn.set_active(True)
 
         self.statusbar = gtk.Statusbar()
@@ -172,7 +171,7 @@ class Gtk_Presentation(object):
             gtk.main_iteration()
         self.show_current_status = show_current_status
 
-        self.show_out_toplevel(None, self.config['outtype'], [(1, 'path', 'bytesize')])
+        self.show_out_toplevel(None, config['outtype'], [(1, 'path', 'bytesize')])
 
     #=== Functions ============================================================
     def start_btn_released(self, btn, start_func):
@@ -220,11 +219,9 @@ class Gtk_Presentation(object):
 
 class Hildon_Presentation(object):
 
-    def __init__(self, config, start_func):
+    def __init__(self, start_func):
         import gtk
         import hildon
 
-        self.config = config
-
     def start_search(self, start_func):
         pass
index cd1bafb..732dec0 100755 (executable)
@@ -5,7 +5,7 @@
 
 import sys
 
-from config import Config
+from config import config
 from misc import _
 
 __progname__ = 'FindIT'
@@ -15,14 +15,13 @@ __version__ = '0.2.0'
 
 class Control(object):
     def __init__(self):
-        config = Config ###()
 
         self.abstrac = Abstraction()
 
         if(len(sys.argv) > 1):
             Cli_Presentation(self.abstrac)  ###
         else:
-            Gtk_Presentation(config, self.abstrac)  ###
+            Gtk_Presentation(self.abstrac)  ###
 
 #==============================================================================
 
@@ -58,13 +57,7 @@ class Cli_Presentation(object):
         parser.add_option('--license', action='callback', callback=self._license)
         (options, args) = parser.parse_args()
 
-        config = {}
-        config['search'] = options.search
-        config['outtype'] = options.output
-        config['ignore_dirs'] = ['/dev', '/proc', '/sys', '/mnt']
-        config['start_path'], config['count'] = options.params.split()
-
-        self.show_search_toplevel(config)
+        self.show_search_toplevel()
 
     def _about(self, *a):
         print self.abstrac.comments
@@ -74,20 +67,18 @@ class Cli_Presentation(object):
         print self.abstrac.license
         sys.exit()
 
-    def show_search_toplevel(self, config):
+    def show_search_toplevel(self):
         search_module = __import__(config['search'] + '.search')
-        search_toplevel = search_module.search.Control('cli', config).run()
+        search_toplevel = search_module.search.Control('cli').run()
 
 #==============================================================================
 
 class Gtk_Presentation(object):
     """Main window class."""
 
-    def __init__(self, config, abstrac):
+    def __init__(self, abstrac):
         import gtk
 
-        self.config = config
-
         def _create_menu():
             """Create main menu."""
             menubar = gtk.MenuBar()
@@ -122,7 +113,7 @@ class Gtk_Presentation(object):
 
             # Activate radio tool button
             for btn in search_tbtns:
-                if btn.get_name() == self.config['search']:
+                if btn.get_name() == config['search']:
                     btn.set_active(True)
 
             return toolbar
@@ -152,7 +143,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, self.config['search'])
+        self.show_search_toplevel(None, config['search'])
 
         window.add(self.vbox)
         window.show_all()
@@ -163,7 +154,7 @@ class Gtk_Presentation(object):
         print 'Entering <' + searchtype + '> search mode...'
 
         search_module = __import__(searchtype + '.search')
-        search_toplevel = search_module.search.Control('gtk', self.config).run()
+        search_toplevel = search_module.search.Control('gtk').run()
 
         try:
             self.vbox.remove(self.vbox.get_children()[2])
@@ -177,11 +168,10 @@ class Gtk_Presentation(object):
 class Hildon_Presentation(object):
     """Main window class."""
 
-    def __init__(self, config):
+    def __init__(self):
         import gtk
         import hildon
 
-        self.config = config
 
 #==============================================================================