changeset 53:699811601bed

merge moin--main--1.3--patch-934,935: detect correctly missing plguins and missing names in plugins Patches applied: * arch@arch.thinkmo.de--2003-archives/moin--main--1.3--patch-934 detect correctly missing plguins and missing names in plugins * arch@arch.thinkmo.de--2003-archives/moin--main--1.3--patch-935 updated CHANGES * nirs@freeshell.org--2005/moin--fix--1.3--patch-60 detect correctly missing plguins and missing names in plugins imported from: moin--main--1.5--patch-54
author Nir Soffer <nirs@freeshell.org>
date Thu, 29 Sep 2005 00:50:30 +0000
parents 80b3d4e891ff
children 34fdc0926460
files ChangeLog MoinMoin/Page.py MoinMoin/formatter/text_python.py MoinMoin/parser/wiki.py MoinMoin/request.py MoinMoin/wikiaction.py MoinMoin/wikimacro.py MoinMoin/wikirpc.py MoinMoin/wikiutil.py docs/CHANGES
diffstat 10 files changed, 105 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Sep 29 00:39:21 2005 +0000
+++ b/ChangeLog	Thu Sep 29 00:50:30 2005 +0000
@@ -2,6 +2,41 @@
 # arch-tag: automatic-ChangeLog--arch@arch.thinkmo.de--2003-archives/moin--main--1.3
 #
 
+2005-09-21 20:35:00 GMT	Nir Soffer <nirs@freeshell.org>	patch-935
+
+    Summary:
+      updated CHANGES
+    Revision:
+      moin--main--1.3--patch-935
+
+
+    modified files:
+     ChangeLog docs/CHANGES
+
+
+2005-09-21 20:13:04 GMT	Nir Soffer <nirs@freeshell.org>	patch-934
+
+    Summary:
+      detect correctly missing plguins and missing names in plugins
+    Revision:
+      moin--main--1.3--patch-934
+
+    Patches applied:
+    
+     * nirs@freeshell.org--2005/moin--fix--1.3--patch-60
+       detect correctly missing plguins and missing names in plugins
+    
+
+    modified files:
+     ChangeLog MoinMoin/Page.py MoinMoin/formatter/text_python.py
+     MoinMoin/parser/wiki.py MoinMoin/request.py
+     MoinMoin/wikiaction.py MoinMoin/wikimacro.py
+     MoinMoin/wikirpc.py MoinMoin/wikiutil.py
+
+    new patches:
+     nirs@freeshell.org--2005/moin--fix--1.3--patch-60
+
+
 2005-09-14 20:37:48 GMT	Nir Soffer <nirs@freeshell.org>	patch-933
 
     Summary:
--- a/MoinMoin/Page.py	Thu Sep 29 00:39:21 2005 +0000
+++ b/MoinMoin/Page.py	Thu Sep 29 00:50:30 2005 +0000
@@ -1178,7 +1178,7 @@
         try:
             Parser = wikiutil.importPlugin(self.request.cfg, "parser", 
                                            self.pi_format, "Parser")
-        except ImportError:
+        except wikiutil.PluginMissingError:
             from MoinMoin.parser.plain import Parser
 
         # start wiki content div
@@ -1263,7 +1263,7 @@
                 try:
                     parser = wikiutil.importPlugin(self.request.cfg, "parser",
                                                    self.pi_format, "Parser")
-                except ImportError:
+                except wikiutil.PluginMissingError:
                     pass
             return getattr(parser, 'caching', False)
         return False
--- a/MoinMoin/formatter/text_python.py	Thu Sep 29 00:39:21 2005 +0000
+++ b/MoinMoin/formatter/text_python.py	Thu Sep 29 00:50:30 2005 +0000
@@ -185,7 +185,7 @@
             Dependencies = wikiutil.importPlugin(self.request.cfg, type,
                                                  processor_name,
                                                  "Dependencies")
-        except AttributeError:
+        except wikiutil.PluginAttributeError:
             Dependencies = self.defaultDependencies
         if self.__is_static(Dependencies):
             return self.formatter.processor(processor_name, lines, is_parser)
--- a/MoinMoin/parser/wiki.py	Thu Sep 29 00:39:21 2005 +0000
+++ b/MoinMoin/parser/wiki.py	Thu Sep 29 00:50:30 2005 +0000
@@ -1086,10 +1086,10 @@
             self.processor = wikiutil.importPlugin(cfg, "processor", name,
                                                    "process")
             self.processor_is_parser = 0
-        except ImportError:
+        except wikiutil.PluginMissingError:
             try:
                 self.processor = wikiutil.importPlugin(cfg, "parser", name,
                                                    "Parser")
                 self.processor_is_parser = 1
