-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NB] Add new core plugin: command_plugin
Add new core plugin, `command_plugin`, with the following 0MQ API: - `register_command(command_name, plugin_name=None, namespace='')` * Register command. * If `plugin_name` is not specified, use name of request source plugin. - `unregister_command(command_name, plugin_name=None, namespace='')` * Unregister command. * If `plugin_name` is not specified, use name of request source plugin. - `get_commands()` * Return `pandas.DataFrame` containing command list.
- Loading branch information
Showing
4 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
try: | ||
import microdrop_plugin | ||
except ImportError: | ||
import sys | ||
|
||
print >> sys.stderr, 'Error importing command_plugin' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
""" | ||
Copyright 2016 Christian Fobel | ||
This file is part of command_plugin. | ||
command_plugin is free software: you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License as published by the | ||
Free Software Foundation, either version 3 of the License, or (at your option) | ||
any later version. | ||
command_plugin is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
more details. | ||
You should have received a copy of the GNU General Public License | ||
along with command_plugin. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
import logging | ||
|
||
import gobject | ||
import zmq | ||
|
||
from .plugin import CommandZmqPlugin | ||
from ...app_context import get_hub_uri | ||
from ...plugin_manager import (PluginGlobals, SingletonPlugin, IPlugin, | ||
implements) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
PluginGlobals.push_env('microdrop') | ||
|
||
|
||
class CommandPlugin(SingletonPlugin): | ||
""" | ||
This class is automatically registered with the PluginManager. | ||
""" | ||
implements(IPlugin) | ||
plugin_name = 'wheelerlab.command_plugin' | ||
|
||
def __init__(self): | ||
self.name = self.plugin_name | ||
self.plugin = None | ||
self.command_timeout_id = None | ||
|
||
def on_plugin_enable(self): | ||
""" | ||
Handler called once the plugin instance is enabled. | ||
Note: if you inherit your plugin from AppDataController and don't | ||
implement this handler, by default, it will automatically load all | ||
app options from the config file. If you decide to overide the | ||
default handler, you should call: | ||
AppDataController.on_plugin_enable(self) | ||
to retain this functionality. | ||
""" | ||
self.cleanup() | ||
self.plugin = CommandZmqPlugin(self, self.name, get_hub_uri()) | ||
# Initialize sockets. | ||
self.plugin.reset() | ||
|
||
def check_command_socket(): | ||
try: | ||
msg_frames = (self.plugin.command_socket | ||
.recv_multipart(zmq.NOBLOCK)) | ||
except zmq.Again: | ||
pass | ||
else: | ||
self.plugin.on_command_recv(msg_frames) | ||
return True | ||
|
||
self.command_timeout_id = gobject.timeout_add(10, check_command_socket) | ||
|
||
def cleanup(self): | ||
if self.command_timeout_id is not None: | ||
gobject.source_remove(self.command_timeout_id) | ||
if self.plugin is not None: | ||
self.plugin = None | ||
|
||
def on_plugin_disable(self): | ||
""" | ||
Handler called once the plugin instance is disabled. | ||
""" | ||
self.cleanup() | ||
|
||
def on_app_exit(self): | ||
""" | ||
Handler called just before the Microdrop application exits. | ||
""" | ||
self.cleanup() | ||
|
||
|
||
PluginGlobals.pop_env() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
""" | ||
Copyright 2016 Christian Fobel | ||
This file is part of command_plugin. | ||
command_plugin is free software: you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License as published by the | ||
Free Software Foundation, either version 3 of the License, or (at your option) | ||
any later version. | ||
command_plugin is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
more details. | ||
You should have received a copy of the GNU General Public License | ||
along with command_plugin. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
from multiprocessing import Process | ||
import logging | ||
import sys | ||
|
||
from zmq_plugin.plugin import Plugin as ZmqPlugin | ||
from zmq_plugin.schema import decode_content_data | ||
import pandas as pd | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CommandZmqPlugin(ZmqPlugin): | ||
''' | ||
API for registering commands. | ||
''' | ||
def __init__(self, parent, *args, **kwargs): | ||
self.parent = parent | ||
self.control_board = None | ||
self._commands = pd.DataFrame(None, columns=['namespace', | ||
'plugin_name', | ||
'command_name', 'title']) | ||
super(CommandZmqPlugin, self).__init__(*args, **kwargs) | ||
|
||
def on_execute__unregister_command(self, request): | ||
data = decode_content_data(request) | ||
commands = self._commands | ||
ix = commands.loc[(commands.namespace == data['namespace']) & | ||
(commands.plugin_name == data['plugin_name']) & | ||
(commands.command_name == data['command_name']) & | ||
(commands.title == data['title'])].index | ||
self._commands.drop(ix, inplace=True) | ||
self._commands.reset_index(drop=True, inplace=True) | ||
return self.commands | ||
|
||
def on_execute__register_command(self, request): | ||
data = decode_content_data(request) | ||
plugin_name = data.get('plugin_name', request['header']['source']) | ||
return self.register_command(plugin_name, data['command_name'], | ||
namespace=data.get('namespace', ''), | ||
title=data.get('title')) | ||
|
||
def on_execute__get_commands(self, request): | ||
return self.commands | ||
|
||
def register_command(self, plugin_name, command_name, namespace='', | ||
title=None): | ||
''' | ||
Register command. | ||
Each command is unique by: | ||
(namespace, plugin_name, command_name) | ||
''' | ||
if title is None: | ||
title = (command_name[:1].upper() + | ||
command_name[1:]).replace('_', ' ') | ||
row_i = dict(zip(self._commands, [namespace, plugin_name, command_name, | ||
title])) | ||
self._commands = self._commands.append(row_i, ignore_index=True) | ||
return self.commands | ||
|
||
@property | ||
def commands(self): | ||
''' | ||
Returns | ||
------- | ||
pd.Series | ||
Series of command groups, where each group name maps to a series of | ||
commands. | ||
''' | ||
return self._commands.copy() | ||
|
||
|
||
|
||
def parse_args(args=None): | ||
"""Parses arguments, returns (options, args).""" | ||
from argparse import ArgumentParser | ||
|
||
if args is None: | ||
args = sys.argv | ||
|
||
parser = ArgumentParser(description='ZeroMQ Plugin process.') | ||
log_levels = ('critical', 'error', 'warning', 'info', 'debug', 'notset') | ||
parser.add_argument('-l', '--log-level', type=str, choices=log_levels, | ||
default='info') | ||
parser.add_argument('hub_uri') | ||
parser.add_argument('name', type=str) | ||
|
||
args = parser.parse_args() | ||
args.log_level = getattr(logging, args.log_level.upper()) | ||
return args | ||
|
||
|
||
if __name__ == '__main__': | ||
from zmq_plugin.bin.plugin import run_plugin | ||
|
||
def run_plugin_process(uri, name, subscribe_options, log_level): | ||
plugin_process = Process(target=run_plugin, | ||
args=()) | ||
plugin_process.daemon = False | ||
plugin_process.start() | ||
|
||
args = parse_args() | ||
|
||
logging.basicConfig(level=args.log_level) | ||
task = CommandZmqPlugin(None, args.name, args.hub_uri, {}) | ||
run_plugin(task, args.log_level) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters