Skip to content

Commit

Permalink
first prototype for a plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
schlegelp committed Apr 16, 2024
1 parent 15e2995 commit 59dc5e2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
6 changes: 5 additions & 1 deletion octarine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from . import utils
from .viewer import *
from .conversion import register_converter, get_converter
from .conversion import register_converter, get_converter
from .plugins import register_plugins


register_plugins()
5 changes: 3 additions & 2 deletions octarine/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ def register_converter(t, converter, insert='first'):
Parameters
----------
t : type | hashable | callable
Data type to register the converter for. If a function
Data type the converter is meant to convert. If a function
it is expected to take a single argument `x` and return
True if `x` can be converted using `converter`.
True if `x` can be converted using `converter` and False
if not.
converter : callable
Function that converts `x` to pygfx visuals. Must accept
at least a single argument and return either a single
Expand Down
42 changes: 42 additions & 0 deletions octarine/plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import importlib

from importlib_metadata import entry_points

registered_plugins = []


def register_plugins():
"""Register a plugin."""
# Find all plugins that defined an entry point
discovered_plugins = entry_points(group="octarine.plugins")

# Go over each of the plugins
for plugin in discovered_plugins:
# Import the module
try:
module = importlib.import_module(plugin.module)
except BaseException as e:
print(f"Error importing plugin {plugin.name}: {e}")
continue

# Get the function to register the plugin
register_func = getattr(module, plugin.value.split(":")[-1], None)

# If the function is not found, print an error
if register_func is None:
print(
f"Registration function {plugin.value.split(':')[-1]} not found for plugin {plugin.name}."
)
continue

# Otherwise, register the plugin
try:
register_func()
except BaseException as e:
print(f"Error registering plugin {plugin.name}: {e}")

# Add the plugin to the list of registered plugins
registered_plugins.append(plugin.name)


register_plugins()

0 comments on commit 59dc5e2

Please sign in to comment.