From 9cac287424e94e66aa21335640428800fb3981a5 Mon Sep 17 00:00:00 2001 From: Eugene Gagarin Date: Tue, 5 May 2009 12:05:39 +0400 Subject: [PATCH] Implemented draft of config globalization - by Wall --- src/config.py | 67 ++++++++++++++++++++++++++++++++++++--------------- src/files/search.py | 40 ++++++++++++++---------------- src/main.py | 40 +++++++++++++++--------------- 3 files changed, 84 insertions(+), 63 deletions(-) diff --git a/src/config.py b/src/config.py index 9e78a33..41943a9 100755 --- a/src/config.py +++ b/src/config.py @@ -1,25 +1,52 @@ #!/usr/bin/env python # -*-coding: utf-8 -*- # vim: sw=4 ts=4 expandtab ai -# main.py --search files -o=table -p ". 7" +CONF = ''' +[DEFAULT] +search = 'files' +outtype = 'out_diabar' + +[files] +ignore_dirs = ['/dev', '/proc', '/sys', '/mnt'] +start_path = '..' +count = 7 + +[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'] = 'out_table' - -# 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 6c3af66..a8586f1 100755 --- a/src/files/search.py +++ b/src/files/search.py @@ -7,16 +7,15 @@ 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): - self.config = config - - self.present = eval(ui + '_Presentation(config, self.start_search)') - self.abstrac = Abstraction(self.config, self.present) + def __init__(self, ui, params): + self.present = eval(ui + '_Presentation(self.start_search, params)') + self.abstrac = Abstraction(self.present) self.toplevel = self.present.toplevel @@ -35,8 +34,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): @@ -72,12 +71,12 @@ class Abstraction(object): #============================================================================== class Cli_Presentation(object): - def __init__(self, config, start_func): + def __init__(self, start_func, params): self.start_func = start_func - self.outtype = config['outtype'] - self.start_path = config['start_path'] - self.count = config['count'] + self.outtype = params['outtype'] + self.start_path = params['start_path'] + self.count = params['count'] self.stopit = False self.toplevel = None @@ -107,24 +106,23 @@ 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')) +# 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(self.config['count']) + self.qty_spin.set_value(config['files']['count']) # "Start" button self.start_btn = gtk.Button(_('Start')) @@ -156,7 +154,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() @@ -175,7 +173,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): @@ -222,11 +220,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 run(self): pass diff --git a/src/main.py b/src/main.py index 66e787e..2a8e06b 100755 --- a/src/main.py +++ b/src/main.py @@ -1,11 +1,11 @@ #!/usr/bin/env python # -*-coding: utf-8 -*- # vim: sw=4 ts=4 expandtab ai -# main.py --search files -o outtable -p ". 3" +# main.py --search files -o out_table -p ". 3" -import sys +import sys ### -from config import Config +from config import config from misc import _ __progname__ = 'FindIT' @@ -15,15 +15,13 @@ __version__ = '0.2.0' class Control(object): def __init__(self): - config = Config ###() - self.abstrac = Abstraction() if(len(sys.argv) > 1): ### - self.present = Cli_Presentation(self.abstrac) ### + self.present = Cli_Presentation(self.abstrac) else: import gtk; global gtk - self.present = Gtk_Presentation(config, self.abstrac) ### + self.present = Gtk_Presentation(self.abstrac) def run(self): self.present.run() @@ -62,11 +60,12 @@ class Cli_Presentation(object): parser.add_option('--license', action='callback', callback=self._license) (options, args) = parser.parse_args() - self.config = {} - self.config['search'] = options.search - self.config['outtype'] = options.output - self.config['ignore_dirs'] = ['/dev', '/proc', '/sys', '/mnt'] - self.config['start_path'], self.config['count'] = options.params.split() + self.search = options.search + + self.params = {} + self.params['outtype'] = options.output + self.params['ignore_dirs'] = config['files']['ignore_dirs'] ### + self.params['start_path'], self.params['count'] = options.params.split() def _about(self, *a): print self.abstrac.comments @@ -77,8 +76,8 @@ class Cli_Presentation(object): sys.exit() def show_search_toplevel(self): - search_module = __import__(self.config['search'] + '.search') - search_toplevel = search_module.search.Control('Cli', self.config).run() + search_module = __import__(self.search + '.search') + search_toplevel = search_module.search.Control('Cli', self.params).run() def run(self): self.show_search_toplevel() @@ -88,8 +87,7 @@ class Cli_Presentation(object): class Gtk_Presentation(object): """Main window class.""" - def __init__(self, config, abstrac): - self.config = config + def __init__(self, abstrac): def _create_menu(): """Create main menu.""" @@ -125,7 +123,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 @@ -155,7 +153,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']) self.window.add(self.vbox) @@ -164,7 +162,7 @@ class Gtk_Presentation(object): print 'Entering <' + searchtype + '> search mode...' search_module = __import__(searchtype + '.search') - search = search_module.search.Control('Gtk', self.config) + search = search_module.search.Control('Gtk', None) search_toplevel = search.toplevel try: @@ -183,8 +181,8 @@ class Gtk_Presentation(object): class Hildon_Presentation(object): """Main window class.""" - def __init__(self, config): - self.config = config + def __init__(self): + pass #============================================================================== -- 1.7.9.5