import importlib
from injector.models import LSP


def __create__(lsp):
    clazz_name = lsp.clazz_name
    module_name = lsp.alias.lower()
    module_path = 'domain.lsp.{}_domain'.format(module_name)
    module = importlib.import_module(module_path)

    clazz = getattr(module, clazz_name)
    injector = clazz()
    return injector


def create_by_id(lsp_pk):
    lsp = LSP.objects.filter(pk=lsp_pk).get()
    return __create__(lsp)


# 通过alias从domain/lsp目录下加载对应的模块，并创建实例
def create_by_alias(alias: str):
    lsp = LSP.objects.filter(alias__iexact=alias).get()
    return __create__(lsp)




