示例#1
0
def load_class(class_name: str, module_name: str):
    if class_name is None:
        raise TypeError("class_name must not be None")
    if not isinstance(class_name, str):
        raise TypeError("class_name must be a string")
    if module_name is None:
        raise TypeError("module_name must not be None")
    if not isinstance(module_name, str):
        raise TypeError("module_name must be a string")
    try:
        verify_dynamic_loading_support(module_name=module_name)
    except FileNotFoundError:
        raise PluginModuleNotFoundError(module_name)

    module_obj: Optional[ModuleType] = import_library_module(
        module_name=module_name)

    if module_obj is None:
        raise PluginModuleNotFoundError(module_name)
    try:
        klass_ = getattr(module_obj, class_name)
    except AttributeError:
        raise PluginClassNotFoundError(module_name=module_name,
                                       class_name=class_name)

    return klass_
示例#2
0
def load_class(class_name, module_name):
    """Dynamically load a class from strings or raise a helpful error."""
    try:
        loaded_module = importlib.import_module(module_name)
        class_ = getattr(loaded_module, class_name)
    except ModuleNotFoundError:
        raise PluginModuleNotFoundError(module_name)
    except AttributeError:
        raise PluginClassNotFoundError(module_name=module_name, class_name=class_name)
    return class_
示例#3
0
def load_class(class_name, module_name):
    try:
        verify_dynamic_loading_support(module_name=module_name)
    except FileNotFoundError:
        raise PluginModuleNotFoundError(module_name)

    module_obj: Union[ModuleType, None] = import_library_module(module_name=module_name)

    if module_obj is None:
        raise PluginModuleNotFoundError(module_name)
    try:
        klass_ = getattr(module_obj, class_name)
    except AttributeError:
        raise PluginClassNotFoundError(module_name=module_name, class_name=class_name)

    return klass_
示例#4
0
def load_class(class_name, module_name):
    """Dynamically load a class from strings or raise a helpful error."""

    # TODO remove this nasty python 2 hack
    try:
        ModuleNotFoundError
    except NameError:
        ModuleNotFoundError = ImportError

    try:
        loaded_module = importlib.import_module(module_name)
        class_ = getattr(loaded_module, class_name)
    except ModuleNotFoundError as e:
        raise PluginModuleNotFoundError(module_name=module_name)
    except AttributeError as e:
        raise PluginClassNotFoundError(module_name=module_name,
                                       class_name=class_name)
    return class_