changeset 3802:88b2eecd1fed

wikiutil.importPlugin: support getting whole plugin module object by giving function=None
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 28 Jun 2008 15:36:31 +0200
parents 7eb4a14108de
children 191ac1cf5f61 69e35e3ddfe6
files MoinMoin/wikiutil.py
diffstat 1 files changed, 25 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/wikiutil.py	Sat Jun 28 14:29:11 2008 +0200
+++ b/MoinMoin/wikiutil.py	Sat Jun 28 15:36:31 2008 +0200
@@ -1083,9 +1083,11 @@
 def importPlugin(cfg, kind, name, function="execute"):
     """ Import wiki or builtin plugin
 
-    Returns function from a plugin module name. If name can not be
-    imported, raise PluginMissingError. If function is missing, raise
-    PluginAttributeError.
+    Returns <function> attr from a plugin module <name>.
+    If <function> attr is missing, raise PluginAttributeError.
+    If <function> is None, return the whole module object.
+
+    If <name> plugin can not be imported, raise PluginMissingError.
 
     kind may be one of 'action', 'formatter', 'macro', 'parser' or any other
     directory that exist in MoinMoin or data/plugin.
@@ -1130,15 +1132,28 @@
 
 
 def importNameFromPlugin(moduleName, name):
-    """ Return name from plugin module
+    """ Return <name> attr from <moduleName> module,
+        raise PluginAttributeError if name does not exist.
 
-    Raise PluginAttributeError if name does not exist.
+        If name is None, return the <moduleName> module object.
     """
-    module = __import__(moduleName, globals(), {}, [name])
-    try:
-        return getattr(module, name)
-    except AttributeError:
-        raise PluginAttributeError
+    if name is None:
+        fromlist = []
+    else:
+        fromlist = [name]
+    module = __import__(moduleName, globals(), {}, fromlist)
+    if fromlist:
+        # module has the obj for module <moduleName>
+        try:
+            return getattr(module, name)
+        except AttributeError:
+            raise PluginAttributeError
+    else:
+       # module now has the toplevel module of <moduleName> (see __import__ docs!)
+       components = moduleName.split('.')
+       for comp in components[1:]:
+           module = getattr(module, comp)
+       return module
 
 
 def builtinPlugins(kind):