[PATCH 1/5] Remove ui.detector class

Sebastian Spaeth Sebastian at SSpaeth.de
Wed Jan 5 16:00:54 GMT 2011


The ui.detector class was not really needed and leads to the illusion
that we provide GUI plugins. For the sake of code maintainability we
don't :-).

Rather than having GUI names equivalent to the classes they are in
(which leads to weird names like TTY.TTYUI), this patch allows to give
each GUI an arbitrary string name. GUI names remain still unchanged in
this patch, the default UI when none was configured is TTY.TTYUI.

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
 offlineimap/init.py        |   20 +++++++++++-----
 offlineimap/ui/__init__.py |   29 +++++++++--------------
 offlineimap/ui/detector.py |   54 --------------------------------------------
 3 files changed, 25 insertions(+), 78 deletions(-)
 delete mode 100644 offlineimap/ui/detector.py

diff --git a/offlineimap/init.py b/offlineimap/init.py
index 4a7c67a..4f8490b 100644
--- a/offlineimap/init.py
+++ b/offlineimap/init.py
@@ -22,7 +22,6 @@ from offlineimap.localeval import LocalEval
 from offlineimap.threadutil import InstanceLimitedThread, ExitNotifyThread
 import offlineimap.ui
 from offlineimap.CustomConfig import CustomConfigParser
-from offlineimap.ui.detector import DEFAULT_UI_LIST
 from optparse import OptionParser
 import re, os, sys
 from threading import *
@@ -151,7 +150,7 @@ class OfflineImap:
               "configuration file. The UI specified with -u will "
               "be forced to be used, even if checks determine that it is "
               "not usable. Possible interface choices are: %s " %
-              ", ".join(DEFAULT_UI_LIST))
+              ", ".join(offlineimap.ui.UI_LIST.keys()))
 
         (options, args) = parser.parse_args()
 
@@ -187,10 +186,19 @@ class OfflineImap:
                     section = "general"
                 config.set(section, key, value)
 
-        #init the ui, and set up additional log files
-        ui = offlineimap.ui.detector.findUI(config, options.interface)
+        #init the ui, cmd line option overrides config file
+        ui_type = config.getdefault('general','ui', 'TTY.TTYUI')
+        if options.interface != None:
+            ui_type = options.interface
+        try:
+            ui = offlineimap.ui.UI_LIST[ui_type](config)
+        except KeyError:
+            logging.error("UI '%s' does not exist, choose one of: %s" % \
+                              (ui_type,', '.join(offlineimap.ui.UI_LIST.keys())))
+            sys.exit(1)
         offlineimap.ui.UIBase.setglobalui(ui)
-    
+
+        #set up additional log files
         if options.logfile:
             ui.setlogfd(open(options.logfile, 'wt'))
     
@@ -236,7 +244,7 @@ class OfflineImap:
     
         def sigterm_handler(self, signum, frame):
             # die immediately
-            ui = BaseUI.getglobalui()
+            ui = offlineimap.ui.getglobalui()
             ui.terminate(errormsg="terminating...")
 
         signal.signal(signal.SIGTERM,sigterm_handler)
diff --git a/offlineimap/ui/__init__.py b/offlineimap/ui/__init__.py
index 0206ab4..83d81c6 100644
--- a/offlineimap/ui/__init__.py
+++ b/offlineimap/ui/__init__.py
@@ -1,6 +1,5 @@
-# UI module directory
-# Copyright (C) 2002 John Goerzen
-# <jgoerzen at complete.org>
+# UI module
+# Copyright (C) 2010 Sebastian Spaeth <Sebastian at SSpaeth.de>
 #
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
@@ -16,23 +15,17 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
+from offlineimap.ui.UIBase import getglobalui, setglobalui
+from offlineimap.ui import TTY, Noninteractive, Machine
 
-import UIBase, Blinkenlights
-try:
-    import TTY
-except ImportError:
-    pass
+UI_LIST = {'TTY.TTYUI': TTY.TTYUI,
+           'Noninteractive.Basic': Noninteractive.Basic,
+           'Noninteractive.Quiet': Noninteractive.Quiet, 
+           'Machine.MachineUI': Machine.MachineUI}
 
+#add Blinkenlights UI if it imports correctly (curses installed)
 try:
-    import curses
+    from offlineimap.ui import Curses
+    UI_LIST['Curses.Blinkenlights'] = Curses.Blinkenlights
 except ImportError:
     pass
-else:
-    import Curses
-
-import Noninteractive
-import Machine
-
-# Must be last
-import detector
-
diff --git a/offlineimap/ui/detector.py b/offlineimap/ui/detector.py
deleted file mode 100644
index 4ec7503..0000000
--- a/offlineimap/ui/detector.py
+++ /dev/null
@@ -1,54 +0,0 @@
-# UI base class
-# Copyright (C) 2002 John Goerzen
-# <jgoerzen at complete.org>
-#
-#    This program is free software; you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation; either version 2 of the License, or
-#    (at your option) any later version.
-#
-#    This program is distributed in the hope that it will be useful,
-#    but WITHOUT ANY WARRANTY; without even the implied warranty of
-#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
-#
-#    You should have received a copy of the GNU General Public License
-#    along with this program; if not, write to the Free Software
-#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
-
-import offlineimap.ui
-import sys
-
-DEFAULT_UI_LIST = ('Curses.Blinkenlights', 'TTY.TTYUI',
-                   'Noninteractive.Basic', 'Noninteractive.Quiet',
-                   'Machine.MachineUI')
-
-def findUI(config, chosenUI=None):
-    uistrlist = list(DEFAULT_UI_LIST)
-    namespace={}
-    for ui in dir(offlineimap.ui):
-        if ui.startswith('_') or ui in ('detector', 'UIBase'):
-            continue
-        namespace[ui]=getattr(offlineimap.ui, ui)
-
-    if chosenUI is not None:
-        uistrlist = [chosenUI]
-    elif config.has_option("general", "ui"):
-        uistrlist = config.get("general", "ui").replace(" ", "").split(",")
-
-    for uistr in uistrlist:
-        uimod = getUImod(uistr, config.getlocaleval(), namespace)
-        if uimod:
-            uiinstance = uimod(config)
-            if uiinstance.isusable():
-                return uiinstance
-    sys.stderr.write("ERROR: No UIs were found usable!\n")
-    sys.exit(200)
-    
-def getUImod(uistr, localeval, namespace):
-    try:
-        uimod = localeval.eval(uistr, namespace)
-    except (AttributeError, NameError), e:
-        #raise
-        return None
-    return uimod
-- 
1.7.1




More information about the OfflineIMAP-project mailing list