summary refs log tree commit diff
path: root/synapse/util/stringutils.py
diff options
context:
space:
mode:
authorAndrew Morgan <andrew@amorgan.xyz>2021-04-22 16:34:35 +0100
committerAndrew Morgan <andrew@amorgan.xyz>2021-04-22 16:34:35 +0100
commita31eb4a9fd1c6af6de0c5162d80736eae7bdaba6 (patch)
tree1fd39929c29ecd3836bda570dbf33cc4fe2a8b7d /synapse/util/stringutils.py
parentMerge commit 'd34c6e127' into anoa/dinsic_release_1_31_0 (diff)
parentAdd an admin API endpoint to protect media. (#9086) (diff)
downloadsynapse-a31eb4a9fd1c6af6de0c5162d80736eae7bdaba6.tar.xz
Merge commit '3e4cdfe5d' into anoa/dinsic_release_1_31_0
Diffstat (limited to 'synapse/util/stringutils.py')
-rw-r--r--synapse/util/stringutils.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/synapse/util/stringutils.py b/synapse/util/stringutils.py

index 61d96a6c28..b103c8694c 100644 --- a/synapse/util/stringutils.py +++ b/synapse/util/stringutils.py
@@ -75,3 +75,22 @@ def shortstr(iterable: Iterable, maxitems: int = 5) -> str: if len(items) <= maxitems: return str(items) return "[" + ", ".join(repr(r) for r in items[:maxitems]) + ", ...]" + + +def strtobool(val: str) -> bool: + """Convert a string representation of truth to True or False + + True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values + are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if + 'val' is anything else. + + This is lifted from distutils.util.strtobool, with the exception that it actually + returns a bool, rather than an int. + """ + val = val.lower() + if val in ("y", "yes", "t", "true", "on", "1"): + return True + elif val in ("n", "no", "f", "false", "off", "0"): + return False + else: + raise ValueError("invalid truth value %r" % (val,))