changeset 224:3eb5e34a9757

move package modules loader to util.pysupport, cleanup imports, docstrings
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 09 May 2011 20:41:09 +0200
parents 46dc442dabb3
children bac248bcf98c
files MoinMoin/converter/__init__.py MoinMoin/util/pysupport.py
diffstat 2 files changed, 42 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/converter/__init__.py	Mon May 09 19:14:29 2011 +0200
+++ b/MoinMoin/converter/__init__.py	Mon May 09 20:41:09 2011 +0200
@@ -20,6 +20,7 @@
 
 
 from ..util.registry import RegistryBase
+from ..util.pysupport import load_package_modules
 
 
 class RegistryConverter(RegistryBase):
@@ -76,30 +77,6 @@
         return self._register(self.Entry(factory, type_input, type_output, priority))
 
 
-# TODO: Move somewhere else. Also how to do that for per-wiki modules?
-def _load():
-    import imp, os, sys
-    for path in __path__:
-        for root, dirs, files in os.walk(path):
-            del dirs[:]
-            for file in files:
-                if file.startswith('_') or not file.endswith('.py'):
-                    continue
-                module = file[:-3]
-                module_complete = __name__ + '.' + module
-                if module_complete in sys.modules:
-                    continue
-                info = imp.find_module(module, [root])
-                try:
-                    try:
-                        imp.load_module(module_complete, *info)
-                    except Exception as e:
-                        import MoinMoin.log as logging
-                        logger = logging.getLogger(__name__)
-                        logger.exception("Failed to import converter package %s: %s" % (module, e))
-                finally:
-                    info[0].close()
+default_registry = RegistryConverter()
+load_package_modules(__name__, __path__)
 
-default_registry = RegistryConverter()
-_load()
-
--- a/MoinMoin/util/pysupport.py	Mon May 09 19:14:29 2011 +0200
+++ b/MoinMoin/util/pysupport.py	Mon May 09 20:41:09 2011 +0200
@@ -1,15 +1,53 @@
 # Copyright: 2002 Juergen Hermann <jh@web.de>
+# Copyright: 2008 MoinMoin:BastianBlank
 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
 
 """
-    MoinMoin - Supporting function for Python magic
+MoinMoin - Supporting functions for Python magic
 """
 
+import os, sys
+import re
+import imp
+
 
 #############################################################################
 ### Module import / Plugins
 #############################################################################
 
+def load_package_modules(package_name, package_pathes):
+    """
+    Load (import) all modules from some package (except those starting with _).
+
+    This is useful if there is some code in the module that runs at import time
+    and registers some code of that module somewhere.
+
+    Call this from __init__.py of the same package like this:
+
+        load_package_modules(__name__, __path__)
+    """
+    for path in package_pathes:
+        for root, dirs, files in os.walk(path):
+            del dirs[:]
+            for fname in files:
+                if fname.startswith('_') or not fname.endswith('.py'):
+                    continue
+                module = fname[:-3]
+                module_complete = package_name + '.' + module
+                if module_complete in sys.modules:
+                    continue
+                info = imp.find_module(module, [root])
+                try:
+                    try:
+                        imp.load_module(module_complete, *info)
+                    except Exception as e:
+                        import MoinMoin.log as logging
+                        logger = logging.getLogger(package_name)
+                        logger.exception("Failed to import %s package module %s: %s" % (package_name, module, e))
+                finally:
+                    info[0].close()
+
+
 def isImportable(module):
     """ Check whether a certain module is available.
     """
@@ -25,8 +63,6 @@
     Return a list of plugin modules for a given plugin package dir,
     omitting any that start with an underscore.
     """
-    import os, re
-
     pyre = re.compile(r"^([^_].*)\.py$")
     dirlist = os.listdir(packagedir)
     matches = [pyre.match(fn) for fn in dirlist]
@@ -39,8 +75,6 @@
     """ Return a list of modules for a package, omitting any modules
         starting with an underscore.
     """
-    import os, re
-
     packagedir = os.path.dirname(packagefile)
 
     in_plugin_dir = lambda dir, ops=os.path.split: ops(ops(dir)[0])[1] == "plugin"