From ca2f5f927c950630b7b3870ecbcd4a92f0f058c2 Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Sun, 7 Jul 2013 16:06:27 +0200 Subject: [PATCH] configuration: save on every change (fixes #80) --- lib/solaar/configuration.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/solaar/configuration.py b/lib/solaar/configuration.py index f8c18400..afd1b922 100644 --- a/lib/solaar/configuration.py +++ b/lib/solaar/configuration.py @@ -81,17 +81,27 @@ def _device_key(device): return '%s:%s' % (device.wpid, device.serial) +class _DeviceEntry(dict): + def __init__(self, *args, **kwargs): + super(_DeviceEntry, self).__init__(*args, **kwargs) + + def __setitem__(self, key, value): + super(_DeviceEntry, self).__setitem__(key, value) + save() + + def _device_entry(device): if not _configuration: _load() device_key = _device_key(device) - if device_key in _configuration: - c = _configuration[device_key] - else: - c = _configuration[device_key] = {} + c = _configuration.get(device_key) or {} + + if not isinstance(c, _DeviceEntry): + c[_KEY_NAME] = device.name + c = _DeviceEntry(c) + _configuration[device_key] = c - c[_KEY_NAME] = device.name return c