From f6de8a4f7a994615ce550b892bd0c4e2ceecd76b Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Thu, 12 Feb 2015 20:44:34 +0000 Subject: [PATCH 1/7] Fix 1416703 --- openlp/core/common/settings.py | 13 +++++++++++-- .../functional/openlp_core_common/test_settings.py | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index f30aeb0ee..4c9fd767a 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -45,6 +45,13 @@ if is_linux(): X11_BYPASS_DEFAULT = False +def recent_files_conv(value): + if isinstance(value, list): + return value + elif isinstance(value, str): + return [value] + + class Settings(QtCore.QSettings): """ Class to wrap QSettings. @@ -328,7 +335,7 @@ class Settings(QtCore.QSettings): ('general/language', 'core/language', []), ('general/last version test', 'core/last version test', []), ('general/loop delay', 'core/loop delay', []), - ('general/recent files', 'core/recent files', []), + ('general/recent files', 'core/recent files', [(recent_files_conv, None)]), ('general/save prompt', 'core/save prompt', []), ('general/screen blank', 'core/screen blank', []), ('general/show splash', 'core/show splash', []), @@ -410,7 +417,9 @@ class Settings(QtCore.QSettings): for new, old in rules: # If the value matches with the condition (rule), then use the provided value. This is used to # convert values. E. g. an old value 1 results in True, and 0 in False. - if old == old_value: + if callable(new): + old_value = new(old_value) + elif old == old_value: old_value = new break self.setValue(new_key, old_value) diff --git a/tests/functional/openlp_core_common/test_settings.py b/tests/functional/openlp_core_common/test_settings.py index 306acba09..6fb5aa658 100644 --- a/tests/functional/openlp_core_common/test_settings.py +++ b/tests/functional/openlp_core_common/test_settings.py @@ -25,6 +25,7 @@ Package to test the openlp.core.lib.settings package. from unittest import TestCase from openlp.core.common import Settings +from tests.functional import patch from tests.helpers.testmixin import TestMixin @@ -120,3 +121,11 @@ class TestSettings(TestCase, TestMixin): # 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') + + def extend_default_settings_test(self): + with patch.dict(Settings() .__default_settings__, {1: 2, 3: 4}, clear=True): + Settings().extend_default_settings({'a': 'b', 'c': 'd'}) + + ds = Settings().__default_settings__ + + self.assertDictEqual(ds, {1: 2, 3: 4, 'a': 'b', 'c': 'd'}) From 126873ddaa15f2a6fe727b7fd4f322f4dab8a85d Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Fri, 13 Feb 2015 20:41:34 +0000 Subject: [PATCH 2/7] fixes bug1416703 by implementing a function to do the conversion Fixes: https://launchpad.net/bugs/1416703 --- openlp/core/common/settings.py | 2 +- .../openlp_core_common/test_settings.py | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index 4c9fd767a..3bc506a11 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -360,7 +360,7 @@ class Settings(QtCore.QSettings): :param default_values: A dict with setting keys and their default values. """ - Settings.__default_settings__ = dict(list(default_values.items()) + list(Settings.__default_settings__.items())) + Settings.__default_settings__.update(default_values) @staticmethod def set_filename(ini_file): diff --git a/tests/functional/openlp_core_common/test_settings.py b/tests/functional/openlp_core_common/test_settings.py index 6fb5aa658..ee305c600 100644 --- a/tests/functional/openlp_core_common/test_settings.py +++ b/tests/functional/openlp_core_common/test_settings.py @@ -123,9 +123,17 @@ class TestSettings(TestCase, TestMixin): self.assertEqual(str(cm.exception), "'core/does not exists'", 'We should get an exception') def extend_default_settings_test(self): - with patch.dict(Settings() .__default_settings__, {1: 2, 3: 4}, clear=True): - Settings().extend_default_settings({'a': 'b', 'c': 'd'}) + """ + Test that the extend_default_settings method extends the default settings + """ + # GIVEN: A patched __default_settings__ dictionary + with patch.dict(Settings.__default_settings__, + {'test/setting 1': 1, 'test/setting 2': 2, 'test/setting 3': 3}, True): - ds = Settings().__default_settings__ + # WHEN: Calling extend_default_settings + Settings.extend_default_settings({'test/setting 3': 4, 'test/extended 1': 1, 'test/extended 2': 2}) - self.assertDictEqual(ds, {1: 2, 3: 4, 'a': 'b', 'c': 'd'}) + # THEN: The _default_settings__ dictionary_ should have the new keys + self.assertEqual( + Settings.__default_settings__, {'test/setting 1': 1, 'test/setting 2': 2, 'test/setting 3': 4, + 'test/extended 1': 1, 'test/extended 2': 2}) From 77109e61fd06380287ac35c1ac20ff334ef6342a Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Fri, 13 Feb 2015 21:00:55 +0000 Subject: [PATCH 3/7] Update doc string --- openlp/core/common/settings.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index 3bc506a11..91a0676cb 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -73,10 +73,9 @@ class Settings(QtCore.QSettings): The first entry is the *old key*; it will be removed. The second entry is the *new key*; we will add it to the config. If this is just an empty string, we just remove - the old key. - - The last entry is a list containing two-pair tuples. If the list is empty, no conversion is made. Otherwise each - pair describes how to convert the old setting's value:: + the old key. The last entry is a list containing two-pair tuples. If the list is empty, no conversion is made. + If the first value is callable i.e. a function, the function will be called with the old setting's value. + Otherwise each pair describes how to convert the old setting's value:: (SlideLimits.Wrap, True) From fa5b03632397cde421a77884b9f78c0d996528dc Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Thu, 19 Feb 2015 21:47:53 +0000 Subject: [PATCH 4/7] adds bytes type --- openlp/core/common/settings.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index 91a0676cb..311fedd23 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -25,7 +25,6 @@ This class contains the core default settings. import datetime import logging import os -import sys from PyQt4 import QtCore, QtGui @@ -46,10 +45,17 @@ if is_linux(): def recent_files_conv(value): + """ + If the value is not a list convert it yo a list + :param value: Value to convert + :return:value as a List + """ if isinstance(value, list): return value elif isinstance(value, str): return [value] + elif isinstance(value, bytes): + return [value.decode()] class Settings(QtCore.QSettings): From 05ce21104de5d87b5e6758cc6db7bd0849c36376 Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Thu, 19 Feb 2015 21:57:26 +0000 Subject: [PATCH 5/7] PEP fix --- openlp/core/common/historycombobox.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openlp/core/common/historycombobox.py b/openlp/core/common/historycombobox.py index e55d08924..cc5e40649 100644 --- a/openlp/core/common/historycombobox.py +++ b/openlp/core/common/historycombobox.py @@ -28,9 +28,9 @@ from PyQt4 import QtCore, QtGui class HistoryComboBox(QtGui.QComboBox): """ - The :class:`~openlp.core.common.historycombobox.HistoryComboBox` widget emulates the QLineEdit ``returnPressed`` signal - for when the :kbd:`Enter` or :kbd:`Return` keys are pressed, and saves anything that is typed into the edit box into - its list. + The :class:`~openlp.core.common.historycombobox.HistoryComboBox` widget emulates the QLineEdit ``returnPressed`` + signal for when the :kbd:`Enter` or :kbd:`Return` keys are pressed, and saves anything that is typed into the edit + box into its list. """ returnPressed = QtCore.pyqtSignal() From 79204ea7238bbe64effba62efa81b321fdfd3b6f Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Thu, 19 Feb 2015 22:04:54 +0000 Subject: [PATCH 6/7] PEP fix --- tests/functional/openlp_core_common/test_settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/openlp_core_common/test_settings.py b/tests/functional/openlp_core_common/test_settings.py index ee305c600..04a98b352 100644 --- a/tests/functional/openlp_core_common/test_settings.py +++ b/tests/functional/openlp_core_common/test_settings.py @@ -128,7 +128,7 @@ class TestSettings(TestCase, TestMixin): """ # GIVEN: A patched __default_settings__ dictionary with patch.dict(Settings.__default_settings__, - {'test/setting 1': 1, 'test/setting 2': 2, 'test/setting 3': 3}, True): + {'test/setting 1': 1, 'test/setting 2': 2, 'test/setting 3': 3}, True): # WHEN: Calling extend_default_settings Settings.extend_default_settings({'test/setting 3': 4, 'test/extended 1': 1, 'test/extended 2': 2}) From 4b52fde1da5141429d7be9ba0eeaeff247ec6edc Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Sat, 21 Feb 2015 13:08:56 +0000 Subject: [PATCH 7/7] Fixes possible bug and adds test --- openlp/core/common/settings.py | 5 +++-- .../openlp_core_common/test_settings.py | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/openlp/core/common/settings.py b/openlp/core/common/settings.py index 672c24d95..0a5e92b48 100644 --- a/openlp/core/common/settings.py +++ b/openlp/core/common/settings.py @@ -46,9 +46,9 @@ if is_linux(): def recent_files_conv(value): """ - If the value is not a list convert it yo a list + If the value is not a list convert it to a list :param value: Value to convert - :return:value as a List + :return: value as a List """ if isinstance(value, list): return value @@ -56,6 +56,7 @@ def recent_files_conv(value): return [value] elif isinstance(value, bytes): return [value.decode()] + return [] class Settings(QtCore.QSettings): diff --git a/tests/functional/openlp_core_common/test_settings.py b/tests/functional/openlp_core_common/test_settings.py index 04a98b352..cf7abadc7 100644 --- a/tests/functional/openlp_core_common/test_settings.py +++ b/tests/functional/openlp_core_common/test_settings.py @@ -25,6 +25,7 @@ Package to test the openlp.core.lib.settings package. from unittest import TestCase from openlp.core.common import Settings +from openlp.core.common.settings import recent_files_conv from tests.functional import patch from tests.helpers.testmixin import TestMixin @@ -46,6 +47,25 @@ class TestSettings(TestCase, TestMixin): """ self.destroy_settings() + def recent_files_conv_test(self): + """ + Test that recent_files_conv, converts various possible types of values correctly. + """ + # GIVEN: A list of possible value types and the expected results + possible_values = [(['multiple', 'values'], ['multiple', 'values']), + (['single value'], ['single value']), + ('string value', ['string value']), + (b'bytes value', ['bytes value']), + ([], []), + (None, [])] + + # WHEN: Calling recent_files_conv with the possible values + for value, expected_result in possible_values: + actual_result = recent_files_conv(value) + + # THEN: The actual result should be the same as the expected result + self.assertEqual(actual_result, expected_result) + def settings_basic_test(self): """ Test the Settings creation and its default usage