Skip to content

Commit

Permalink
issue: kupferlauncher#61-JSON - MAINT: Clean up kupfer.core.settings k…
Browse files Browse the repository at this point in the history
…upferlauncher#61 - JSON POC; commit: remove _same_type and value_type from settings methods
  • Loading branch information
hugosenari committed Apr 10, 2017
1 parent fcf8534 commit 5522f46
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
34 changes: 9 additions & 25 deletions kupfer/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ def _override_encoding(name):
else:
return None


def _same_val(value):
return value

def _merge(old, new):
for key, val in new.items():
if isinstance(val, collections.Mapping):
Expand Down Expand Up @@ -151,10 +147,8 @@ def _set_config(self, section, key, value):
"""General interface, but section must exist"""
self.output_debug("Set", section, key, "to", value)
key = key.lower()
oldvalue = self._config[section].get(key)
if section in self.defaults:
value_type = type(oldvalue) if oldvalue is not None else _same_val
self._config[section][key] = value_type(value)
self._config[section][key] = value
self._emit_value_changed(section, key, value)
self._update_config_save_timer()
return True
Expand Down Expand Up @@ -317,7 +311,7 @@ def get_special_dir(opt):
def set_directories(self, dirs):
return self._set_config("Directories", "direct", dirs)

def get_plugin_config(self, plugin, key, value_type=_same_val, default=None):
def get_plugin_config(self, plugin, key, default=None):
"""Return setting @key for plugin names @plugin, try
to coerce to type @value_type.
Else return @default if does not exist, or can't be coerced
Expand All @@ -330,30 +324,20 @@ def get_plugin_config(self, plugin, key, value_type=_same_val, default=None):
if val is None:
return default

if hasattr(value_type, "load"):
val_obj = value_type()
val_obj.load(plugin, key, val)
return val_obj
if hasattr(default, "load"):
default.load(plugin, key, val)
return default
else:
try:
val = value_type(val)
except ValueError as err:
self.output_info("Error for stored value %s.%s" %
(plug_section, key), err)
return default
return val

def set_plugin_config(self, plugin, key, value, value_type=_same_val):
def set_plugin_config(self, plugin, key, value):
"""Try set @key for plugin names @plugin, coerce to @value_type
first. """
plug_section = "plugin_%s" % plugin
self._emit_value_changed(plug_section, key, value)

if hasattr(value_type, "save"):
value_repr = value.save(plugin, key)
else:
value_repr = value_type(value)
return self._set_raw_config(plug_section, key, value_repr)
if hasattr(value, "save"):
value = value.save(plugin, key)
return self._set_raw_config(plug_section, key, value)

def get_accelerator(self, name):
return self.get_config("Keybindings", name)
Expand Down
4 changes: 2 additions & 2 deletions kupfer/plugin_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ def initialize(self, plugin_name):
setctl = settings.GetSettingsController()
for key in self:
value_type = self.setting_descriptions[key]["type"]
value = setctl.get_plugin_config(plugin_name, key, value_type)
value = setctl.get_plugin_config(plugin_name, key, value_type())
if value is not None:
self[key] = value
elif _is_core_setting(key):
default = self.setting_descriptions[key]["value"]
setctl.set_plugin_config(plugin_name, key, default, value_type)
setctl.set_plugin_config(plugin_name, key, default)
setctl.connect("value-changed", self._value_changed, plugin_name)
# register for unload notification
if not plugin_name.startswith("core."):
Expand Down
6 changes: 3 additions & 3 deletions kupfer/ui/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def callback(widget):
if no_false_values and not value:
return
setctl = settings.GetSettingsController()
setctl.set_plugin_config(plugin_id, key, value, value_type)
setctl.set_plugin_config(plugin_id, key, value)
return callback

def _get_plugin_credentials_callback(self, plugin_id, key):
Expand All @@ -655,12 +655,12 @@ def callback(widget):
information = _("Using encrypted password storage: %s") % backend_name
else:
information = _("Using password storage: %s") % backend_name
upass = setctl.get_plugin_config(plugin_id, key, val_type) \
upass = setctl.get_plugin_config(plugin_id, key, val_type()) \
or plugin_support.UserNamePassword()
user_password = ask_user_credentials(upass.username, upass.password, information)
if user_password:
upass.username, upass.password = user_password
setctl.set_plugin_config(plugin_id, key, upass, val_type)
setctl.set_plugin_config(plugin_id, key, upass)
return callback

def _make_plugin_settings_widget(self, plugin_id):
Expand Down

0 comments on commit 5522f46

Please sign in to comment.