import importlib


def getClass(typ, name, init=None):
    """ Get an instance of an ADMIT class

        Parameters
        ----------
        typ : str
            package name, e.g. 'at' or 'bdp', 'admit.' wil be prepended to this.

        name : str
            The name of the class to retrieve, must have proper capitalization

        init : dict
            Initialization parameters. Default: None

        Returns
        -------
        An instance of the requested class
    """
    # prepend admit to the name
    path  = "admit." + typ
    # import the module
    module = importlib.import_module(path)
    # Normally, "import admit" has brought the class names
    # up to the top level...
    #print "##getClass(%s,%s) got module %s" % (typ,name,module)
    if hasattr(module,name):
        if init is None:
            return getattr(module, name)()

    # ...However, when running dtdGenerator, new __init__.py files
    # are created and imported, but not yet promoted to the top
    # level, so we must check the original "unpromoted" class path.
    else:
       path  = "admit." + typ + "." + name
       #print "##trying getClass(%s)"
       module = importlib.import_module(path)
       if init is None:
    # get the class with default parameters
           return getattr(module, name)()

    # if init != None in both cases
    # get the class with the given parameters
    return getattr(module, name)(**init)