From 80ccf8b1c641fd6c41256fd28ea3b89c590e8d4c Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 17 Oct 2008 22:09:50 -0600 Subject: Reworked load_plugins so it doesn't use imp.load_module() to load from the plugins/ sub-packages, which previously caused them to be loaded multiple times when runnig the doctests --- ipalib/load_plugins.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/ipalib/load_plugins.py b/ipalib/load_plugins.py index 4352ac651..4e02f5bab 100644 --- a/ipalib/load_plugins.py +++ b/ipalib/load_plugins.py @@ -33,9 +33,9 @@ import imp import inspect -def load_plugins(src_dir): +def find_modules_in_dir(src_dir): """ - Import each Python module found in ``src_dir``. + Iterate through module names found in ``src_dir``. """ if not (path.abspath(src_dir) == src_dir and path.isdir(src_dir)): return @@ -51,23 +51,32 @@ def load_plugins(src_dir): module = name[:-len(suffix)] if module == '__init__': continue + yield module + + +def load_plugins_in_dir(src_dir): + """ + Import each Python module found in ``src_dir``. + """ + for module in find_modules_in_dir(src_dir): imp.load_module(module, *imp.find_module(module, [src_dir])) -def load_plugins_subpackage(file_in_package): +def import_plugins(name): """ - Load all Python modules found in a plugins/ subpackage. + Load all plugins found in standard 'plugins' sub-package. """ - package_dir = path.dirname(path.abspath(file_in_package)) - plugins_dir = path.join(package_dir, 'plugins') - load_plugins(plugins_dir) + try: + plugins = __import__(name + '.plugins').plugins + except ImportError: + return + src_dir = path.dirname(path.abspath(plugins.__file__)) + for name in find_modules_in_dir(src_dir): + full_name = '%s.%s' % (plugins.__name__, name) + __import__(full_name) -load_plugins_subpackage(__file__) -try: - import ipa_server - load_plugins_subpackage(ipa_server.__file__) -except ImportError: - pass +for name in ['ipalib', 'ipa_server', 'ipa_not_a_package']: + import_plugins(name) -load_plugins(path.expanduser('~/.freeipa')) +load_plugins_in_dir(path.expanduser('~/.freeipa')) -- cgit