import os
import imp
import mpiimporter

from mpi4py import MPI

class Importer:
    def __init__(self, path=None):
        if path is not None and not os.path.isdir(path):
            raise ImportError
        self.path = path

    def find_module(self, fullname, path=None):
        rank = MPI.COMM_WORLD.Get_rank()
        #        print "[%d] find_module %s %s" % (rank, fullname, path)

        subname = fullname.split(".")[-1]
        if subname != fullname and self.path is None:
            return None
        if self.path is None:
            path = None
        else:
            path = [self.path]
        try:
            file, filename, stuff = mpiimporter.find_module(subname, path)
        except ImportError:
            #            print ImportError
            return None

        #        print "[%d] find_module found: %s %s" % (rank, file, filename)

        ignore, ext = os.path.splitext(filename)

        if ext == '.so':
            file, filename, stuff = imp.find_module(subname, path)
            return ImpLoader(file, filename, stuff)
        
        return Loader(file, filename, stuff)


class Loader:
    def __init__(self, file, filename, stuff):
        self.file = file
        self.filename = filename
        self.stuff = stuff

    def load_module(self, fullname):
        rank = MPI.COMM_WORLD.Get_rank()
        #        print "[%d] load_module: %s %s %s %s" % (rank, fullname, self.file, self.filename, self.stuff)
        mod = mpiimporter.load_module(fullname, self.file, self.filename, self.stuff)
        if self.file:
            self.file.close()
        mod.__loader__ = self  # for introspection

                               #        print "[%d] load_module loaded: %s" % (rank, mod)
        return mod

class ImpLoader:

    def __init__(self, file, filename, stuff):
        self.file = file
        self.filename = filename
        self.stuff = stuff

    def load_module(self, fullname):
        mod = imp.load_module(fullname, self.file, self.filename, self.stuff)
        if self.file:
            self.file.close()
        mod.__loader__ = self  # for introspection
        return mod

@@ -462,21 +462,16 @@
 ame and 
-self.
 path is 
@@ -504,107 +504,8 @@
 one%0A
-        if self.path is None:%0A            path = None%0A        else:%0A            path = %5Bself.path%5D%0A
     

fix to mpiimport so only find_module(path) is used