-            except ImportError:
+            except wikiutil.PluginMissingError:
                 self.processor = None
--- a/MoinMoin/request.py	Thu Sep 29 00:39:21 2005 +0000
+++ b/MoinMoin/request.py	Thu Sep 29 00:50:30 2005 +0000
@@ -480,12 +480,12 @@
         try:
             Theme = wikiutil.importPlugin(self.cfg, 'theme', theme_name,
                                           'Theme')
-        except ImportError:
+        except wikiutil.PluginMissingError:
             fallback = 1
             try:
                 Theme = wikiutil.importPlugin(self.cfg, 'theme',
                                               self.cfg.theme_default, 'Theme')
-            except ImportError:
+            except wikiutil.PluginMissingError:
                 fallback = 2
                 from MoinMoin.theme.modern import Theme
         
--- a/MoinMoin/wikiaction.py	Thu Sep 29 00:39:21 2005 +0000
+++ b/MoinMoin/wikiaction.py	Thu Sep 29 00:50:30 2005 +0000
@@ -834,7 +834,7 @@
     try:
         Formatter = wikiutil.importPlugin(request.cfg, "formatter",
                                           formatterName, "Formatter")
-    except ImportError:
+    except wikiutil.PluginMissingError:
         # default to plain text formatter
         mimetype = "text/plain"
         from MoinMoin.formatter.text_plain import Formatter
@@ -954,7 +954,7 @@
     try:
         handler = wikiutil.importPlugin(request.cfg, "action", action,
                                         identifier)
-    except ImportError:
+    except wikiutil.PluginMissingError:
         handler = globals().get('do_' + action)
         
     return handler
--- a/MoinMoin/wikimacro.py	Thu Sep 29 00:39:21 2005 +0000
+++ b/MoinMoin/wikimacro.py	Thu Sep 29 00:50:30 2005 +0000
@@ -108,7 +108,7 @@
         self.name = macro_name
         try:
             execute = wikiutil.importPlugin(self.cfg, 'macro', macro_name)
-        except ImportError:
+        except wikiutil.PluginMissingError:
             try:
                 builtins = self.__class__
                 execute = getattr(builtins, '_macro_' + macro_name)
@@ -116,7 +116,7 @@
                 if macro_name in i18n.languages:
                     execute = builtins._m_lang
                 else:
-                    raise ImportError("Cannot load macro %s" % macro_name)        
+                    raise ImportError("Cannot load macro %s" % macro_name)
         return execute(self, args)
 
     def _m_lang(self, text):
@@ -140,7 +140,7 @@
         try:
             return wikiutil.importPlugin(self.request.cfg, 'macro',
                                          macro_name, 'Dependencies')
-        except (ImportError, AttributeError):
+        except wikiutil.PluginError:
             return self.defaultDependency
 
     def _macro_TitleSearch(self, args):
--- a/MoinMoin/wikirpc.py	Thu Sep 29 00:39:21 2005 +0000
+++ b/MoinMoin/wikirpc.py	Thu Sep 29 00:50:30 2005 +0000
@@ -411,7 +411,7 @@
                 try:
                     fn = wikiutil.importPlugin(self.request.cfg, 'xmlrpc',
                                                method, 'execute')
-                except ImportError:
+                except wikiutil.PluginMissingError:
                     response = xmlrpclib.Fault(1, "No such method: %s." %
                                                method)
                 else:
--- a/MoinMoin/wikiutil.py	Thu Sep 29 00:39:21 2005 +0000
+++ b/MoinMoin/wikiutil.py	Thu Sep 29 00:50:30 2005 +0000
@@ -565,74 +565,76 @@
 ### Plugins
 #############################################################################
 
+class PluginError(Exception):
+    """ Base class for plugin errors """
+
+class PluginMissingError(PluginError):
+    """ Raised when a plugin is not found """
+
+class PluginAttributeError(PluginError):
+    """ Raised when plugin does not contain an attribtue """
+
+
 def importPlugin(cfg, kind, name, function="execute"):
     """ Import wiki or builtin plugin
     
-    Returns function from a plugin module. If the module can not be
-    imported, raise ImportError. If function is not there, raise
-    AttributeError.
+    Returns function from a plugin module name. If name can not be
+    imported, raise PluginMissingError. If function is missing, raise
+    PluginAttributeError.
 
     kind may be one of 'action', 'formatter', 'macro', 'processor',
     'parser' or any other directory that exist in MoinMoin or
     data/plugin
 
     Wiki plugins will always override builtin plugins. If you want
-    specific plugin, use either importWikiPlugin or importName directly.
+    specific plugin, use either importWikiPlugin or importBuiltinPlugin
+    directly.
     
     @param cfg: wiki config instance
     @param kind: what kind of module we want to import
     @param name: the name of the module
     @param function: the function name
-    @rtype: callable
+    @rtype: any object
     @return: "function" of module "name" of kind "kind", or None
     """
     try:
