From 344abd42c6b480a783ee05b6e92532a1113a86d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Touceda?= Date: Thu, 21 Mar 2013 10:47:37 -0300 Subject: Wrap bool setting casting in a method --- src/leap/config/leapsettings.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'src/leap/config') diff --git a/src/leap/config/leapsettings.py b/src/leap/config/leapsettings.py index b728ce15..35c9fef6 100644 --- a/src/leap/config/leapsettings.py +++ b/src/leap/config/leapsettings.py @@ -29,6 +29,28 @@ from leap.config.prefixers import get_platform_prefixer logger = logging.getLogger(__name__) +def to_bool(val): + """ + Returns the boolean value corresponding to val. Will return False + in case val is not a string or something that behaves like one. + + @param val: value to cast + @type val: either bool already or str + + @rtype: bool + """ + if isinstance(val, bool): + return val + + bool_val = False + try: + bool_val = val.lower() == "true" + except: + pass + + return bool_val + + class LeapSettings(object): """ Leap client QSettings wrapper @@ -153,7 +175,7 @@ class LeapSettings(object): @rtype: bool """ - return self._settings.value(self.AUTOLOGIN_KEY, "false") != "false" + return to_bool(self._settings.value(self.AUTOLOGIN_KEY, False)) def set_autologin(self, autologin): """ @@ -173,8 +195,7 @@ class LeapSettings(object): @rtype: bool """ - return self._settings.value(self.PROPERPROVIDER_KEY, - "false") != "false" + return to_bool(self._settings.value(self.PROPERPROVIDER_KEY, False)) def set_properprovider(self, properprovider): """ -- cgit v1.2.3