Skip to content

runtime.plugin

EntryPointsPluginLoader

Bases: PluginLoader

Loads plugins from entry point groups

https://packaging.python.org/en/latest/specifications/entry-points/

Source code in /home/runner/work/arc/arc/arc/runtime/plugin.py
class EntryPointsPluginLoader(PluginLoader):
    """Loads plugins from entry point groups

    https://packaging.python.org/en/latest/specifications/entry-points/
    """

    def __init__(
        self,
        manager: PluginManager,
        locations: t.Iterable[str],
        filter: str,
    ) -> None:
        self.filter = filter
        super().__init__(manager, locations)

    def load(self) -> None:
        for entry_point in self.__get_entry_points(self.locations):
            plugin = entry_point.load()
            self.manager.register(entry_point.name, plugin)

    def __get_entry_points(
        self, locations: t.Iterable[str]
    ) -> t.Iterator[metadata.EntryPoint]:
        for location in locations:
            for entry_point in metadata.entry_points(**{self.filter: location}):
                yield entry_point  # type: ignore

PathPluginLoader

Bases: PluginLoader

Loads plugins from a a set of file paths

Source code in /home/runner/work/arc/arc/arc/runtime/plugin.py
class PathPluginLoader(PluginLoader):
    """Loads plugins from a a set of file paths"""

    def load(self) -> None:
        for path in self.__get_paths(self.locations):
            plugin = self.__load_plugin(path)

            if plugin:
                self.manager.register(str(path), plugin)

    def __get_paths(self, paths: t.Iterable[str]) -> t.Iterator[Path]:
        for filepath in paths:
            path = self.path(filepath)
            if not path:
                continue

            if path.name.startswith("__"):
                continue

            if path.is_dir():
                yield from self.__get_paths(path.iterdir())  # type: ignore
            else:
                yield path

    def __load_plugin(self, path: Path) -> Plugin | None:
        sys.path.append(str(path.parent))
        module = import_module(path.stem)
        return getattr(module, "plugin", None)

    @staticmethod
    def path(filepath: str) -> t.Optional[Path]:
        path = Path(filepath)
        path = path.expanduser().resolve()
        if path.exists():
            return path
        return None