New helper plugin_settings_for_settings_plugin, to use from cli hooks and internally

This commit is contained in:
Gina Häußge 2015-10-27 10:48:23 +01:00
parent f4ae7b58e5
commit 03830990ac
2 changed files with 32 additions and 8 deletions

View file

@ -161,6 +161,34 @@ def plugin_settings(plugin_key, defaults=None, get_preprocessors=None, set_prepr
set_preprocessors=set_preprocessors)
def plugin_settings_for_settings_plugin(plugin_key, instance, settings=None):
"""
Factory method for creating a :class:`PluginSettings` instance for a given :class:`SettingsPlugin` instance.
Will return `None` if the provided `instance` is not a :class:`SettingsPlugin` instance.
Arguments:
plugin_key (string): The plugin identifier for which to create the settings instance.
implementation (octoprint.plugin.SettingsPlugin): The :class:`SettingsPlugin` instance.
settings (octoprint.settings.Settings): The settings instance to use. Defaults to the global OctoPrint settings.
Returns:
PluginSettings or None: A fully initialized :class:`PluginSettings` instance to be used to access the plugin's
settings, or `None` if the provided `instance` was not a class:`SettingsPlugin`
"""
if not isinstance(instance, SettingsPlugin):
return None
try:
defaults = instance.get_settings_defaults()
get_preprocessors, set_preprocessors = instance.get_settings_preprocessors()
except:
logging.getLogger(__name__).exception("Error while retrieving defaults or preprocessors for plugin {}".format(plugin_key))
return None
return plugin_settings(plugin_key, defaults=defaults, get_preprocessors=get_preprocessors, set_preprocessors=set_preprocessors, settings=settings)
def call_plugin(types, method, args=None, kwargs=None, callback=None, error_callback=None, sorting_context=None):
"""
Helper method to invoke the indicated ``method`` on all registered plugin implementations implementing the

View file

@ -215,14 +215,10 @@ class Server():
)
def settings_plugin_inject_factory(name, implementation):
if not isinstance(implementation, octoprint.plugin.SettingsPlugin):
return None
default_settings = implementation.get_settings_defaults()
get_preprocessors, set_preprocessors = implementation.get_settings_preprocessors()
plugin_settings = octoprint.plugin.plugin_settings(name,
defaults=default_settings,
get_preprocessors=get_preprocessors,
set_preprocessors=set_preprocessors)
plugin_settings = octoprint.plugin.plugin_settings_for_settings_plugin(name, implementation)
if plugin_settings is None:
return
return dict(settings=plugin_settings)
def settings_plugin_config_migration_and_cleanup(name, implementation):