Let truthystring take a fallback.

master
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 = '\\/:*?<>|"'
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')}
USER_ID_CHARACTERS = string.digits + string.ascii_uppercase

View File

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