[orca/gnome-3-6] Fix for bug 686189 - Use importlib.import_module() instead of __import__() and imp.reload() instead



commit 8d63edbba86e54d80f97eca69c69ad5865ed91a3
Author: Joanmarie Diggs <jdiggs igalia com>
Date:   Sat Oct 20 19:23:17 2012 +0000

    Fix for bug 686189 - Use importlib.import_module() instead of __import__() and imp.reload() instead of reload()

 src/orca/app_prefs.py        |    3 ++-
 src/orca/orca.py             |   31 +++++++------------------------
 src/orca/script.py           |    6 ++++--
 src/orca/script_manager.py   |   10 ++++++----
 src/orca/settings.py         |   29 -----------------------------
 src/orca/settings_manager.py |   14 +++++++-------
 src/orca/speech.py           |   20 ++++----------------
 7 files changed, 30 insertions(+), 83 deletions(-)
---
diff --git a/src/orca/app_prefs.py b/src/orca/app_prefs.py
index 116bb28..72c0b49 100644
--- a/src/orca/app_prefs.py
+++ b/src/orca/app_prefs.py
@@ -245,6 +245,7 @@ class OrcaPrefs:
         prefs.writelines( \
                  "# be overwritten, edit %s-customizations.py.\n" % appName)
         prefs.writelines("#\n")
+        prefs.writelines("import importlib\n")
         prefs.writelines("import orca.settings\n")
         prefs.writelines("import orca.acss\n")
         prefs.writelines("\n")
@@ -260,7 +261,7 @@ class OrcaPrefs:
 
         prefs.writelines("\ntry:\n")
         prefs.writelines( \
-           "    __import__(\"app-settings.%s-customizations\")\n" % appName)
+           "    importlib.import_module(\"app-settings.%s-customizations\")\n" % appName)
         prefs.writelines("except ImportError:\n")
         prefs.writelines("    pass\n")
 
diff --git a/src/orca/orca.py b/src/orca/orca.py
index ad2ba17..fbb76db 100644
--- a/src/orca/orca.py
+++ b/src/orca/orca.py
@@ -29,6 +29,7 @@ __copyright__ = "Copyright (c) 2004-2009 Sun Microsystems Inc." \
                 "Copyright (c) 2012 Igalia, S.L."
 __license__   = "LGPL"
 
+import importlib
 import os
 import pyatspi
 import re
@@ -485,10 +486,7 @@ def showAppPreferencesGUI(script=None, inputEvent=None):
     """
 
     try:
-        module = __import__(settings.appGuiPreferencesModule,
-                            globals(),
-                            locals(),
-                            [''])
+        module = importlib.import_module('.app_gui_prefs', 'orca')
         module.showPreferencesUI()
     except:
         debug.printException(debug.LEVEL_SEVERE)
@@ -503,10 +501,7 @@ def showPreferencesGUI(script=None, inputEvent=None):
     """
 
     try:
-        module = __import__(settings.guiPreferencesModule,
-                            globals(),
-                            locals(),
-                            [''])
+        module = importlib.import_module('.orca_gui_prefs', 'orca')
         module.showPreferencesUI()
     except:
         debug.printException(debug.LEVEL_SEVERE)
@@ -520,10 +515,7 @@ def showMainWindowGUI(script=None, inputEvent=None):
     """
 
     try:
-        module = __import__(settings.mainWindowModule,
-                            globals(),
-                            locals(),
-                            [''])
+        module = importlib.import_module('.orca_gui_main', 'orca')
         if settings.showMainWindow or settings.overrideDisabledMainWindow:
             module.showMainUI()
         else:
@@ -725,10 +717,7 @@ def quitOrca(script=None, inputEvent=None):
         shutdown()
     else:
         try:
-            module = __import__(settings.quitModule,
-                                globals(),
-                                locals(),
-                                [''])
+            module = importlib.import_module('.orca_gui_quit', 'orca')
             module.showQuitUI()
         except:
             debug.printException(debug.LEVEL_SEVERE)
@@ -742,10 +731,7 @@ def showFindGUI(script=None, inputEvent=None):
     """
 
     try:
