clean-up configuration on load and save

This commit is contained in:
Daniel Pavel 2013-06-26 16:05:14 +02:00
parent c25b769578
commit f43e298ac7
1 changed files with 11 additions and 5 deletions

View File

@ -37,6 +37,7 @@ def _load():
if _log.isEnabledFor(_DEBUG): if _log.isEnabledFor(_DEBUG):
_log.debug("load => %s", _configuration) _log.debug("load => %s", _configuration)
_cleanup(_configuration)
_configuration[_KEY_VERSION] = __version__ _configuration[_KEY_VERSION] = __version__
return _configuration return _configuration
@ -54,6 +55,8 @@ def save():
_log.error("failed to create %s", dirname) _log.error("failed to create %s", dirname)
return False return False
_cleanup(_configuration)
try: try:
with open(_file_path, 'w') as config_file: with open(_file_path, 'w') as config_file:
_json_save(_configuration, config_file, skipkeys=True, indent=2, sort_keys=True) _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) _log.error("failed to save to %s", _file_path)
def all(): def _cleanup(d):
if not _configuration: # remove None values from the dict
_load() for key in list(d.keys()):
value = d.get(key)
return dict(_configuration) if value is None:
del d[key]
elif isinstance(value, dict):
_cleanup(value)
def _device_key(device): def _device_key(device):