From 0e60b26048a295f15de52edf3ecde375907e46e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 19 Jun 2015 22:31:05 +0200 Subject: [PATCH] Config migration must happen after all plugin implementations are initialized Introduced new callback for settings plugin to hook into the point after the settings have been migrated since the initialize method is not suitable for that as it might be called before other plugin implementations reachable through hooks are initialized. --- src/octoprint/plugin/types.py | 15 ++++++++++++--- src/octoprint/server/__init__.py | 11 +++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/octoprint/plugin/types.py b/src/octoprint/plugin/types.py index d285173c..578e117a 100644 --- a/src/octoprint/plugin/types.py +++ b/src/octoprint/plugin/types.py @@ -853,9 +853,9 @@ class SettingsPlugin(OctoPrintPlugin): Your plugin's implementation should take care of migrating any data by utilizing self._settings. OctoPrint will take care of saving any changes to disk by calling `self._settings.save()` after returning from this method. - This method will be called before your plugin's :func:`initialize` method, but with all injections already - having taken place. You can therefore depend on the configuration having been migrated by the time :func:`initialize` - is called. + This method will be called before your plugin's :func:`on_settings_initialized` method, with all injections already + having taken place. You can therefore depend on the configuration having been migrated by the time + :func:`on_settings_initialized` is called. Arguments: target (int): The settings format version the plugin requires, this should always be the same value as @@ -865,6 +865,15 @@ class SettingsPlugin(OctoPrintPlugin): """ pass + def on_settings_initialized(self): + """ + Called after the settings have been initialized and - if necessary - also been migrated through a call to + func:`on_settings_migrate`. + + This method will always be called after the `initialize` method. + """ + pass + class EventHandlerPlugin(OctoPrintPlugin): """ diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index d94ba4a4..811baaf6 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -210,7 +210,7 @@ class Server(): set_preprocessors=set_preprocessors) return dict(settings=plugin_settings) - def settings_plugin_pre_init(name, implementation): + def settings_plugin_config_migration(name, implementation): if not isinstance(implementation, octoprint.plugin.SettingsPlugin): return @@ -224,10 +224,17 @@ class Server(): implementation._settings.set_int(["_config_version"], settings_version) implementation._settings.save() + implementation.on_settings_initialized() + pluginManager.implementation_inject_factories=[octoprint_plugin_inject_factory, settings_plugin_inject_factory] - pluginManager.implementation_pre_inits=[settings_plugin_pre_init] pluginManager.initialize_implementations() + settingsPlugins = pluginManager.get_implementations(octoprint.plugin.SettingsPlugin) + for implementation in settingsPlugins: + settings_plugin_config_migration(implementation._identifier, implementation) + + pluginManager.implementation_post_inits=[settings_plugin_config_migration] + pluginManager.log_all_plugins() # initialize file manager and register it for changes in the registered plugins