##
# Copyright (c) 2008-2011 Sprymix Inc.
# All rights reserved.
#
# See LICENSE for details.
##


import imp
import importlib
import sys

from . import module as module_types
from .context import ImportContext

from .finder import install, update_finders


def reload(module):
    if isinstance(module, module_types.BaseProxyModule):
        sys.modules[module.__name__] = module.__wrapped__

        # XXX: imp.reload has a hardcoded check that fails on instances of module subclasses
        try:
            new_mod = module.__wrapped__.__loader__.load_module(module.__name__)

            if isinstance(new_mod, module_types.BaseProxyModule):
                module.__wrapped__ = new_mod.__wrapped__
            else:
                module.__wrapped__ = new_mod

        finally:
            sys.modules[module.__name__] = module

        return module

    else:
        return imp.reload(module)


class ObjectImportError(Exception):
    pass


def get_object(cls):
    modname, _, name = cls.rpartition('.')

    try:
        mod = importlib.import_module(modname)
    except ImportError as e:
        raise ObjectImportError('could not load object %s' % cls) from e
    else:
        try:
            result = getattr(mod, name)
        except AttributeError as e:
            raise ObjectImportError('could not load object %s' % cls) from e

        return result

@@ -20,17 +20,17 @@
 2008-201
-1
+2
  Sprymix
@@ -120,16 +120,26 @@
 portlib%0A
+import os%0A
 import s
@@ -865,32 +865,32 @@
 dule%0A%0A    else:%0A
-
         return i
@@ -909,16 +909,1778 @@
 dule)%0A%0A%0A
+def modules_from_import_statements(package, imports):%0A    modules = %5B%5D%0A%0A    for name, fromlist in imports:%0A        level = 0%0A%0A        while level %3C len(name) and name%5Blevel%5D == '.':%0A            level += 1%0A%0A        if level %3E 0:%0A            steps = package.rsplit('.', level - 1)%0A            if len(steps) %3C level:%0A                raise ValueError('relative import reaches beyond top-level package')%0A%0A            suffix = name%5Blevel:%5D%0A%0A            if suffix:%0A                fq_name = '%7B%7D.%7B%7D'.format(steps%5B0%5D, name%5Blevel:%5D)%0A            else:%0A                fq_name = steps%5B0%5D%0A        else:%0A            fq_name = name%0A%0A        path = None%0A        steps = fq_name.split('.')%0A%0A        add_package = True%0A%0A        for i in range(len(steps)):%0A            modname = '.'.join(steps%5B:i + 1%5D)%0A%0A            loader = importlib.find_loader(modname, path=path)%0A%0A            if loader is None:%0A                raise ValueError('could not find loader for module %7B%7D'.format(modname))%0A%0A            if not loader.is_package(modname):%0A                break%0A%0A            modfile = loader.get_filename(modname)%0A            # os.path.dirname(__file__) is a common importlib assumption for __path__%0A            path = %5Bos.path.dirname(modfile)%5D%0A        else:%0A            if fromlist:%0A                add_package = False%0A%0A                for entry in fromlist:%0A                    modname = '%7B%7D.%7B%7D'.format(fq_name, entry)%0A                    entry_loader = importlib.find_loader(modname, path=path)%0A%0A                    if entry_loader is not None and entry_loader.path != loader.path:%0A                        modules.append(modname)%0A                    else:%0A                        add_package = True%0A%0A        if add_package:%0A            modules.append(fq_name)%0A%0A    return modules%0A%0A%0A
 class Ob

Add modules_from_import_statements() helper