From 55ec8b180951b821d47d5abdc5f5da789688cd3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 28 Mar 2019 10:52:15 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Partial=20revert=20of=20always?= =?UTF-8?q?=20sending=20SettingsUpdated=20event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It was causing a ton of issues with backend code using `Settings.save` to persist changes outside of REST API calls, triggering configuration update dialogs in the frontend way too often and when not needed. Added a flag instead to cause the `save` method to trigger an event and setting that to False by default. Settings REST API uses it, other callees in e.g. plugin code will be able to as well in the future if necessary. It would be cleaner to always trigger this event and adjust calling code and frontend code to compensate, but with third party plugins out there this is not going to happen reliably so we go this route. --- docs/events/index.rst | 21 ++++++++++++++++++++- src/octoprint/plugin/__init__.py | 11 +++++++++++ src/octoprint/server/api/settings.py | 2 +- src/octoprint/settings.py | 9 +++++---- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/docs/events/index.rst b/docs/events/index.rst index 26deef5bd..108cfc20b 100644 --- a/docs/events/index.rst +++ b/docs/events/index.rst @@ -80,6 +80,8 @@ Available Events Plugins may add additional events via the :ref:`octoprint.events.register_custom_events hook `. +.. _sec-events-available_events-server: + Server ------ @@ -113,6 +115,8 @@ ConnectivityChanged * ``old``: Old connectivity value (true for online, false for offline) * ``new``: New connectivity value (true for online, false for offline) +.. _sec-events-available_events-printer_commmunication: + Printer communication --------------------- @@ -152,6 +156,8 @@ PrinterStateChanged :func:`~octoprint.printer.PrinterInterface.get_state_id` for possible values. * ``state_string``: Text representation of the new state. +.. _sec-events-available_events-file_handling: + File handling ------------- @@ -299,6 +305,8 @@ TransferDone * ``local``: the file's name as stored locally * ``remote``: the file's name as stored on SD +.. _sec-events-available_events-printing: + Printing -------- @@ -443,6 +451,8 @@ PrintResumed * ``file``: the file's full path on disk (``local``) or within its storage (``sdcard``). To be removed in 1.4.0. * ``filename``: the file's name. To be removed in 1.4.0. +.. _sec-events-available_events-gcode_processing: + GCODE processing ---------------- @@ -504,6 +514,8 @@ ToolChange * ``old``: old tool index * ``new``: new tool index +.. _sec-events-available_events-timelapses: + Timelapses ---------- @@ -558,6 +570,8 @@ MovieFailed returned a non-0 return code, ``no_frames`` if no frames were captured that could be rendered to a timelapse, or ``unknown`` for any other reason of failure to render. +.. _sec-events-available_events-slicing: + Slicing ------- @@ -629,8 +643,13 @@ SlicingProfileDeleted * ``slicer``: the slicer for which the profile was deleted * ``profile``: the profile that was deleted +.. _sec-events-available_events-settings: + Settings -------- SettingsUpdated - The internal settings were updated. + The settings were updated via the REST API. + + This event may also be triggered if calling code of :py:class:`octoprint.settings.Settings.save` or + :py:class:`octoprint.plugin.PluginSettings.save` sets the ``trigger_event`` parameter to ``True``. diff --git a/src/octoprint/plugin/__init__.py b/src/octoprint/plugin/__init__.py index 5a1efef7a..fceee2de2 100644 --- a/src/octoprint/plugin/__init__.py +++ b/src/octoprint/plugin/__init__.py @@ -297,6 +297,17 @@ class PluginSettings(object): .. method:: set_boolean(path, value, force=False) Like :func:`set` but ensures the value is an ``boolean`` through attempted conversion before setting it. + + .. method:: save(force=False, trigger_event=False) + + Saves the settings to ``config.yaml`` if there are active changes. If ``force`` is set to ``True`` the settings + will be saved even if there are no changes. Settings ``trigger_event`` to ``True`` will cause a ``SettingsUpdated`` + :ref:`event ` to get triggered. + + :param force: Force saving to ``config.yaml`` even if there are no changes. + :type force: boolean + :param trigger_event: Trigger the ``SettingsUpdated`` :ref:`event ` on save. + :type trigger_event: boolean """ def __init__(self, settings, plugin_key, defaults=None, get_preprocessors=None, set_preprocessors=None): diff --git a/src/octoprint/server/api/settings.py b/src/octoprint/server/api/settings.py index 6c41c333d..70b73ef81 100644 --- a/src/octoprint/server/api/settings.py +++ b/src/octoprint/server/api/settings.py @@ -553,4 +553,4 @@ def _saveSettings(data): except: logger.exception("Could not save settings for plugin {name} ({version})".format(version=plugin._plugin_version, name=plugin._plugin_name)) - s.save() + s.save(trigger_event=True) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index e371dc8f8..22b6084f4 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -1355,7 +1355,7 @@ class Settings(object): shutil.copy(self._configfile, backup) return backup - def save(self, force=False): + def save(self, force=False, trigger_event=False): if not self._dirty and not force: return False @@ -1372,9 +1372,10 @@ class Settings(object): self.load() - payload = dict(config_hash=self.config_hash, - effective_hash=self.effective_hash) - eventManager().fire(Events.SETTINGS_UPDATED, payload=payload) + if trigger_event: + payload = dict(config_hash=self.config_hash, + effective_hash=self.effective_hash) + eventManager().fire(Events.SETTINGS_UPDATED, payload=payload) return True