From 423536fec98cc3140bd2f161ed808d38ddb6c08a Mon Sep 17 00:00:00 2001 From: Antoine Amarilli Date: Tue, 23 Oct 2012 19:43:11 +0200 Subject: Add body_mimetype to tweak extract_body's defaults Add a body_mimetype global configuration option to choose the default message part to return as body in extract_body when types is None. Parts of the preferred type will be returned if present, all text/* parts will be returned if none parts of the preferred type exist. The default is "text/html", which should result in the same behavior as before. --- alot/db/message.py | 4 +--- alot/db/utils.py | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'alot') diff --git a/alot/db/message.py b/alot/db/message.py index 35a36aa1..65ef2dc8 100644 --- a/alot/db/message.py +++ b/alot/db/message.py @@ -243,9 +243,7 @@ class Message(object): """ returns bodystring extracted from this mail """ - #TODO: don't hardcode which part is considered body but allow toggle - # commands and a config default setting - + #TODO: allow toggle commands to decide which part is considered body return extract_body(self.get_email()) def get_text_content(self): diff --git a/alot/db/utils.py b/alot/db/utils.py index 8f9de4c9..03435a24 100644 --- a/alot/db/utils.py +++ b/alot/db/utils.py @@ -46,20 +46,21 @@ def extract_body(mail, types=None): """ returns a body text string for given mail. If types is `None`, `text/*` is used: - In case mail has a `text/html` part, it is prefered over - `text/plain` parts. + The exact preferred type is specified by the body_mimetype config option + which defaults to text/html. :param mail: the mail to use :type mail: :class:`email.Message` :param types: mime content types to use for body string :type types: list of str """ - html = list(typed_subpart_iterator(mail, 'text', 'html')) - # if no specific types are given, we favor text/html over text/plain - drop_plaintext = False - if html and not types: - drop_plaintext = True + preferred = settings.get('body_mimetype', 'text/html') + has_preferred = False + + # see if the mail has our preferred type + if types == None: + has_preferred = list(typed_subpart_iterator(mail, *preferred.split('/'))) body_parts = [] for part in mail.walk(): @@ -71,10 +72,14 @@ def extract_body(mail, types=None): cd = part.get('Content-Disposition', '') if cd.startswith('attachment'): continue + # if the mail has our preferred type, we only keep this type + # note that if types != None, has_preferred always stays False + if has_preferred and ctype != preferred: + continue enc = part.get_content_charset() or 'ascii' raw_payload = part.get_payload(decode=True) - if ctype == 'text/plain' and not drop_plaintext: + if ctype == 'text/plain': raw_payload = string_decode(raw_payload, enc) body_parts.append(string_sanitize(raw_payload)) else: -- cgit v1.2.3