Let truthystring take a fallback.

This commit is contained in:
voussoir 2021-01-28 17:21:30 -08:00
parent 3795bde5b1
commit 25de9be94a
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
2 changed files with 8 additions and 4 deletions

View file

@ -227,6 +227,7 @@ You can\'t order "{column}" by "{direction}". Defaulting to descending.
FILENAME_BADCHARS = '\\/:*?<>|"' FILENAME_BADCHARS = '\\/:*?<>|"'
TRUTHYSTRING_TRUE = {s.lower() for s in ('1', 'true', 't', 'yes', 'y', 'on')} TRUTHYSTRING_TRUE = {s.lower() for s in ('1', 'true', 't', 'yes', 'y', 'on')}
TRUTHYSTRING_FALSE = {s.lower() for s in ('0', 'false', 'f', 'no', 'n', 'off')}
TRUTHYSTRING_NONE = {s.lower() for s in ('null', 'none')} TRUTHYSTRING_NONE = {s.lower() for s in ('null', 'none')}
USER_ID_CHARACTERS = string.digits + string.ascii_uppercase USER_ID_CHARACTERS = string.digits + string.ascii_uppercase

View file

@ -470,12 +470,13 @@ def split_easybake_string(ebstring):
tagname = tagname.strip('.') tagname = tagname.strip('.')
return (tagname, synonym, rename_to) return (tagname, synonym, rename_to)
def truthystring(s): def truthystring(s, fallback=False):
''' '''
If s is already a boolean, int, or None, return a boolean or None. If s is already a boolean, int, or None, return a boolean or None.
If s is a string, return True, False, or None based on the options presented If s is a string, return True, False, or None based on the options presented
in constants.TRUTHYSTRING_TRUE, constants.TRUTHYSTRING_NONE, or False in constants.TRUTHYSTRING_TRUE, TRUTHYSTRING_FALSE, TRUTHYSTRING_NONE where
for all else. Case insensitive. s is treated case-insensitively.
If s is not in any of those sets, return the fallback.
''' '''
if s is None: if s is None:
return None return None
@ -484,11 +485,13 @@ def truthystring(s):
return bool(s) return bool(s)
if not isinstance(s, str): if not isinstance(s, str):
raise TypeError(f'String should be {bool}, {int}, {str}, or None, not {type(s)}.') return fallback
s = s.lower() s = s.lower()
if s in constants.TRUTHYSTRING_TRUE: if s in constants.TRUTHYSTRING_TRUE:
return True return True
if s in constants.TRUTHYSTRING_FALSE:
return False
if s in constants.TRUTHYSTRING_NONE: if s in constants.TRUTHYSTRING_NONE:
return None return None
return False return False