web/lib/django/utils/module_loading.py
changeset 29 cc9b7e14412b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/django/utils/module_loading.py	Tue May 25 02:43:45 2010 +0200
@@ -0,0 +1,60 @@
+import imp
+import os
+import sys
+
+
+def module_has_submodule(package, module_name):
+    """See if 'module' is in 'package'."""
+    name = ".".join([package.__name__, module_name])
+    if name in sys.modules:
+        return True
+    for finder in sys.meta_path:
+        if finder.find_module(name):
+            return True
+    for entry in package.__path__:  # No __path__, then not a package.
+        try:
+            # Try the cached finder.
+            finder = sys.path_importer_cache[entry]
+            if finder is None:
+                # Implicit import machinery should be used.
+                try:
+                    file_, _, _ = imp.find_module(module_name, [entry])
+                    if file_:
+                        file_.close()
+                    return True
+                except ImportError:
+                    continue
+            # Else see if the finder knows of a loader.
+            elif finder.find_module(name):
+                return True
+            else:
+                continue
+        except KeyError:
+            # No cached finder, so try and make one.
+            for hook in sys.path_hooks:
+                try:
+                    finder = hook(entry)
+                    # XXX Could cache in sys.path_importer_cache
+                    if finder.find_module(name):
+                        return True
+                    else:
+                        # Once a finder is found, stop the search.
+                        break
+                except ImportError:
+                    # Continue the search for a finder.
+                    continue
+            else:
+                # No finder found.
+                # Try the implicit import machinery if searching a directory.
+                if os.path.isdir(entry):
+                    try:
+                        file_, _, _ = imp.find_module(module_name, [entry])
+                        if file_:
+                            file_.close()
+                        return True
+                    except ImportError:
+                        pass
+                # XXX Could insert None or NullImporter
+    else:
+        # Exhausted the search, so the module cannot be found.
+        return False