Source code for mantidimaging.core.operations.loader

# Copyright (C) 2023 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later
from __future__ import annotations
import os
import pkgutil
import sys
from importlib.util import module_from_spec
from typing import List, TYPE_CHECKING, Type

    from mantidimaging.core.operations.base_filter import BaseFilter
    BaseFilterClass = Type[BaseFilter]

_OPERATION_MODULES_LIST: List[BaseFilterClass] = []

def _find_operation_modules() -> List[BaseFilterClass]:
    module_list: List[BaseFilterClass] = []
    for finder, module_name, ispkg in pkgutil.walk_packages([os.path.dirname(__file__)]):
        if not ispkg:

        if getattr(sys, 'frozen', False):
            # If we're running a PyInstaller executable then we need to use a full module path
            module_name = f'mantidimaging.core.operations.{module_name}'

        # near impossible to type check as find can be a pyinstaller specific type that we can't normally import
        spec = finder.find_spec(module_name)  # type: ignore[call-arg]

        assert spec is not None
        assert spec.loader is not None
        module = module_from_spec(spec)
        sys.modules[module_name] = module
        if hasattr(module, 'FILTER_CLASS'):

    return module_list

[docs]def load_filter_packages() -> List[BaseFilterClass]: """ Imports all subpackages with a FILTER_CLASS attribute, which should be an extension of BaseFilter. These classes are then used to provide the names, required inputs, and behaviour to execute then named filter on a stack of images. :param ignored_packages: List of ignore rules """ global _OPERATION_MODULES_LIST if not _OPERATION_MODULES_LIST: _OPERATION_MODULES_LIST = _find_operation_modules() return _OPERATION_MODULES_LIST