summaryrefslogtreecommitdiff
path: root/alot/helper.py
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2018-02-21 09:27:31 -0800
committerDylan Baker <dylan@pnwbakers.com>2018-03-01 10:34:56 -0800
commit62c0e7354d64ddbfbb0130f690a5768185e3eb92 (patch)
tree075f1b73530140ad9fec1453f1f8cd25122637a7 /alot/helper.py
parentd61975d90f6cb5372c9973fa6cec340aed0b1502 (diff)
helper: add a helper to guess the encoding a of a blob and decode it
Diffstat (limited to 'alot/helper.py')
-rw-r--r--alot/helper.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/alot/helper.py b/alot/helper.py
index bd84e287..6b67da11 100644
--- a/alot/helper.py
+++ b/alot/helper.py
@@ -412,6 +412,17 @@ def guess_encoding(blob):
raise Exception('Unknown magic API')
+def try_decode(blob):
+ """Guess the encoding of blob and try to decode it into a str.
+
+ :param bytes blob: The bytes to decode
+ :returns: the decoded blob
+ :rtype: str
+ """
+ assert isinstance(blob, bytes), 'cannot decode a str or non-bytes object'
+ return blob.decode(guess_encoding(blob))
+
+
def libmagic_version_at_least(version):
"""
checks if the libmagic library installed is more recent than a given