From 9398560306f5609bcf1d3fdda6511cdb3b67d3f3 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Wed, 24 Nov 2021 15:36:28 +0100 Subject: helper: move guess_mimetype into its own file --- alot/db/message.py | 4 ++-- alot/helper.py | 40 ---------------------------------------- alot/mail/envelope.py | 4 ++-- alot/utils/magic.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 44 deletions(-) create mode 100644 alot/utils/magic.py diff --git a/alot/db/message.py b/alot/db/message.py index 01d7c7f1..f737721c 100644 --- a/alot/db/message.py +++ b/alot/db/message.py @@ -17,9 +17,9 @@ from urwid.util import detected_encoding from ..mail.attachment import Attachment from ..mail.policy import p as policy from .. import crypto -from .. import helper from ..errors import GPGProblem from ..settings.const import settings +from ..utils.magic import guess_mimetype from ..utils.mailcap import MailcapHandler charset.add_charset('utf-8', charset.QP, charset.QP, 'utf-8') @@ -262,7 +262,7 @@ class _MimeTree: # replace underspecified mime description by a better guess if ctype in ['octet/stream', 'application/octet-stream', 'application/octetstream']: - ctype = helper.guess_mimetype(self._part.get_content()) + ctype = guess_mimetype(self._part.get_content()) logging.debug('Overriding octet-stream content type to %s', ctype) maintype, _, subtype = ctype.partition('/') diff --git a/alot/helper.py b/alot/helper.py index 08df3667..43732b24 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -6,12 +6,9 @@ from collections import deque import logging import os -import re import shlex import email -import magic - def split_commandline(s): """ @@ -120,43 +117,6 @@ def shorten_author_string(authors_string, maxlength): authorsstring = ', '.join(authors_chain) return authorsstring - -def guess_mimetype(blob): - """ - uses file magic to determine the mime-type of the given data blob. - - :param blob: file content as read by file.read() - :type blob: data - :returns: mime-type, falls back to 'application/octet-stream' - :rtype: str - """ - mimetype = 'application/octet-stream' - # this is a bit of a hack to support different versions of python magic. - # Hopefully at some point this will no longer be necessary - # - # the version with open() is the bindings shipped with the file source from - # http://darwinsys.com/file/ - this is what is used by the python-magic - # package on Debian/Ubuntu. However, it is not available on pypi/via pip. - # - # the version with from_buffer() is available at - # https://github.com/ahupp/python-magic and directly installable via pip. - # - # for more detail see https://github.com/pazz/alot/pull/588 - if hasattr(magic, 'open'): - m = magic.open(magic.MAGIC_MIME_TYPE) - m.load() - magictype = m.buffer(blob) - elif hasattr(magic, 'from_buffer'): - # cf. issue #841 - magictype = magic.from_buffer(blob, mime=True) or magictype - else: - raise Exception('Unknown magic API') - - # libmagic does not always return proper mimetype strings, cf. issue #459 - if re.match(r'\w+\/\w+', magictype): - mimetype = magictype - return mimetype - def shell_quote(text): """Escape the given text for passing it to the shell for interpretation. The resulting string will be parsed into one "word" (in the sense used in diff --git a/alot/mail/envelope.py b/alot/mail/envelope.py index d3cace18..0a616a57 100644 --- a/alot/mail/envelope.py +++ b/alot/mail/envelope.py @@ -17,10 +17,10 @@ from . import headers as HDR from .attachment import Attachment from .. import __version__ -from .. import helper from .. import crypto from ..settings.const import settings from ..errors import GPGProblem, GPGCode +from ..utils.magic import guess_mimetype charset.add_charset('utf-8', charset.QP, charset.QP, 'utf-8') @@ -239,7 +239,7 @@ class Envelope: with open(path, 'rb') as f: data = f.read() - ctype = helper.guess_mimetype(data) + ctype = guess_mimetype(data) params = [] diff --git a/alot/utils/magic.py b/alot/utils/magic.py new file mode 100644 index 00000000..b6bbcfb2 --- /dev/null +++ b/alot/utils/magic.py @@ -0,0 +1,44 @@ +# This file is released under the GNU GPL, version 3 or a later revision. +# For further details see the COPYING file + +import re + +import magic + + +def guess_mimetype(blob): + """ + uses file magic to determine the mime-type of the given data blob. + + :param blob: file content as read by file.read() + :type blob: data + :returns: mime-type, falls back to 'application/octet-stream' + :rtype: str + """ + mimetype = 'application/octet-stream' + # this is a bit of a hack to support different versions of python magic. + # Hopefully at some point this will no longer be necessary + # + # the version with open() is the bindings shipped with the file source from + # http://darwinsys.com/file/ - this is what is used by the python-magic + # package on Debian/Ubuntu. However, it is not available on pypi/via pip. + # + # the version with from_buffer() is available at + # https://github.com/ahupp/python-magic and directly installable via pip. + # + # for more detail see https://github.com/pazz/alot/pull/588 + if hasattr(magic, 'open'): + m = magic.open(magic.MAGIC_MIME_TYPE) + m.load() + magictype = m.buffer(blob) + elif hasattr(magic, 'from_buffer'): + # cf. issue #841 + magictype = magic.from_buffer(blob, mime=True) or magictype + else: + raise Exception('Unknown magic API') + + # libmagic does not always return proper mimetype strings, cf. issue #459 + if re.match(r'\w+\/\w+', magictype): + mimetype = magictype + return mimetype + -- cgit v1.2.3