def _import_hook(self, fqname, globals=None, locals=None, fromlist=None,
                           level=-1):
        """Python calls this hook to locate and import a module."""

        parts = fqname.split('.')

        #print "_import_hook", parts
        # pyjamas-gtk hack
        if parts[0] in ['gtk', 'gdk', 'pygtk', 'gobject']:
            parts = ['pygtkweb'] + parts

        # determine the context of this import
        parent = self._determine_import_context(globals)

        # if there is a parent, then its importer should manage this import
        if parent:
            module = parent.__importer__._do_import(parent, parts, fromlist)
            if module:
                return module

        # has the top module already been imported?
        try:
            top_module = sys.modules[parts[0]]
        except __HOLE__:

            # look for the topmost module
            top_module = self._import_top_module(parts[0])
            if not top_module:
                # the topmost module wasn't found at all.
                # try previous importer.
                return self.previous_importer(fqname, globals, locals, fromlist, level)
                #raise ImportError, 'No module named ' + fqname

        # fast-path simple imports
        if len(parts) == 1:
            if not fromlist:
                return top_module

            if not top_module.__dict__.get('__ispkg__'):
                # __ispkg__ isn't defined (the module was not imported by us),
                # or it is zero.
                #
                # In the former case, there is no way that we could import
                # sub-modules that occur in the fromlist (but we can't raise an
                # error because it may just be names) because we don't know how
                # to deal with packages that were imported by other systems.
                #
                # In the latter case (__ispkg__ == 0), there can't be any sub-
                # modules present, so we can just return.
                #
                # In both cases, since len(parts) == 1, the top_module is also
                # the "bottom" which is the defined return when a fromlist
                # exists.
                return top_module

        importer = top_module.__dict__.get('__importer__')
        if importer:
            return importer._finish_import(top_module, parts[1:], fromlist)

        # Grrr, some people "import os.path" or do "from os.path import ..."
        if len(parts) == 2 and hasattr(top_module, parts[1]):
            if fromlist:
                return getattr(top_module, parts[1])
            else:
                return top_module

        # assume that the module has already been imported,
        # walk from top_module to find it.
        mod = top_module

        for k in parts[1:]:
            if not hasattr(mod, k):
                #print "no mod", mod, k, parts
                mod = None
                break
            mod = getattr(mod, k)

        if mod:
            return mod

        # ok, pass through to previous importer
        return self.previous_importer(fqname, globals, locals, fromlist, level)

        # If the importer does not exist, then we have to bail. A missing
        # importer means that something else imported the module, and we have
        # no knowledge of how to get sub-modules out of the thing.
        raise ImportError, 'No module named ' + fqname
dataset/ETHPy150Open anandology/pyjamas/pyjd/imputil.py/ImportManager._import_hook