From cdc7ae594ccd9667b43c58471a2d8beffdbaaf31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 17 Jan 2023 14:29:04 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Adjust=20compat=20overlay=20on?= =?UTF-8?q?=20default=20webcam=20change?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/__init__.py | 44 ++++++++++++++++++------------ src/octoprint/settings/__init__.py | 36 ++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/octoprint/__init__.py b/src/octoprint/__init__.py index 78e976c15..2e8e634db 100644 --- a/src/octoprint/__init__.py +++ b/src/octoprint/__init__.py @@ -512,25 +512,35 @@ def init_webcam_compat_overlay(settings, plugin_manager): import octoprint.webcams - default_webcam = octoprint.webcams.get_default_webcam( - settings=settings, plugin_manager=plugin_manager - ) - if default_webcam is None: - return + def set_overlay(): + default_webcam = octoprint.webcams.get_default_webcam( + settings=settings, plugin_manager=plugin_manager + ) + if default_webcam is None: + settings.remove_overlay("webcam_compat") + return - if not default_webcam.config or not default_webcam.config.compat: - return + if not default_webcam.config or not default_webcam.config.compat: + settings.remove_overlay("webcam_compat") + return - logging.getLogger(__name__).info( - f"Installing webcam compat overlay for configured default webcam {default_webcam}" - ) - overlay = {"webcam": default_webcam.config.compat.dict(by_alias=True)} - settings.add_overlay( - overlay, - key="webcam_compat", - at_end=True, - deprecated="Please use the webcam system introduced with 1.9.0, this compatibility layer will be removed in a future release.", - ) + logging.getLogger(__name__).info( + f"Installing webcam compat overlay for configured default webcam {default_webcam}" + ) + overlay = {"webcam": default_webcam.config.compat.dict(by_alias=True)} + settings.add_overlay( + overlay, + key="webcam_compat", + at_end=True, + deprecated="Please use the webcam system introduced with 1.9.0, this compatibility layer will be removed in a future release.", + replace=True, + ) + + def callback(path, current_value, new_value): + set_overlay() + + set_overlay() + settings.add_path_update_callback(["webcam", "defaultWebcam"], callback) def init_custom_events(plugin_manager): diff --git a/src/octoprint/settings/__init__.py b/src/octoprint/settings/__init__.py index c98282bc5..1144aa4ed 100644 --- a/src/octoprint/settings/__init__.py +++ b/src/octoprint/settings/__init__.py @@ -27,7 +27,7 @@ import re import sys import threading import time -from collections import ChainMap +from collections import ChainMap, defaultdict from collections.abc import KeysView from yaml import YAMLError @@ -491,6 +491,7 @@ class Settings: self._get_preprocessors = {"controls": self._process_custom_controls} self._set_preprocessors = {} + self._path_update_callbacks = defaultdict(list) self._deprecated_paths = {} self._init_basedir(basedir) @@ -563,6 +564,18 @@ class Settings: ) return self._deprecated_paths.get(tuple(path), False) + def _path_modified(self, path, current_value, new_value): + callbacks = self._path_update_callbacks.get(tuple(path)) + if callbacks: + for callback in callbacks: + try: + if callable(callback): + callback(path, current_value, new_value) + except Exception: + self._logger.exception( + f"Error while executing callback {callback} for path {path}" + ) + def _get_default_folder(self, type): folder = default_settings["folder"][type] if folder is None: @@ -926,7 +939,9 @@ class Settings: self._migrate_config(config) return config - def add_overlay(self, overlay, at_end=False, key=None, deprecated=None): + def add_overlay( + self, overlay, at_end=False, key=None, deprecated=None, replace=False + ): assert isinstance(overlay, dict) if key is None: @@ -937,6 +952,9 @@ class Settings: hash.update(overlay_yaml.encode("utf-8")) key = hash.hexdigest() + if replace: + self.remove_overlay(key) + if deprecated is not None: def paths(prefix, data): @@ -971,6 +989,18 @@ class Settings: return True return False + def add_path_update_callback(self, path, callback): + callbacks = self._path_update_callbacks[tuple(path)] + if callback not in callbacks: + callbacks.append(callback) + + def remove_path_update_callback(self, path, callback): + try: + self._path_update_callbacks[tuple(path)].remove(callback) + except ValueError: + # callback not in list + pass + def _migrate_config(self, config=None, persist=False): if config is None: config = self._map.top_map @@ -1887,6 +1917,7 @@ class Settings: try: chain.del_by_path(path) self._mark_dirty() + self._path_modified(path, current, value) except KeyError: if error_on_path: raise NoSuchSettingsPath() @@ -1898,6 +1929,7 @@ class Settings: ): chain.set_by_path(path, value) self._mark_dirty() + self._path_modified(path, current, value) # we've changed the interface to no longer mutate the passed in config, so we # must manually do that here