From 09ed5b4cc086edf6dc1b6840f81bd9c6abef05d1 Mon Sep 17 00:00:00 2001 From: Martin Sivak Date: Tue, 12 Feb 2013 15:58:03 +0100 Subject: Try to import modules the standard way first in collect This ensures that the correct package structure is present in sys.path for modules and addons that make use of it. The package-less addons will be imported file by file with the package structure ensured by dummy empty modules. This also changes module masks for addons to make the change work properly. --- pyanaconda/addons.py | 6 +++--- pyanaconda/ui/common.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/pyanaconda/addons.py b/pyanaconda/addons.py index 77792be99..7c517c674 100644 --- a/pyanaconda/addons.py +++ b/pyanaconda/addons.py @@ -50,15 +50,15 @@ def collect_addon_paths(toplevel_addon_paths, ui_subdir="gui"): for addon_id in directories: addon_ks_path = os.path.join(path, addon_id, "ks") if os.path.isdir(addon_ks_path): - module_paths["ks"].append(("pyanaconda.addon.%s.ks.%%s" % addon_id, addon_ks_path)) + module_paths["ks"].append(("%s.ks.%%s" % addon_id, addon_ks_path)) addon_spoke_path = os.path.join(path, addon_id, ui_subdir, "spokes") if os.path.isdir(addon_spoke_path): - module_paths["spokes"].append(("pyanaconda.addon.%s.spokes.%%s" % addon_id, addon_spoke_path)) + module_paths["spokes"].append(("%s.%s.spokes.%%s" % (addon_id, ui_subdir), addon_spoke_path)) addon_category_path = os.path.join(path, addon_id, ui_subdir, "categories") if os.path.isdir(addon_spoke_path): - module_paths["categories"].append(("pyanaconda.addon.%s.categories.%%s" % addon_id, addon_category_path)) + module_paths["categories"].append(("%s.%s.categories.%%s" % (addon_id, ui_subdir), addon_category_path)) return module_paths diff --git a/pyanaconda/ui/common.py b/pyanaconda/ui/common.py index c94861ed0..29c97fee1 100644 --- a/pyanaconda/ui/common.py +++ b/pyanaconda/ui/common.py @@ -25,6 +25,7 @@ import imp import inspect import copy import sys +import types class PathDict(dict): """Dictionary class supporting + operator""" @@ -500,8 +501,32 @@ def collect(module_pattern, path, pred): # do not load module if any module with the same name # is already imported if not module: - module = imp.load_module(module_pattern % mod_name, - fo, module_path, module_flags) + # try importing the module the standard way first + # uses sys.path and the module's full name! + try: + __import__(module_pattern % mod_name) + module = sys.modules[module_pattern % mod_name] + + # if it fails (package-less addon?) try importing single file + # and filling up the package structure voids + except ImportError: + # prepare dummy modules to prevent RuntimeWarnings + module_parts = (module_pattern % mod_name).split(".") + + # remove the last name as it will be inserted by the import + module_parts.pop() + + # make sure all "parent" modules are in sys.modules + for l in range(len(module_parts)): + module_part_name = ".".join(module_parts[:l+1]) + if module_part_name not in sys.modules: + module_part = types.ModuleType(module_part_name) + module_part.__path__ = [path] + sys.modules[module_part_name] = module_part + + # load the collected module + module = imp.load_module(module_pattern % mod_name, + fo, module_path, module_flags) # get the filenames without the extensions so we can compare those -- cgit