From dda06dd8de5f01b6713f161b4af43a2714187acc Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 15 Jul 2014 00:33:30 +0200 Subject: [PATCH] config: Add optional support to Boolean type --- docs/changelog.rst | 4 ++++ mopidy/config/types.py | 6 ++++++ tests/config/test_types.py | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 14c476b0..c8ce1841 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -31,6 +31,10 @@ Feature release. release, like Mopidy 0.18, or migrate the configuration to the new format by hand. +**Configuration** + +- Add ``optional=True`` support to :class:`mopidy.config.Boolean`. + **Logging** - Fix proper decoding of exception messages that depends on the user's locale. diff --git a/mopidy/config/types.py b/mopidy/config/types.py index a6736371..4498cb67 100644 --- a/mopidy/config/types.py +++ b/mopidy/config/types.py @@ -151,7 +151,13 @@ class Boolean(ConfigValue): true_values = ('1', 'yes', 'true', 'on') false_values = ('0', 'no', 'false', 'off') + def __init__(self, optional=False): + self._required = not optional + def deserialize(self, value): + validators.validate_required(value, self._required) + if not value: + return None if value.lower() in self.true_values: return True elif value.lower() in self.false_values: diff --git a/tests/config/test_types.py b/tests/config/test_types.py index e4a6f22e..dfb439be 100644 --- a/tests/config/test_types.py +++ b/tests/config/test_types.py @@ -214,6 +214,10 @@ class BooleanTest(unittest.TestCase): self.assertEqual(b'false', result) self.assertIsInstance(result, bytes) + def test_deserialize_respects_optional(self): + value = types.Boolean(optional=True) + self.assertEqual(None, value.deserialize('')) + # TODO: test None or other invalid values into serialize?