Rewritten plugin attacher for new system
This commit is contained in:
parent
6d464349b7
commit
e1b021583b
1 changed files with 35 additions and 12 deletions
|
|
@ -9,7 +9,7 @@ import numpy as np
|
||||||
from openflexure_stage import OpenFlexureStage
|
from openflexure_stage import OpenFlexureStage
|
||||||
from .camera.pi import StreamingCamera
|
from .camera.pi import StreamingCamera
|
||||||
|
|
||||||
from .plugins import PluginMount, load_plugin, search_plugin_dirs
|
from .plugins import PluginMount
|
||||||
from .config import load_config, save_config, convert_config
|
from .config import load_config, save_config, convert_config
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -22,16 +22,24 @@ class Microscope(object):
|
||||||
Args:
|
Args:
|
||||||
camera (:py:class:`openflexure_microscope.camera.pi.StreamingCamera`): camera object
|
camera (:py:class:`openflexure_microscope.camera.pi.StreamingCamera`): camera object
|
||||||
microscope (:py:class:`openflexure_stage.stage.OpenFlexureStage`): stage object
|
microscope (:py:class:`openflexure_stage.stage.OpenFlexureStage`): stage object
|
||||||
load_config (bool): Should a config file be automatically loaded on init?
|
config (dict): Dictionary of the runtime config to apply to the microscope. Does not automatically apply to camera and stage.
|
||||||
config_path (str): Path to the config YAML file to be loaded. If None, defaults to USER_CONFIG_PATH.
|
attach_plugins (bool): Should the microscope attach plugins listen in 'config' immediately.
|
||||||
"""
|
"""
|
||||||
def __init__(self, camera: StreamingCamera, stage: OpenFlexureStage):
|
def __init__(self, camera: StreamingCamera, stage: OpenFlexureStage, config: dict={}, attach_plugins: bool=True):
|
||||||
|
|
||||||
|
# Attach initial hardware (may be NoneTypes)
|
||||||
self.attach(camera, stage)
|
self.attach(camera, stage)
|
||||||
|
|
||||||
|
# Store entire runtime-config
|
||||||
|
self.config = config
|
||||||
|
|
||||||
# Create plugin mountpoint
|
# Create plugin mountpoint
|
||||||
self.plugin = PluginMount(self) #: :py:class:`openflexure_microscope.plugins.PluginMount`: Mounting point for all microscope plugins
|
self.plugin = PluginMount(self) #: :py:class:`openflexure_microscope.plugins.PluginMount`: Mounting point for all microscope plugins
|
||||||
|
|
||||||
|
# Attach plugins
|
||||||
|
if attach_plugins:
|
||||||
|
self.attach_plugins()
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
"""Create microscope on context enter."""
|
"""Create microscope on context enter."""
|
||||||
return self
|
return self
|
||||||
|
|
@ -75,22 +83,37 @@ class Microscope(object):
|
||||||
elif not self.stage:
|
elif not self.stage:
|
||||||
logging.info("Attached dummy stage.")
|
logging.info("Attached dummy stage.")
|
||||||
|
|
||||||
def find_plugins(self, plugin_paths=[], include_default=True):
|
def attach_plugin_maps(self, plugin_maps):
|
||||||
"""
|
"""
|
||||||
Automatically search for plugins, and attach to self.
|
Attach plugins from a list of plugin map strings
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
plugin_paths (list): List of strings of plugin directories.
|
plugin_maps (list): List of strings of plugin maps.
|
||||||
include_default (bool): Also load plugins from the module default directory (DEFAULT_PLUGIN_PATH)
|
|
||||||
"""
|
"""
|
||||||
logging.debug("Attaching plugins...")
|
logging.debug("Attaching plugins...")
|
||||||
plugins_list = search_plugin_dirs(plugin_paths, include_default=include_default)
|
|
||||||
|
|
||||||
for i in plugins_list:
|
for plugin_map in plugin_maps:
|
||||||
module = load_plugin(i)
|
self.plugin.attach(plugin_map)
|
||||||
self.plugin.attach(module)
|
|
||||||
logging.debug("Plugins attached.")
|
logging.debug("Plugins attached.")
|
||||||
|
|
||||||
|
def attach_plugins(self):
|
||||||
|
"""
|
||||||
|
Automatically search for plugin maps in self.config, and attach.
|
||||||
|
"""
|
||||||
|
if 'plugins' in self.config:
|
||||||
|
self.attach_plugin_maps(self.config['plugins'])
|
||||||
|
else:
|
||||||
|
logging.warning("No plugins specified in microscoperc.yaml. Skipping.")
|
||||||
|
|
||||||
|
def reload_plugins(self):
|
||||||
|
"""
|
||||||
|
Empty the plugin mount and re-attach from self.config.
|
||||||
|
"""
|
||||||
|
logging.info("Tearing down existing PluginMount...")
|
||||||
|
self.plugin = PluginMount(self)
|
||||||
|
logging.info("Repopulating PluginMount...")
|
||||||
|
self.attach_plugins()
|
||||||
|
|
||||||
# Create unified state
|
# Create unified state
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue