🐛 Adjust compat overlay on default webcam change
This commit is contained in:
1 parent
58086a2928
commit
cdc7ae594c
2 files changed
+61
-19
No files matched your search
+27
-17
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user