✨ Support for changing hook order & sorting context (#4186)
* Add support for changing hook order Default hook value changed from None to 1000 * ♻️ Refactor sorting override logic * fetch settings during platform initialization instead of in plugin core * rename from hook order to sorting order as it's also used for the sorting context of implementations * make sure settings are initialized empty in defaults * 📝 Add missing docs for sorting order overrides * ✨ Fully support reordering of implementations too Co-authored-by: Gina Häußge <gina@octoprint.org>
This commit is contained in:
1 parent
33b45768a2
commit
80ffa8865e
5 files changed
+40
-4
No files matched your search
@@ -561,6 +561,14 @@ plugins are tracked:
|
||||
_forcedCompatible:
|
||||
- ...
|
||||
|
||||
# Custom sorting of hooks and implementations provided by plugins. Two-tiered dictionary
|
||||
# structure, plugin identifier mapping to a dictionary of order overrides mapped by
|
||||
# sorting context/hook name
|
||||
_sortingOrder:
|
||||
some_plugin:
|
||||
some_hook: 1
|
||||
some_other_hook: 200
|
||||
|
||||
# The rest are individual plugin settings, each tracked by their identifier, e.g.:
|
||||
some_plugin:
|
||||
some_setting: true
|
||||
|
||||
@@ -601,6 +601,7 @@ def init_pluginsystem(
|
||||
]
|
||||
plugin_entry_points = ["octoprint.plugin"]
|
||||
plugin_disabled_list = settings.get(["plugins", "_disabled"])
|
||||
plugin_sorting_order = settings.get(["plugins", "_sortingOrder"], merged=True)
|
||||
|
||||
plugin_blacklist = []
|
||||
if not ignore_blacklist and settings.getBoolean(
|
||||
@@ -632,6 +633,7 @@ def init_pluginsystem(
|
||||
plugin_folders=plugin_folders,
|
||||
plugin_entry_points=plugin_entry_points,
|
||||
plugin_disabled_list=plugin_disabled_list,
|
||||
plugin_sorting_order=plugin_sorting_order,
|
||||
plugin_blacklist=plugin_blacklist,
|
||||
plugin_validators=plugin_validators,
|
||||
compatibility_ignored_list=compatibility_ignored_list,
|
||||
|
||||
@@ -39,6 +39,7 @@ def plugin_manager(
|
||||
plugin_bases=None,
|
||||
plugin_entry_points=None,
|
||||
plugin_disabled_list=None,
|
||||
plugin_sorting_order=None,
|
||||
plugin_blacklist=None,
|
||||
plugin_restart_needing_hooks=None,
|
||||
plugin_obsolete_hooks=None,
|
||||
@@ -64,6 +65,8 @@ def plugin_manager(
|
||||
this defaults to the entry point ``octoprint.plugin``.
|
||||
plugin_disabled_list (list): A list of plugin identifiers that are currently disabled. If not provided this
|
||||
defaults to all plugins for which ``enabled`` is set to ``False`` in the settings.
|
||||
plugin_sorting_order (dict): A dict containing a custom sorting orders for plugins. The keys are plugin identifiers,
|
||||
mapped to dictionaries containing the sorting contexts as key and the custom sorting value as value.
|
||||
plugin_blacklist (list): A list of plugin identifiers/identifier-requirement tuples
|
||||
that are currently blacklisted.
|
||||
plugin_restart_needing_hooks (list): A list of hook namespaces which cause a plugin to need a restart in order
|
||||
@@ -121,6 +124,7 @@ def plugin_manager(
|
||||
plugin_entry_points,
|
||||
logging_prefix="octoprint.plugins.",
|
||||
plugin_disabled_list=plugin_disabled_list,
|
||||
plugin_sorting_order=plugin_sorting_order,
|
||||
plugin_blacklist=plugin_blacklist,
|
||||
plugin_restart_needing_hooks=plugin_restart_needing_hooks,
|
||||
plugin_obsolete_hooks=plugin_obsolete_hooks,
|
||||
|
||||
@@ -796,6 +796,7 @@ class PluginManager:
|
||||
|
||||
plugin_timings_logtarget = "PLUGIN_TIMINGS"
|
||||
plugin_timings_message = "{func} - {timing:05.2f}ms"
|
||||
default_order = 1000
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -804,6 +805,7 @@ class PluginManager:
|
||||
plugin_entry_points,
|
||||
logging_prefix=None,
|
||||
plugin_disabled_list=None,
|
||||
plugin_sorting_order=None,
|
||||
plugin_blacklist=None,
|
||||
plugin_restart_needing_hooks=None,
|
||||
plugin_obsolete_hooks=None,
|
||||
@@ -823,6 +825,8 @@ class PluginManager:
|
||||
plugin_entry_points = []
|
||||
if plugin_disabled_list is None:
|
||||
plugin_disabled_list = []
|
||||
if plugin_sorting_order is None:
|
||||
plugin_sorting_order = {}
|
||||
if plugin_blacklist is None:
|
||||
plugin_blacklist = []
|
||||
if compatibility_ignored_list is None:
|
||||
@@ -850,6 +854,7 @@ class PluginManager:
|
||||
self.plugin_bases = plugin_bases
|
||||
self.plugin_entry_points = plugin_entry_points
|
||||
self.plugin_disabled_list = plugin_disabled_list
|
||||
self.plugin_sorting_order = plugin_sorting_order
|
||||
self.plugin_blacklist = processed_blacklist
|
||||
self.plugin_restart_needing_hooks = plugin_restart_needing_hooks
|
||||
self.plugin_obsolete_hooks = plugin_obsolete_hooks
|
||||
@@ -1638,6 +1643,13 @@ class PluginManager:
|
||||
)
|
||||
continue
|
||||
|
||||
if not order:
|
||||
order = self.default_order
|
||||
|
||||
override = self.plugin_sorting_order.get(name, {}).get(hook, None)
|
||||
if override:
|
||||
order = override
|
||||
|
||||
self._plugin_hooks[hook].append((order, name, callback))
|
||||
self._sort_hooks(hook)
|
||||
|
||||
@@ -1681,6 +1693,9 @@ class PluginManager:
|
||||
)
|
||||
continue
|
||||
|
||||
if not order:
|
||||
order = self.default_order
|
||||
|
||||
try:
|
||||
self._plugin_hooks[hook].remove((order, name, callback))
|
||||
self._sort_hooks(hook)
|
||||
@@ -2125,11 +2140,18 @@ class PluginManager:
|
||||
impl[0], sorting_context
|
||||
)
|
||||
)
|
||||
sorting_value = None
|
||||
sorting_value = self.default_order
|
||||
else:
|
||||
sorting_value = self.default_order
|
||||
|
||||
override = self.plugin_sorting_order.get(impl[0], {}).get(
|
||||
sorting_context, None
|
||||
)
|
||||
if override:
|
||||
sorting_value = override
|
||||
|
||||
plugin_info = self.get_plugin_info(impl[0], require_enabled=False)
|
||||
return (
|
||||
sorting_value is None,
|
||||
sv(sorting_value),
|
||||
not plugin_info.bundled if plugin_info else True,
|
||||
sv(impl[0]),
|
||||
@@ -2223,7 +2245,7 @@ class PluginManager:
|
||||
def _sort_hooks(self, hook):
|
||||
self._plugin_hooks[hook] = sorted(
|
||||
self._plugin_hooks[hook],
|
||||
key=lambda x: (x[0] is None, sv(x[0]), sv(x[1]), sv(x[2])),
|
||||
key=lambda x: (sv(x[0]), sv(x[1]), sv(x[2])),
|
||||
)
|
||||
|
||||
def _get_callback_and_order(self, hook):
|
||||
|
||||
@@ -438,7 +438,7 @@ default_settings = {
|
||||
"regex": r"Recv: (echo:\s*)?busy:\s*processing",
|
||||
},
|
||||
],
|
||||
"plugins": {"_disabled": [], "_forcedCompatible": []},
|
||||
"plugins": {"_disabled": [], "_forcedCompatible": [], "_sortingOrder": {}},
|
||||
"scripts": {
|
||||
"gcode": {
|
||||
"afterPrintCancelled": "; disable motors\nM84\n\n;disable all heaters\n{% snippet 'disable_hotends' %}\n{% snippet 'disable_bed' %}\n;disable fan\nM106 S0",
|
||||
|
||||
Reference in new issue
Block a user