def test_plugin_by_name(): BaseThing = plugin_by_name(base_registry.BaseThing) BasePlugin = plugin_by_name(base_registry.BasePlugin) from entry_points.base.thing import BaseThing as BaseThingImported from entry_points.plugins.base.plugin import BasePlugin as BasePluginImported assert BaseThing == BaseThingImported assert BasePlugin == BasePluginImported
from entry_points.base.discovery import plugin_by_name from entry_points.plugins.base import registry BasePlugin = plugin_by_name(registry.BasePlugin) class ReadyPlugin(BasePlugin): ready_methods = [] def is_ready(self): return all(method(self) for method in self.ready_methods)
from entry_points.base.discovery import plugin_by_name from entry_points.plugins.base import registry as base_registry from entry_points.plugins.ready import registry as ready_registry BaseThing = plugin_by_name(base_registry.BaseThing) ReadyPlugin = plugin_by_name(ready_registry.ReadyPlugin) class ReadyThing(BaseThing, ReadyPlugin): def always_true(self): return True ready_methods = [always_true] class UnreadyThing(BaseThing, ReadyPlugin): def always_false(self): return False ready_methods = [always_false]
def do_expensive_setup(self): self._expensive = plugin_by_name(expensive_registry.ExpensivePlugin)()
from entry_points.base.discovery import plugin_by_name from entry_points.plugins.base import registry as base_registry from entry_points.plugins.ready import registry as ready_registry from entry_points.plugins.health import registry as health_registry from entry_points.plugins.expensive import registry as expensive_registry BaseThing = plugin_by_name(base_registry.BaseThing) ReadyPlugin = plugin_by_name(ready_registry.ReadyPlugin) HealthPlugin = plugin_by_name(health_registry.HealthPlugin) class ImplementedThingExpensive(BaseThing, ReadyPlugin, HealthPlugin): """Example of how to dynamically import an expensive-to-import module using entry point plugins.""" def __init__(self): self._expensive = None def do_expensive_setup(self): self._expensive = plugin_by_name(expensive_registry.ExpensivePlugin)() def do_expensive_thing(self): self._expensive.do_expensive()
from entry_points.base.discovery import plugin_by_name from entry_points.plugins.base import registry as base_registry from entry_points.plugins.health import registry as health_registry BaseThing = plugin_by_name(base_registry.BaseThing) HealthPlugin = plugin_by_name(health_registry.HealthPlugin) class HealthyThing(BaseThing, HealthPlugin): def always_true(self): return True health_methods = [always_true] class UnhealthyThing(BaseThing, HealthPlugin): def always_false(self): return False health_methods = [always_false]