|  | 
|  | 1 | +""" | 
|  | 2 | +Automatic module importing utilities for dynamic class discovery. | 
|  | 3 | +
 | 
|  | 4 | +This module provides a mixin class for automatic module importing within a package, | 
|  | 5 | +enabling dynamic discovery of classes and implementations without explicit imports. | 
|  | 6 | +It is particularly useful for auto-registering classes in a registry pattern where | 
|  | 7 | +subclasses need to be discoverable at runtime. | 
|  | 8 | +
 | 
|  | 9 | +The AutoImporterMixin can be combined with registration mechanisms to create | 
|  | 10 | +extensible systems where new implementations are automatically discovered and | 
|  | 11 | +registered when they are placed in the correct package structure. | 
|  | 12 | +""" | 
|  | 13 | + | 
|  | 14 | +from __future__ import annotations | 
|  | 15 | + | 
|  | 16 | +import importlib | 
|  | 17 | +import pkgutil | 
|  | 18 | +import sys | 
|  | 19 | +from typing import ClassVar | 
|  | 20 | + | 
|  | 21 | +__all__ = ["AutoImporterMixin"] | 
|  | 22 | + | 
|  | 23 | + | 
|  | 24 | +class AutoImporterMixin: | 
|  | 25 | +    """ | 
|  | 26 | +    Mixin class for automatic module importing within packages. | 
|  | 27 | +
 | 
|  | 28 | +    This mixin enables dynamic discovery of classes and implementations without | 
|  | 29 | +    explicit imports by automatically importing all modules within specified | 
|  | 30 | +    packages. It is designed for use with class registration mechanisms to enable | 
|  | 31 | +    automatic discovery and registration of classes when they are placed in the | 
|  | 32 | +    correct package structure. | 
|  | 33 | +
 | 
|  | 34 | +    Example: | 
|  | 35 | +    :: | 
|  | 36 | +        from guidellm.utils import AutoImporterMixin | 
|  | 37 | +
 | 
|  | 38 | +        class MyRegistry(AutoImporterMixin): | 
|  | 39 | +            auto_package = "my_package.implementations" | 
|  | 40 | +
 | 
|  | 41 | +        MyRegistry.auto_import_package_modules() | 
|  | 42 | +
 | 
|  | 43 | +    :cvar auto_package: Package name or tuple of package names to import modules from | 
|  | 44 | +    :cvar auto_ignore_modules: Module names to ignore during import | 
|  | 45 | +    :cvar auto_imported_modules: List tracking which modules have been imported | 
|  | 46 | +    """ | 
|  | 47 | + | 
|  | 48 | +    auto_package: ClassVar[str | tuple[str, ...] | None] = None | 
|  | 49 | +    auto_ignore_modules: ClassVar[tuple[str, ...] | None] = None | 
|  | 50 | +    auto_imported_modules: ClassVar[list[str] | None] = None | 
|  | 51 | + | 
|  | 52 | +    @classmethod | 
|  | 53 | +    def auto_import_package_modules(cls) -> None: | 
|  | 54 | +        """ | 
|  | 55 | +        Automatically import all modules within the specified package(s). | 
|  | 56 | +
 | 
|  | 57 | +        Scans the package(s) defined in the `auto_package` class variable and imports | 
|  | 58 | +        all modules found, tracking them in `auto_imported_modules`. Skips packages | 
|  | 59 | +        (directories) and any modules listed in `auto_ignore_modules`. | 
|  | 60 | +
 | 
|  | 61 | +        :raises ValueError: If the `auto_package` class variable is not set | 
|  | 62 | +        """ | 
|  | 63 | +        if cls.auto_package is None: | 
|  | 64 | +            raise ValueError( | 
|  | 65 | +                "The class variable 'auto_package' must be set to the package name to " | 
|  | 66 | +                "import modules from." | 
|  | 67 | +            ) | 
|  | 68 | + | 
|  | 69 | +        cls.auto_imported_modules = [] | 
|  | 70 | +        packages = ( | 
|  | 71 | +            cls.auto_package | 
|  | 72 | +            if isinstance(cls.auto_package, tuple) | 
|  | 73 | +            else (cls.auto_package,) | 
|  | 74 | +        ) | 
|  | 75 | + | 
|  | 76 | +        for package_name in packages: | 
|  | 77 | +            package = importlib.import_module(package_name) | 
|  | 78 | + | 
|  | 79 | +            for _, module_name, is_pkg in pkgutil.walk_packages( | 
|  | 80 | +                package.__path__, package.__name__ + "." | 
|  | 81 | +            ): | 
|  | 82 | +                if ( | 
|  | 83 | +                    is_pkg | 
|  | 84 | +                    or ( | 
|  | 85 | +                        cls.auto_ignore_modules is not None | 
|  | 86 | +                        and module_name in cls.auto_ignore_modules | 
|  | 87 | +                    ) | 
|  | 88 | +                    or module_name in cls.auto_imported_modules | 
|  | 89 | +                ): | 
|  | 90 | +                    # Skip packages and ignored modules | 
|  | 91 | +                    continue | 
|  | 92 | + | 
|  | 93 | +                if module_name in sys.modules: | 
|  | 94 | +                    # Avoid circular imports | 
|  | 95 | +                    cls.auto_imported_modules.append(module_name) | 
|  | 96 | +                else: | 
|  | 97 | +                    importlib.import_module(module_name) | 
|  | 98 | +                    cls.auto_imported_modules.append(module_name) | 
0 commit comments