changeset 1520:563bfe02b04f

Removed a lot of ugly MimeType() calls, refactored it into a helper, fixed page rendering for missing parsers.
author Alexander Schremmer <alex AT alexanderweb DOT de>
date Sun, 03 Sep 2006 01:06:20 +0200
parents cc27f2c82cde
children ac7e2218a298
files MoinMoin/Page.py MoinMoin/formatter/__init__.py MoinMoin/formatter/text_python.py MoinMoin/parser/__init__.py MoinMoin/parser/text_moin_wiki.py MoinMoin/wikiutil.py
diffstat 6 files changed, 37 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/Page.py	Sun Sep 03 00:34:29 2006 +0200
+++ b/MoinMoin/Page.py	Sun Sep 03 01:06:20 2006 +0200
@@ -1052,16 +1052,8 @@
             from MoinMoin.formatter.text_html import Formatter
             self.formatter = Formatter(request, store_pagelinks=1)
         elif not self.formatter:
-            omt = wikiutil.MimeType(self.output_mimetype)
-            for module_name in omt.module_name():
-                try:
-                    Formatter = wikiutil.importPlugin(request.cfg, "formatter", module_name, "Formatter")
-                    self.formatter = Formatter(request)
-                    break
-                except wikiutil.PluginMissingError:
-                    pass
-            else:
-                raise NotImplementedError("Plugin missing error!") # XXX what now?
+            Formatter = wikiutil.searchAndImportPlugin(request.cfg, "formatter", self.output_mimetype)
+            self.formatter = Formatter(request)
         request.formatter = self.formatter
         self.formatter.setPage(self)
         if self.hilite_re:
@@ -1258,15 +1250,7 @@
                     request.write(''.join(pi_formtext))
 
         # Load the parser
-        mt = wikiutil.MimeType(self.pi_format)
-        for module_name in mt.module_name():
-            try:
-                Parser = wikiutil.importPlugin(self.request.cfg, "parser", module_name, "Parser")
-                break
-            except wikiutil.PluginMissingError:
-                pass
-        else:
-            raise NotImplementedError("No matching parser") # XXX what do we use if nothing at all matches?
+        Parser = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", self.pi_format)
             
         # start wiki content div
         request.write(self.formatter.startContent(content_id))
@@ -1350,15 +1334,7 @@
             self.getFormatterName() in self.cfg.caching_formats):
             # Everything is fine, now check the parser:
             if parser is None:
-                mt = wikiutil.MimeType(self.pi_format)
-                for module_name in mt.module_name():
-                    try:
-                        parser = wikiutil.importPlugin(self.request.cfg, "parser", module_name, "Parser")
-                        break
-                    except wikiutil.PluginMissingError:
-                        pass
-                else:
-                    raise NotImplementedError("no matching parser") # XXX what now?
+                parser = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", self.pi_format)
             return getattr(parser, 'caching', False)
         return False
 
--- a/MoinMoin/formatter/__init__.py	Sun Sep 03 00:34:29 2006 +0200
+++ b/MoinMoin/formatter/__init__.py	Sun Sep 03 01:06:20 2006 +0200
@@ -309,15 +309,8 @@
         """ parser_name MUST be valid!
             writes out the result instead of returning it!
         """
-        mt = wikiutil.MimeType(parser_name)
-        for module_name in mt.module_name():
-            try:
-                parser = wikiutil.importPlugin(self.request.cfg, "parser", module_name, "Parser")
-                break
-            except wikiutil.PluginMissingError:
-                pass
-        else:
-            raise "Parser not found" # XXX what now?
+        parser = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", parser_name)
+
         args = self._get_bang_args(lines[0])
         if args is not None:
             lines = lines[1:]
--- a/MoinMoin/formatter/text_python.py	Sun Sep 03 00:34:29 2006 +0200
+++ b/MoinMoin/formatter/text_python.py	Sun Sep 03 01:06:20 2006 +0200
@@ -177,15 +177,11 @@
         """ parser_name MUST be valid!
             prints out the result instead of returning it!
         """
-        mt = wikiutil.MimeType(parser_name)
-        for module_name in mt.module_name():
-            try:
-                Dependencies = wikiutil.importPlugin(self.request.cfg, "parser", module_name, "Dependencies")
-                break
-            except (wikiutil.PluginMissingError, wikiutil.PluginAttributeError), err:
-                pass
-        else:
+        try:
+            Dependencies = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", parser_name, "Dependencies")
+        except (wikiutil.PluginMissingError, wikiutil.PluginAttributeError):
             Dependencies = self.defaultDependencies
+
         if self.__is_static(Dependencies):
             return self.formatter.parser(parser_name, lines)
         else:
--- a/MoinMoin/parser/__init__.py	Sun Sep 03 00:34:29 2006 +0200
+++ b/MoinMoin/parser/__init__.py	Sun Sep 03 01:06:20 2006 +0200
@@ -8,6 +8,8 @@
     @copyright: 2000 by Jürgen Hermann <jh@web.de>
     @license: GNU GPL, see COPYING for details.
 """
+
 from MoinMoin.util import pysupport
+
 modules = pysupport.getPackageModules(__file__)
 
--- a/MoinMoin/parser/text_moin_wiki.py	Sun Sep 03 00:34:29 2006 +0200
+++ b/MoinMoin/parser/text_moin_wiki.py	Sun Sep 03 01:06:20 2006 +0200
@@ -785,7 +785,7 @@
             self.in_pre = 'no_parser'
             return self._closeP() + self.formatter.preformatted(1)
         elif s_word.startswith('#!'):
-            # First try to find a parser for this (will go away in 2.0)
+            # First try to find a parser for this
             parser_name = s_word[2:].split()[0]
             self.setParser(parser_name)
 
@@ -1111,14 +1111,8 @@
 
     def setParser(self, name):
         """ Set parser to parser named 'name' """
-        mt = wikiutil.MimeType(name)
-        self.parser = None
-        for module_name in mt.module_name():
-            try:
-                self.parser = wikiutil.importPlugin(self.request.cfg, "parser", module_name, "Parser")
-                break
-            except wikiutil.PluginMissingError:
-                pass
-        else:
-            raise "Parser not found!" # XXX what now?
-
+        # XXX this is done by the formatter as well
+        try:
+            self.parser = wikiutil.searchAndImportPlugin(self.request.cfg, "parser", name)
+        except wikiutil.PluginMissingError:
+            self.parser = None
--- a/MoinMoin/wikiutil.py	Sun Sep 03 00:34:29 2006 +0200
+++ b/MoinMoin/wikiutil.py	Sun Sep 03 01:06:20 2006 +0200
@@ -1114,6 +1114,25 @@
     return all_plugins
 
 
+def searchAndImportPlugin(cfg, type, name, what=None):
+    type2classname = {"parser": "Parser",
+                      "formatter": "Formatter",
+    }
+    if what is None:
+        what = type2classname[type]
+    mt = MimeType(name)
+    plugin = None
+    for module_name in mt.module_name():
+        try:
+            plugin = importPlugin(cfg, type, module_name, what)
+            break
+        except PluginMissingError:
+            pass
+    else:
+        raise PluginMissingError("Plugin not found!")
+    return plugin
+
+
 #############################################################################
 ### Parsers
 #############################################################################