-        plugin = importWikiPlugin(cfg, kind, name, function)
-    except ImportError:
-        modulename = 'MoinMoin.%s.%s' % (kind, name)
-        plugin = pysupport.importName(modulename, function)
-    return plugin
+        return importWikiPlugin(cfg, kind, name, function)
+    except PluginMissingError:
+        return importBuiltinPlugin(kind, name, function)
+
 
 def importWikiPlugin(cfg, kind, name, function):
-    """ Import and cache plugin from the wiki data directory
+    """ Import plugin from the wiki data directory
     
-    Returns function from a plugin module. If the module can not be
-    imported, raise ImportError. If function is not there, raise
-    AttributeError.
+    See importPlugin docstring.
+    """
+    if not name in wikiPlugins(kind, cfg):
+        raise PluginMissingError
+    moduleName = '%s.plugin.%s.%s' % (cfg.siteid, kind, name)
+    return importNameFromPlugin(moduleName, function)
 
-    We try to import only ONCE - then cache the plugin, even if we got
-    None. This way we prevent expensive import of existing plugins for
-    each call to a plugin.
 
-    @param cfg: wiki config instance
-    @param kind: what kind of module we want to import
-    @param name: the name of the module
-    @param function: the function name
-    @rtype: callable
-    @return: "function" of module "name" of kind "kind"
+def importBuiltinPlugin(kind, name, function):
+    """ Import builtin plugin from MoinMoin package 
+    
+    See importPlugin docstring.
     """
+    if not name in builtinPlugins(kind):
+        raise PluginMissingError
+    moduleName = 'MoinMoin.%s.%s' % (kind, name)
+    return importNameFromPlugin(moduleName, function)
+
+
+def importNameFromPlugin(moduleName, name):
+    """ Return name from plugin module 
+    
+    Raise PluginAttributeError if name does not exists.
+    """
+    module = __import__(moduleName, globals(), {}, [name])
     try:
-        wikiPlugins = cfg._wiki_plugins
+        return getattr(module, name)
     except AttributeError:
-        wikiPlugins = cfg._wiki_plugins = {}
-        
-    # Wiki plugins are located under 'wikiconfigname.plugin' module.
-    modulename = '%s.plugin.%s.%s' % (cfg.siteid, kind, name)
-    # Try cache or import once from disk
-    try:
-        module = wikiPlugins[modulename]
-    except KeyError:
-        try:
-            module = __import__(modulename, globals(), {}, ['dummy'])
-        except ImportError:
-            module = wikiPlugins[modulename] = None
-    if module is None:
-        raise ImportError
-    return getattr(module, function)
-
-# If we use threads, make this function thread safe
-if config.use_threads:
-    importWikiPlugin = pysupport.makeThreadSafe(importWikiPlugin)
+        raise PluginAttributeError
 
 
 def builtinPlugins(kind):
@@ -702,7 +704,7 @@
         for pname in getPlugins('parser', cfg):
             try:
                 Parser = importPlugin(cfg, 'parser', pname, 'Parser')
-            except ImportError:
+            except wikiutil.PluginMissingError:
                 continue
             if hasattr(Parser, 'extensions'):
                 exts = Parser.extensions
--- a/docs/CHANGES	Thu Sep 29 00:39:21 2005 +0000
+++ b/docs/CHANGES	Thu Sep 29 00:50:30 2005 +0000
@@ -153,12 +153,17 @@
 
 Developer notes:
     
-    * Plugin API was improved. When plugin module is missing, an
-      ImportError is raised. When trying to import a missing name from a
-      plugin module, an AttributeError is raised. You must update any
-      code that use wikiutil.importPlugin or util.pysupport.importName.
-      Errors in your plugin should raise now correct tracebacks.
-      See http://moinmoin.wikiwikiweb.de/ErrorHandlingInPlugins
+    * Plugin API was improved. When plugin module is missing,
+      wikiutil.PluginMissingError is raised. When trying to import a
+      missing name from a plugin module, wikiutil.PluginMissingError is
+      raised. You must update any code that use wikiutil.importPlugin.
+      Errors in your plugin should raise now correct tracebacks. See
+      http://moinmoin.wikiwikiweb.de/ErrorHandlingInPlugins
+    * pysupport.importName was changed, it does not check for any
+      errors when trying to import a name from a module. The calling
+      code should check for ImportError or AttributeError. Previous
+      code used to hide all errors behind None.
+
 
   Internal Changes:
     * request.formatter (html) is available for actions now