Renamed LabThing plugins to extensions

This commit is contained in:
jtc42 2020-01-05 18:56:46 +00:00
parent d96e188d16
commit 2a245185a6
10 changed files with 89 additions and 89 deletions

View file

@ -9,9 +9,9 @@ from openflexure_microscope.common.labthings_core.utilities import get_docstring
from openflexure_microscope.utilities import camel_to_snake, snake_to_spine
class BasePlugin:
class BaseExtension:
"""
Parent class for all plugins.
Parent class for all extensions.
Handles binding route views and forms.
"""
@ -23,7 +23,7 @@ class BasePlugin:
self._rules = {} # Key: Original rule. Val: View class
self._gui = None
self._cls = str(self) # String description of plugin instance
self._cls = str(self) # String description of extension instance
self.actions = []
self.properties = []
@ -43,7 +43,7 @@ class BasePlugin:
while cleaned_rule[0] == "/":
cleaned_rule = cleaned_rule[1:]
# Expand the rule to include plugin name
# Expand the rule to include extension name
full_rule = "/{}/{}".format(self._name_uri_safe, cleaned_rule)
view_id = cleaned_rule.replace("/", "_").replace("<", "").replace(">", "")
@ -119,20 +119,20 @@ class BasePlugin:
setattr(self, method_name, method)
else:
logging.warning(
"Unable to bind method to plugin. Method name already exists."
"Unable to bind method to extension. Method name already exists."
)
def find_plugins(plugin_path, module_name="plugins"):
logging.debug(f"Loading plugins from {plugin_path}")
def find_extensions(extension_path, module_name="extensions"):
logging.debug(f"Loading extensions from {extension_path}")
spec = util.spec_from_file_location(module_name, plugin_path)
spec = util.spec_from_file_location(module_name, extension_path)
mod = util.module_from_spec(spec)
sys.modules[spec.name] = mod
spec.loader.exec_module(mod)
if hasattr(mod, "__plugins__"):
return mod.__plugins__
if hasattr(mod, "__extensions__"):
return mod.__extensions__
else:
return None

View file

@ -15,10 +15,10 @@ def current_labthing():
return app.extensions[EXTENSION_NAME]
def registered_plugins(labthing_instance=None):
def registered_extensions(labthing_instance=None):
if not labthing_instance:
labthing_instance = current_labthing()
return labthing_instance.plugins
return labthing_instance.extensions
def registered_devices(labthing_instance=None):
@ -37,14 +37,14 @@ def find_device(device_name, labthing_instance=None):
return None
def find_plugin(plugin_name, labthing_instance=None):
def find_extension(extension_name, labthing_instance=None):
if not labthing_instance:
labthing_instance = current_labthing()
logging.debug("Current labthing:")
logging.debug(current_labthing())
if plugin_name in labthing_instance.plugins:
return labthing_instance.plugins[plugin_name]
if extension_name in labthing_instance.extensions:
return labthing_instance.extensions[extension_name]
else:
return None

View file

@ -2,8 +2,8 @@ from flask import url_for, jsonify
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from .plugins import BasePlugin
from .views.plugins import PluginListResource
from .extensions import BaseExtension
from .views.extensions import ExtensionListResource
from .views.tasks import TaskList, TaskResource
from .spec import view2path
@ -32,7 +32,7 @@ class LabThing(object):
self.devices = {}
self.plugins = {}
self.extensions = {}
self.resources = []
self.properties = {}
@ -121,9 +121,9 @@ class LabThing(object):
self._complete_url("/swagger", ""), "swagger", self.swagger
)
# Add plugin overview
self.add_resource(PluginListResource, "/plugins")
self.register_property(PluginListResource)
# Add extension overview
self.add_resource(ExtensionListResource, "/extensions")
self.register_property(ExtensionListResource)
# Add task routes
self.add_resource(TaskList, "/tasks")
self.register_property(TaskList)
@ -134,26 +134,26 @@ class LabThing(object):
def register_device(self, device_object, device_name: str):
self.devices[device_name] = device_object
### Plugin stuff
### Extension stuff
def register_plugin(self, plugin_object):
if isinstance(plugin_object, BasePlugin):
self.plugins[plugin_object.name] = plugin_object
def register_extension(self, extension_object):
if isinstance(extension_object, BaseExtension):
self.extensions[extension_object.name] = extension_object
else:
raise TypeError("Plugin object must be an instance of BasePlugin")
raise TypeError("Extension object must be an instance of BaseExtension")
for plugin_view_id, plugin_view in plugin_object.views.items():
# Add route to the plugins blueprint
for extension_view_id, extension_view in extension_object.views.items():
# Add route to the extensions blueprint
self.add_resource(
plugin_view["view"],
"/plugins" + plugin_view["rule"],
**plugin_view["kwargs"],
extension_view["view"],
"/extensions" + extension_view["rule"],
**extension_view["kwargs"],
)
for prop in plugin_object.properties:
for prop in extension_object.properties:
self.register_property(prop)
for action in plugin_object.actions:
for action in extension_object.actions:
self.register_action(action)
### Resource stuff
@ -327,9 +327,9 @@ class LabThing(object):
"description": get_docstring(self.swagger),
"methods": ["GET"],
},
"plugins": {
"href": self.url_for(PluginListResource, _external=True),
**description_from_view(PluginListResource),
"extensions": {
"href": self.url_for(ExtensionListResource, _external=True),
**description_from_view(ExtensionListResource),
},
"tasks": {
"href": self.url_for(TaskList, _external=True),

View file

@ -1,12 +1,12 @@
"""
Top-level representation of attached and enabled plugins
Top-level representation of attached and enabled Extensions
"""
from openflexure_microscope.common.labthings_core.utilities import get_docstring
from ..utilities import description_from_view
from openflexure_microscope.common.flask_labthings.resource import Resource
from openflexure_microscope.common.flask_labthings.find import registered_plugins
from openflexure_microscope.common.flask_labthings.find import registered_extensions
from openflexure_microscope.common.flask_labthings.schema import Schema
from openflexure_microscope.common.flask_labthings.decorators import marshal_with
from openflexure_microscope.common.flask_labthings import fields
@ -17,7 +17,7 @@ from flask import jsonify, url_for
import logging
class PluginSchema(Schema):
class ExtensionSchema(Schema):
name = fields.String(data_key="title")
_name_python_safe = fields.String(data_key="pythonName")
_cls = fields.String(data_key="pythonObject")
@ -44,23 +44,23 @@ class PluginSchema(Schema):
return data
class PluginListResource(Resource):
class ExtensionListResource(Resource):
"""
List and basic documentation for all enabled plugins
List and basic documentation for all enabled Extensions
"""
@marshal_with(PluginSchema(many=True))
@marshal_with(ExtensionSchema(many=True))
def get(self):
"""
Return the current plugin forms
Return the current Extension forms
.. :quickref: Plugin; Get forms
.. :quickref: Extension; Get forms
Returns an array of present plugin forms (describing plugin user interfaces.)
Please note, this is *not* a list of all enabled plugins, only those with associated
Returns an array of present Extension forms (describing Extension user interfaces.)
Please note, this is *not* a list of all enabled Extensions, only those with associated
user interface forms.
A complete list of enabled plugins can be found in the microscope state.
A complete list of enabled Extensions can be found in the microscope state.
"""
return registered_plugins().values()
return registered_extensions().values()