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: change confmap_difference by _dict_diference

rename _merge by _dict_union
  • Loading branch information
hugosenari committed Apr 12, 2017
1 parent aeeb540 commit 524fa12
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions kupfer/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import copy
import os
import locale
import collections

from collections import Mapping

from gi.repository import GLib, GObject

Expand All @@ -19,12 +20,32 @@ def _override_encoding(name):
else:
return None

def _merge(old, new):

def _is_map(val, vval):
return isinstance(vval, Mapping) and isinstance(val, Mapping)



def _dict_union(updated, update):
for key, val in update.items():
old_val = updated.get(key, None)
if _is_map(val, old_val):
val = _dict_union(old_val, val)
updated[key] = val
return updated


def _dict_difference(new, old):
difference = {}
for key, val in new.items():
if isinstance(val, collections.Mapping):
val = _merge(old.get(key, {}), val)
old[key] = val
return old
old_val = old.get(key, None)
if not old_val:
difference[key] = val
elif val != old[key]:
if _is_map(val, old_val):
val = _dict_difference(val, old_val)
difference[key] = val
return difference


class SettingsController (GObject.GObject, pretty.OutputMixin):
Expand Down Expand Up @@ -91,7 +112,7 @@ def _read_config(self, read_config=True):
for config_file in config_files:
try:
with open(config_file, encoding=self.encoding) as fp:
_merge(confmap, json.load(fp, encoding=self.encoding))
_dict_union(confmap, json.load(fp, encoding=self.encoding))
except IOError as e:
self.output_error(("Error reading configuration file %s: %s" % (config_file, e)))
except UnicodeDecodeError as e:
Expand All @@ -108,25 +129,7 @@ def _save_config(self, scheduler=None):
# read in just the default values
default_confmap = self._read_config(read_config=False)

def confmap_difference(config, defaults):
"""Extract the non-default keys to write out"""
difference = dict()
for secname, section in list(config.items()):
if secname not in defaults:
difference[secname] = dict(section)
continue
difference[secname] = {}
for key, config_val in list(section.items()):
if (secname in defaults and
key in defaults[secname]):
if defaults[secname][key] == config_val:
continue
difference[secname][key] = config_val
if not difference[secname]:
del difference[secname]
return difference

confmap = confmap_difference(self._config, default_confmap)
confmap = _dict_difference(self._config, default_confmap)
## Write to tmp then rename over for it to be atomic
temp_config_path = "%s.%s" % (config_path, os.getpid())
with open(temp_config_path, "w") as out:
Expand Down Expand Up @@ -180,7 +183,7 @@ def get_from_defaults(self, section, option=None):
return
default_value = {}
with open(self._defaults_path) as fp:
_merge(default_value, json.load(fp))
_dict_union(default_value, json.load(fp))
if option is None:
return default_value.get(section, {})
else:
Expand Down

0 comments on commit 524fa12

Please sign in to comment.