-        module = __import__(settings.findModule,
-                            globals(),
-                            locals(),
-                            [''])
+        module = importlib.import_module('.orca_gui_find', 'orca')
         module.showFindUI()
     except:
         debug.printException(debug.LEVEL_SEVERE)
@@ -757,10 +743,7 @@ def showSplashGUI(script=None, inputEvent=None):
     """
 
     try:
-        module = __import__(settings.splashModule,
-                            globals(),
-                            locals(),
-                            [''])
+        module = importlib.import_module('.orca_gui_splash', 'orca')
         if _settingsManager.getSetting("showSplashWindow"):
             module.showSplashUI()
         else:
diff --git a/src/orca/script.py b/src/orca/script.py
index dbdaa30..bb2e60a 100644
--- a/src/orca/script.py
+++ b/src/orca/script.py
@@ -39,6 +39,8 @@ __date__      = "$Date$"
 __copyright__ = "Copyright (c) 2005-2009 Sun Microsystems Inc."
 __license__   = "LGPL"
 
+import imp
+import importlib
 import pyatspi
 
 from . import braille_generator
@@ -351,8 +353,8 @@ class Script:
                 for package in _settingsManager.settingsPackages:
                     name = package + "." + name
                     try:
-                        module = __import__(name, globals(), locals(), [''])
-                        reload(module)
+                        module = importlib.import_module(name)
+                        imp.reload(module)
                         scriptSettings = module.orca.settings
                     except:
                         pass
diff --git a/src/orca/script_manager.py b/src/orca/script_manager.py
index be900f4..d6cdca1 100644
--- a/src/orca/script_manager.py
+++ b/src/orca/script_manager.py
@@ -24,6 +24,8 @@ __date__      = "$Date$"
 __copyright__ = "Copyright (c) 2011. Orca Team."
 __license__   = "LGPL"
 
+import importlib
+
 from . import debug
 from . import orca_state
 from .scripts import apps, toolkits
@@ -39,9 +41,9 @@ class ScriptManager:
         self._defaultScript = None
         self._scriptPackages = \
             ["orca-scripts",
-             "scripts",
-             "scripts.apps",
-             "scripts.toolkits"]
+             "orca.scripts",
+             "orca.scripts.apps",
+             "orca.scripts.toolkits"]
         self._appNames = \
             {'Bon Echo':         'Mozilla',
              'Deer Park':        'Mozilla',
@@ -153,7 +155,7 @@ class ScriptManager:
             moduleName = '.'.join((package, name))
             debug.println(debug.LEVEL_FINE, "Looking for %s.py" % moduleName)
             try:
-                module = __import__(moduleName, globals(), locals(), [''])
+                module = importlib.import_module(moduleName)
             except ImportError:
                 debug.println(
                     debug.LEVEL_FINE, "Could not import %s.py" % moduleName)
diff --git a/src/orca/settings.py b/src/orca/settings.py
index 48a866a..4c776bb 100644
--- a/src/orca/settings.py
+++ b/src/orca/settings.py
@@ -118,35 +118,6 @@ excludeKeys = ["pronunciations",
                "startingProfile",
                "activeProfile"]
 
-# The name of the module that hold the user interface for the main window
-# for Orca. This module is expected to have two methods, showMainUI and
-# hideMainUI, which will show and hide the main window GUI.
-#
-mainWindowModule = "orca_gui_main"
-
-# The name of the modules that hold the user interface for setting
-# Orca preferences.  Each module is expected to have the method,
-# showPreferencesUI, which will prompt the user for preferences.
-#
-guiPreferencesModule     = "orca_gui_prefs"
-consolePreferencesModule = "orca_console_prefs"
-appGuiPreferencesModule  = "app_gui_prefs"
-
-# The name of the module that hold the user interface for quitting Orca.
-# This module is expected to have the method, showQuitUI, which will
-# display the quit GUI.
-#
-quitModule = "orca_gui_quit"
-
-# The name of the module that holds the user interface for performing a
-# flat review find.
-#
-findModule = "orca_gui_find"
-
-# The name of the module that holds the splash screen.
-#
-splashModule = "orca_gui_splash"
-
 # Profiles
 #
 startingProfile = ['Default', 'default']
diff --git a/src/orca/settings_manager.py b/src/orca/settings_manager.py
index 011912d..b132c64 100644
--- a/src/orca/settings_manager.py
+++ b/src/orca/settings_manager.py
@@ -28,8 +28,9 @@ __date__      = "$Date$"
 __copyright__ = "Copyright (c) 2010 Consorcio Fernando de los Rios."
 __license__   = "LGPL"
 
-import os
 import imp
+import importlib
+import os
 from gi.repository import Gio, GLib
 
 from . import debug
@@ -150,9 +151,8 @@ class SettingsManager(object):
         """Load specific backend for manage user settings"""
 
         try:
-            backend = 'backends.%s_backend' % self.backendName
-            self.backendModule = __import__(backend, globals(), locals(),
-                                            backend, -1)
+            backend = '.backends.%s_backend' % self.backendName
+            self.backendModule = importlib.import_module(backend, 'orca')
             return True
         except:
             return False
@@ -521,13 +521,13 @@ class SettingsManager(object):
             name = '.'.join((package, moduleName))
             debug.println(debug.LEVEL_FINEST, "Looking for %s.py" % name)
             try:
-                module = __import__(name, globals(), locals(), [''])
+                module = importlib.import_module(name)
             except ImportError:
                 debug.println(
                     debug.LEVEL_FINEST, "Could not import %s.py" % name)
                 continue
             try:
-                reload(module)
+                imp.reload(module)
             except:
                 debug.println(debug.LEVEL_FINEST, "Could not load %s.py" % name)
                 module = None
@@ -539,7 +539,7 @@ class SettingsManager(object):
             return
 
         self._knownAppSettings[name] = module
-        reload(self._knownAppSettings[name])
+        imp.reload(self._knownAppSettings[name])
 
         appVoices = self.getSetting('voices')
         for voiceType, voiceDef in list(appVoices.items()):
diff --git a/src/orca/speech.py b/src/orca/speech.py
index 0700f90..308479c 100644
--- a/src/orca/speech.py
+++ b/src/orca/speech.py
@@ -26,6 +26,7 @@ __date__      = "$Date$"
 __copyright__ = "Copyright (c) 2005-2009 Sun Microsystems Inc."
 __license__   = "LGPL"
 
+import importlib
 import logging
 log = logging.getLogger("speech")
 
@@ -62,10 +63,7 @@ def getSpeechServerFactories():
     moduleNames = settings.speechFactoryModules
     for moduleName in moduleNames:
         try:
-            module =  __import__(moduleName,
-                                 globals(),
-                                 locals(),
-                                 [''])
+            module = importlib.import_module('orca.%s' % moduleName)
             factories.append(module)
         except:
             debug.printException(debug.LEVEL_CONFIGURATION)
@@ -84,19 +82,9 @@ def _initSpeechServer(moduleName, speechServerInfo):
 
     factory = None
     try:
-        factory =  __import__(moduleName,
-                              globals(),
-                              locals(),
-                              [''])
+        factory = importlib.import_module(moduleName)
     except:
-        try:
-            moduleName = moduleName.replace("orca.", "", 1)
-            factory =  __import__(moduleName,
-                                  globals(),
-                                  locals(),
-                                  [''])
-        except:
-            debug.printException(debug.LEVEL_SEVERE)
+        debug.printException(debug.LEVEL_SEVERE)
 
     # Now, get the speech server we care about.
     #



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]