From 62c0e7354d64ddbfbb0130f690a5768185e3eb92 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 21 Feb 2018 09:27:31 -0800 Subject: helper: add a helper to guess the encoding a of a blob and decode it --- alot/helper.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'alot/helper.py') 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 -- cgit v1.2.3