From: Volker Theile Date: Thu, 28 Jun 2018 09:01:22 +0000 (+0200) Subject: mgr/dashboard: Improve str_to_bool X-Git-Tag: v14.0.1~956^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F22757%2Fhead;p=ceph.git mgr/dashboard: Improve str_to_bool Signed-off-by: Volker Theile --- diff --git a/src/pybind/mgr/dashboard/tools.py b/src/pybind/mgr/dashboard/tools.py index a2a5f3ef4765..aafed807d7c8 100644 --- a/src/pybind/mgr/dashboard/tools.py +++ b/src/pybind/mgr/dashboard/tools.py @@ -7,6 +7,7 @@ import functools import collections from datetime import datetime, timedelta +from distutils.util import strtobool import fnmatch import time import threading @@ -741,7 +742,25 @@ def getargspec(func): return _getargspec(func) -def str_to_bool(var): - if isinstance(var, bool): - return var - return var.lower() in ("true", "yes", "1", 1) +def str_to_bool(val): + """ + Convert a string representation of truth to True or False. + + >>> str_to_bool('true') and str_to_bool('yes') and str_to_bool('1') and str_to_bool(True) + True + + >>> str_to_bool('false') and str_to_bool('no') and str_to_bool('0') and str_to_bool(False) + False + + >>> str_to_bool('xyz') + Traceback (most recent call last): + ... + ValueError: invalid truth value 'xyz' + + :param val: The value to convert. + :type val: str|bool + :rtype: bool + """ + if isinstance(val, bool): + return val + return bool(strtobool(val))