Moved Settings KeyError handling to the export.

This commit is contained in:
Tomas Groth 2014-11-13 20:15:43 +00:00
parent 2efdc97311
commit 378132d82f
3 changed files with 17 additions and 14 deletions

View File

@ -431,14 +431,10 @@ class Settings(QtCore.QSettings):
:param key: The key to return the value from.
"""
# if group() is not empty the group has not been specified together with the key.
try:
if self.group():
default_value = Settings.__default_settings__[self.group() + '/' + key]
else:
default_value = Settings.__default_settings__[key]
except KeyError:
log.warning('Key "%s" was not found in settings, returning None!' % key)
return None
setting = super(Settings, self).value(key, default_value)
return self._convert_value(setting, default_value)

View File

@ -978,7 +978,14 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow, RegistryProperties):
# FIXME: We are conflicting with the standard "General" section.
if 'eneral' in section_key:
section_key = section_key.lower()
try:
key_value = settings.value(section_key)
except KeyError:
QtGui.QMessageBox.critical(self, translate('OpenLP.MainWindow', 'Export setting error'),
translate('OpenLP.MainWindow', 'The key "%s" does not have a default value '
'so it will be skipped in this export.') % section_key,
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok))
key_value = None
if key_value is not None:
export_settings.setValue(section_key, key_value)
export_settings.sync()

View File

@ -121,9 +121,9 @@ class TestSettings(TestCase, TestMixin):
Test the Settings on query for non-existing value
"""
# GIVEN: A new Settings setup
with self.assertRaises(KeyError) as cm:
# WHEN reading a setting that doesn't exists
does_not_exist_value = Settings().value('core/does not exists')
# THEN None should be returned
self.assertEqual(does_not_exist_value, None, 'The value should be None')
# THEN: An exception with the non-existing key should be thrown
self.assertEqual(str(cm.exception), "'core/does not exists'", 'We should get an exception')