diff --git a/adminapi/dataset.py b/adminapi/dataset.py index 3e3c5f433..22a992f4c 100644 --- a/adminapi/dataset.py +++ b/adminapi/dataset.py @@ -3,7 +3,6 @@ Copyright (c) 2019 InnoGames GmbH """ -from distutils.util import strtobool from ipaddress import IPv4Address, IPv4Network, IPv6Address, IPv6Network from itertools import chain from types import GeneratorType @@ -461,7 +460,7 @@ def set(self, key, value): if isinstance(self[key], MultiAttr): self[key].add(value) elif type(self[key]) is bool: - self[key] = bool(strtobool(value)) + self[key] = strtobool(value) elif type(self[key]) is int: self[key] = int(value) else: @@ -595,3 +594,21 @@ def _format_attribute_value(value): if isinstance(value, dict): return _format_obj(value) return json_to_datatype(value) + + +def strtobool(val) -> bool: + """ + Convert a string representation of truth to true or false. + Acts the same as distutils.util.strtobool, which was removed in Python 3.12. + + True values are 'y', 'yes', 't', 'true', 'on', and '1'; + false values are 'n', 'no', 'f', 'false', 'off', and '0'. + Raises ValueError if 'val' is anything else. + """ + val = val.lower() + if val in ('y', 'yes', 't', 'true', 'on', '1'): + return True + elif val in ('n', 'no', 'f', 'false', 'off', '0'): + return False + else: + raise ValueError(f"invalid truth value {val}") \ No newline at end of file diff --git a/adminapi/tests/test_dataset.py b/adminapi/tests/test_dataset.py new file mode 100644 index 000000000..132e14ee3 --- /dev/null +++ b/adminapi/tests/test_dataset.py @@ -0,0 +1,24 @@ +import unittest + +from adminapi.dataset import strtobool + + +class TestStrtobool(unittest.TestCase): + def test_true_values(self): + true_values = ["y", "yes", "t", "true", "on", "1"] + for value in true_values: + with self.subTest(value=value): + self.assertTrue(strtobool(value)) + + def test_false_values(self): + false_values = ["n", "no", "f", "false", "off", "0"] + for value in false_values: + with self.subTest(value=value): + self.assertFalse(strtobool(value)) + + def test_invalid_values(self): + invalid_values = ["maybe", "2", "none", "", "Yess"] + for value in invalid_values: + with self.subTest(value=value): + with self.assertRaises(ValueError): + strtobool(value)