Utilize settings chain maps for plugin default settings too

This commit is contained in:
Gina Häußge 2016-11-16 15:37:49 +01:00
parent 83a08cd927
commit a58b167bed
3 changed files with 27 additions and 15 deletions

View file

@ -145,7 +145,7 @@ def plugin_settings(plugin_key, defaults=None, get_preprocessors=None, set_prepr
Arguments:
plugin_key (string): The plugin identifier for which to create the settings instance.
defaults (dict): The default settings for the plugin.
defaults (dict): The default settings for the plugin, if different from get_settings_defaults.
get_preprocessors (dict): The getter preprocessors for the plugin.
set_preprocessors (dict): The setter preprocessors for the plugin.
settings (octoprint.settings.Settings): The settings instance to use.
@ -180,13 +180,12 @@ def plugin_settings_for_settings_plugin(plugin_key, instance, settings=None):
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))
logging.getLogger(__name__).exception("Error while retrieving 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)
return plugin_settings(plugin_key, 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):
@ -315,11 +314,12 @@ class PluginSettings(object):
self.settings = settings
self.plugin_key = plugin_key
if defaults is None:
defaults = dict()
self.defaults = dict(plugins=dict())
self.defaults["plugins"][plugin_key] = defaults
self.defaults["plugins"][plugin_key]["_config_version"] = None
if defaults is not None:
self.defaults = dict(plugins=dict())
self.defaults["plugins"][plugin_key] = defaults
self.defaults["plugins"][plugin_key]["_config_version"] = None
else:
self.defaults = None
if get_preprocessors is None:
get_preprocessors = dict()
@ -345,14 +345,14 @@ class PluginSettings(object):
return result
def add_getter_kwargs(kwargs):
if not "defaults" in kwargs:
if not "defaults" in kwargs and self.defaults is not None:
kwargs.update(defaults=self.defaults)
if not "preprocessors" in kwargs:
kwargs.update(preprocessors=self.get_preprocessors)
return kwargs
def add_setter_kwargs(kwargs):
if not "defaults" in kwargs:
if not "defaults" in kwargs and self.defaults is not None:
kwargs.update(defaults=self.defaults)
if not "preprocessors" in kwargs:
kwargs.update(preprocessors=self.set_preprocessors)

View file

@ -245,7 +245,14 @@ class Server(object):
return props
def settings_plugin_inject_factory(name, implementation):
"""Factory for additional injections depending on plugin type"""
"""Factory for additional injections/initializations depending on plugin type"""
if not isinstance(implementation, octoprint.plugin.SettingsPlugin):
return
default_settings_overlay = dict(plugins=dict())
default_settings_overlay["plugins"][name] = implementation.get_settings_defaults()
self._settings.add_overlay(default_settings_overlay, at_end=True)
plugin_settings = octoprint.plugin.plugin_settings_for_settings_plugin(name, implementation)
if plugin_settings is None:
return
@ -272,7 +279,8 @@ class Server(object):
implementation.on_settings_initialized()
pluginManager.implementation_inject_factories=[octoprint_plugin_inject_factory, settings_plugin_inject_factory]
pluginManager.implementation_inject_factories=[octoprint_plugin_inject_factory,
settings_plugin_inject_factory]
pluginManager.initialize_implementations()
settingsPlugins = pluginManager.get_implementations(octoprint.plugin.SettingsPlugin)

View file

@ -829,8 +829,12 @@ class Settings(object):
self._migrate_config(config)
return config
def add_overlay(self, overlay):
self._map.maps.insert(1, overlay)
def add_overlay(self, overlay, at_end=False):
if at_end:
pos = len(self._map.maps) - 1
self._map.maps.insert(pos, overlay)
else:
self._map.maps.insert(1, overlay)
def _migrate_config(self, config=None):
if config is None: