From f43e298ac7b29ea390ea7c45cceb92eb352d6f02 Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Wed, 26 Jun 2013 16:05:14 +0200 Subject: [PATCH] clean-up configuration on load and save --- lib/solaar/configuration.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/solaar/configuration.py b/lib/solaar/configuration.py index 7b63796f..37ce02b8 100644 --- a/lib/solaar/configuration.py +++ b/lib/solaar/configuration.py @@ -37,6 +37,7 @@ def _load(): if _log.isEnabledFor(_DEBUG): _log.debug("load => %s", _configuration) + _cleanup(_configuration) _configuration[_KEY_VERSION] = __version__ return _configuration @@ -54,6 +55,8 @@ def save(): _log.error("failed to create %s", dirname) return False + _cleanup(_configuration) + try: with open(_file_path, 'w') as config_file: _json_save(_configuration, config_file, skipkeys=True, indent=2, sort_keys=True) @@ -64,11 +67,14 @@ def save(): _log.error("failed to save to %s", _file_path) -def all(): - if not _configuration: - _load() - - return dict(_configuration) +def _cleanup(d): + # remove None values from the dict + for key in list(d.keys()): + value = d.get(key) + if value is None: + del d[key] + elif isinstance(value, dict): + _cleanup(value) def _device_key(device):