✨ New hook: octoprint.system.additional_commands
Allows to inject additional commands into the system menu from plugins. Implements #4143
This commit is contained in:
1 parent
b0917feba7
commit
c3a796ae4c
3 files changed
+137
-5
No files matched your search
@@ -1629,6 +1629,79 @@ octoprint.server.sockjs.emit
|
||||
:return: whether to proceed with sending the message (``True``) or not (``False``)
|
||||
:rtype: boolean
|
||||
|
||||
.. _sec-plugins-hook-system-additional_commands:
|
||||
|
||||
octoprint.system.additional_commands
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. py:function:: additional_commands_hook(*args, **kwargs)
|
||||
|
||||
.. versionadded:: 1.7.0
|
||||
|
||||
Allows adding additional system commands into the system menu. Handlers must return
|
||||
a list of system command definitions, each definition matching the following data
|
||||
structure:
|
||||
|
||||
.. list-table::
|
||||
:widths: 15 5 10 30
|
||||
:header-rows: 1
|
||||
|
||||
* - Name
|
||||
- Multiplicity
|
||||
- Type
|
||||
- Description
|
||||
* - ``name``
|
||||
- 1
|
||||
- String
|
||||
- The name to display in the menu.
|
||||
* - ``action``
|
||||
- 1
|
||||
- String
|
||||
- An identifier for the action, must only consist of lower case a-z, numbers, ``-`` and ``_`` (``[a-z0-9-_]``).
|
||||
* - ``command``
|
||||
- 1
|
||||
- String
|
||||
- The system command to execute.
|
||||
* - ``confirm``
|
||||
- 0..1
|
||||
- String
|
||||
- An optional message to show as a confirmation dialog before executing the command.
|
||||
* - ``async``
|
||||
- 0..1
|
||||
- bool
|
||||
- If ``True``, the command will be run asynchronously and the API call will return immediately after enqueuing it for execution.
|
||||
* - ``ignore``
|
||||
- 0..1
|
||||
- bool
|
||||
- If ``True``, OctoPrint will ignore the result of the command's (and ``before``'s, if set) execution and return a successful result regardless. Defaults to ``False``.
|
||||
* - ``debug``
|
||||
- 0..1
|
||||
- bool
|
||||
- If ``True``, the command will generate debug output in the log including the command line that's run. Use with care. Defaults to ``False``
|
||||
* - ``before``
|
||||
- 0..1
|
||||
- callable
|
||||
- Optional callable to execute before the actual ``command`` is run. If ``ignore`` is false and this fails in any way, the command will not run and an error returned.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
def get_additional_commands(*args, **kwargs):
|
||||
return [
|
||||
{
|
||||
"name": "Just a test",
|
||||
"action": "test",
|
||||
"command": "logger This is just a test of an OctoPrint system command from a plugin",
|
||||
"before": lambda: print("Hello World!")
|
||||
}
|
||||
]
|
||||
|
||||
__plugin_hooks__ = {
|
||||
"octoprint.system.additional_commands": get_additional_commands
|
||||
}
|
||||
|
||||
:return: a list of command specifications
|
||||
:rtype: list
|
||||
|
||||
.. _sec-plugins-hook-timelapse-extensions:
|
||||
|
||||
octoprint.timelapse.extensions
|
||||
|
||||
@@ -6,6 +6,7 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms
|
||||
|
||||
import collections
|
||||
import logging
|
||||
import re
|
||||
import threading
|
||||
|
||||
import psutil
|
||||
@@ -15,6 +16,7 @@ from flask_babel import gettext
|
||||
|
||||
from octoprint.access.permissions import Permissions
|
||||
from octoprint.logging import prefix_multilines
|
||||
from octoprint.plugin import plugin_manager
|
||||
from octoprint.server import NO_CONTENT
|
||||
from octoprint.server.api import api
|
||||
from octoprint.server.util.flask import get_remote_address, no_firstrun_access
|
||||
@@ -98,6 +100,7 @@ def performSystemAction():
|
||||
def retrieveSystemCommands():
|
||||
return jsonify(
|
||||
core=_to_client_specs(_get_core_command_specs()),
|
||||
plugin=_to_client_specs(_get_plugin_command_specs()),
|
||||
custom=_to_client_specs(_get_custom_command_specs()),
|
||||
)
|
||||
|
||||
@@ -110,6 +113,8 @@ def retrieveSystemCommandsForSource(source):
|
||||
specs = _get_core_command_specs()
|
||||
elif source == "custom":
|
||||
specs = _get_custom_command_specs()
|
||||
elif source == "plugin":
|
||||
specs = _get_plugin_command_specs()
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
@@ -225,6 +230,8 @@ def _get_command_spec(source, action):
|
||||
return _get_core_command_spec(action)
|
||||
elif source == "custom":
|
||||
return _get_custom_command_spec(action)
|
||||
elif source == "plugin":
|
||||
return _get_plugin_command_spec(action)
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -288,6 +295,50 @@ def _get_core_command_spec(action):
|
||||
return available_actions[action]
|
||||
|
||||
|
||||
_plugin_action_regex = re.compile(r"[a-z0-9_-]+")
|
||||
|
||||
|
||||
def _get_plugin_command_specs(plugin=None):
|
||||
specs = collections.OrderedDict()
|
||||
|
||||
hooks = plugin_manager().get_hooks("octoprint.system.additional_commands")
|
||||
if plugin is not None:
|
||||
if plugin in hooks:
|
||||
hooks = {plugin: hooks[plugin]}
|
||||
else:
|
||||
hooks = {}
|
||||
|
||||
for name, hook in hooks.items():
|
||||
try:
|
||||
plugin_specs = hook()
|
||||
for spec in plugin_specs:
|
||||
action = spec.get("action")
|
||||
if (
|
||||
not action
|
||||
or action == "divider"
|
||||
or not _plugin_action_regex.match(action)
|
||||
):
|
||||
continue
|
||||
action = name.lower() + ":" + action
|
||||
|
||||
copied = dict(spec)
|
||||
copied["source"] = "plugin"
|
||||
copied["action"] = action
|
||||
specs[action] = copied
|
||||
except Exception:
|
||||
logging.getLogger(__name__).exception(
|
||||
"Error while fetching additional actions from plugin {}".format(name),
|
||||
extra={"plugin": name},
|
||||
)
|
||||
return specs
|
||||
|
||||
|
||||
def _get_plugin_command_spec(action):
|
||||
plugin = action.split(":", 1)[0]
|
||||
available_actions = _get_plugin_command_specs(plugin=plugin)
|
||||
return available_actions.get(action)
|
||||
|
||||
|
||||
def _get_custom_command_specs():
|
||||
specs = collections.OrderedDict()
|
||||
dividers = 0
|
||||
@@ -307,10 +358,7 @@ def _get_custom_command_specs():
|
||||
|
||||
def _get_custom_command_spec(action):
|
||||
available_actions = _get_custom_command_specs()
|
||||
if action not in available_actions:
|
||||
return None
|
||||
|
||||
return available_actions[action]
|
||||
return available_actions.get(action)
|
||||
|
||||
|
||||
class CommandFailed(Exception):
|
||||
|
||||
@@ -28,10 +28,21 @@ $(function () {
|
||||
action.actionSource = "core";
|
||||
actions.push(action);
|
||||
});
|
||||
if (response.custom && response.custom.length) {
|
||||
if (
|
||||
(response.custom && response.custom.length) ||
|
||||
(response.plugin && response.plugin.length)
|
||||
) {
|
||||
actions.push({action: "divider"});
|
||||
}
|
||||
}
|
||||
_.each(response.plugin, function (data) {
|
||||
var action = _.extend({}, data);
|
||||
action.actionSource = "plugin";
|
||||
actions.push(action);
|
||||
if (response.custom && response.custom.length) {
|
||||
actions.push({action: "divider"});
|
||||
}
|
||||
});
|
||||
_.each(response.custom, function (data) {
|
||||
var action = _.extend({}, data);
|
||||
action.actionSource = "custom";
|
||||
|
||||
Reference in new issue
Block a user