from typing import List
import importlib


def instanciate_classes(modules: dict, module_root) -> List:
    """
    Creates sources based on the config file where each key is a module and each subkey is a class
    Each class can also have custom configurations when creating an instance
    :param modules: dictionary with modules/classes to create
    :return: list of instances of classes
    """
    classes_instantiated = []

    for module in modules.keys():

        # import python module
        pmodule = importlib.import_module(f'{module_root}.{module}')

        # each source in the module may have configurations
        for source, params in modules[module].items():

            # create a class from the module
            class_ = getattr(pmodule, source)

            if params:
                # specific module config
                instance = class_(**params)
            else:
                instance = class_()

            classes_instantiated.append(instance)

    return classes_instantiated