diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 8df4ffa71..d4e880312 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -43,7 +43,7 @@ try: except ImportError: from chainmap import ChainMap -from octoprint.util import atomic_write, is_hidden_path, dict_merge +from octoprint.util import atomic_write, is_hidden_path, dict_merge, CaseInsensitiveSet _APPNAME = "OctoPrint" @@ -405,7 +405,7 @@ default_settings = { } """The default settings of the core application.""" -valid_boolean_trues = [True, "True", "true", "yes", "y", "1"] +valid_boolean_trues = CaseInsensitiveSet(True, "true", "yes", "y", "1", 1) """ Values that are considered to be equivalent to the boolean ``True`` value, used for type conversion in various places.""" diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 45dad8a07..63c63e7c8 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -1167,7 +1167,7 @@ class ResettableTimer(threading.Thread): on_cancelled (callable): Callback to call when the timer finishes due to being cancelled. on_reset (callable): Callback to call when the timer is reset. """ - + def __init__(self, interval, function, args=None, kwargs=None, on_reset=None, on_cancelled=None): threading.Thread.__init__(self) self._event = threading.Event() @@ -1501,3 +1501,26 @@ class ConnectivityChecker(object): "online" if new_value else "offline")) if callable(self._on_change): self._on_change(old_value, new_value) + + +class CaseInsensitiveSet(collections.Set): + """ + Basic case insensitive set + + Any str or unicode values will be stored and compared in lower case. Other value types are left as-is. + """ + + def __init__(self, *args): + self.data = set([x.lower() if isinstance(x, (str, unicode)) else x for x in args]) + + def __contains__(self, item): + if isinstance(item, (str, unicode)): + return item.lower() in self.data + else: + return item in self.data + + def __iter__(self): + return iter(self.data) + + def __len__(self): + return len(self.data) diff --git a/tests/test_settings.py b/tests/test_settings.py new file mode 100644 index 000000000..3a811bc79 --- /dev/null +++ b/tests/test_settings.py @@ -0,0 +1,34 @@ +# coding=utf-8 +from __future__ import absolute_import, division, print_function + +__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' +__copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms of the AGPLv3 License" + +import unittest +import ddt + +import octoprint.settings + +@ddt.ddt +class TestHelpers(unittest.TestCase): + + @ddt.data( + (True, True), + ("true", True), + ("True", True), + ("tRuE", True), + ("yes", True), + ("YES", True), + ("y", True), + ("Y", True), + ("1", True), + (1, True), + + (False, False), + ("Truuuuuuuuue", False), + ("Nope", False), + (None, False) + ) + @ddt.unpack + def test_valid_boolean_trues(self, value, expected): + self.assertEqual(expected, value in octoprint.settings.valid_boolean_trues) diff --git a/tests/util/test_caseinsensitive_set.py b/tests/util/test_caseinsensitive_set.py new file mode 100644 index 000000000..9dfbf7443 --- /dev/null +++ b/tests/util/test_caseinsensitive_set.py @@ -0,0 +1,25 @@ +# coding=utf-8 +from __future__ import absolute_import + +__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' +__copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms of the AGPLv3 License" + + +import unittest +import ddt + +import octoprint.util + +@ddt.ddt +class TestCaseInsensitiveSet(unittest.TestCase): + + def setUp(self): + self.set = octoprint.util.CaseInsensitiveSet("A", "FoO", True, 23) + + @ddt.data("A", "a", "foo", True, 23) + def test_contained(self, value): + self.assertTrue(value in self.set) + + @ddt.data("b", "fnord", False, 42) + def test_not_contained(self, value): + self.assertFalse(value in self.set)