Migrated scan plugin

This commit is contained in:
Joel Collins 2019-12-18 11:06:02 +00:00
parent 480848b009
commit dee77c295a
13 changed files with 442 additions and 290 deletions

View file

@ -1,12 +1,17 @@
import logging
from flask import current_app
from . import EXTENSION_NAME
def _current_labthing():
app = current_app
app = current_app._get_current_object()
if not app:
return None
logging.debug("Active app extensions:")
logging.debug(app.extensions)
logging.debug("Active labthing:")
logging.debug(app.extensions[EXTENSION_NAME])
return app.extensions[EXTENSION_NAME]
@ -30,3 +35,15 @@ def find_device(device_name, labthing_instance=None):
return labthing_instance.devices[device_name]
else:
return None
def find_plugin(plugin_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]
else:
return None

View file

@ -53,6 +53,14 @@ class LabThing(object):
# TODO: Add plugin routes
for plugin_view_id, plugin_view in plugin_object.views.items():
# Add route to the plugins blueprint
self.add_resource(
plugin_view["view"],
"/plugins" + plugin_view["rule"],
**plugin_view["kwargs"],
)
### Resource stuff
def _complete_url(self, url_part, registration_prefix):

View file

@ -29,6 +29,8 @@ class BasePlugin:
self.name = name
self.methods = {}
@property
def views(self):
return self._views
@ -102,6 +104,13 @@ class BasePlugin:
def _name_uri_safe(self):
return snake_to_spine(self._name_python_safe)
def add_method(self, method_name, method):
self.methods[method_name] = method
if not hasattr(self, method_name):
setattr(self, method_name, method)
else:
logging.warning("Unable to bind method to plugin. Method name already exists.")
def find_plugins(plugin_path, module_name="plugins"):
print(f"Loading plugins from {plugin_path}")

View file

@ -4,6 +4,7 @@ from functools import wraps
from .thread import TaskThread
from flask import copy_current_request_context
class TaskMaster:
def __init__(self, *args, **kwargs):
@ -26,7 +27,8 @@ class TaskMaster:
return {t.id: t.state for t in self._tasks}
def new(self, f, *args, **kwargs):
task = TaskThread(target=f, args=args, kwargs=kwargs)
# copy_current_request_context allows threads to access flask current_app
task = TaskThread(target=copy_current_request_context(f), args=args, kwargs=kwargs)
self._tasks.append(task)
return task