Introduce CaseInsensitiveSet & use for valid boolean trues
Follow up to #2385
This commit is contained in:
1 parent
b3b19dc136
commit
ec12bd7cd4
4 files changed
+85
-3
No files matched your search
@@ -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."""
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
Reference in new issue
Block a user