diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2012-04-05 19:46:29 +0100 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2012-04-05 19:46:29 +0100 |
commit | c27326c6f85c0830a955706bfebee4368b7cd8e1 (patch) | |
tree | 992a244ac906d7ff6a6acd1af0199450e5efd664 | |
parent | 70ecb1582fa90abb437314242bb13c50d4dd9d96 (diff) | |
parent | 49f028101342d0b531da9833693f773f19297341 (diff) |
Merge branch '0.3-fix-attachments-419' into staging
-rw-r--r-- | alot/commands/thread.py | 28 | ||||
-rw-r--r-- | alot/db/attachment.py | 6 | ||||
-rw-r--r-- | alot/db/utils.py | 14 | ||||
-rw-r--r-- | alot/helper.py | 15 |
4 files changed, 46 insertions, 17 deletions
diff --git a/alot/commands/thread.py b/alot/commands/thread.py index 45734b21..41fbac18 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -6,6 +6,7 @@ import shlex import re import subprocess from email.Utils import parseaddr +import mailcap from alot.commands import Command, registerCommand from alot.commands.globals import ExternalCommand @@ -23,6 +24,7 @@ from alot.db.attachment import Attachment from alot.db.errors import DatabaseROError from alot.settings import settings +from alot.helper import parse_mailcap_nametemplate MODE = 'thread' @@ -661,19 +663,33 @@ class OpenAttachmentCommand(Command): logging.info('open attachment') mimetype = self.attachment.get_content_type() - handler = settings.get_mime_handler(mimetype) + handler, entry = settings.mailcap_find_match(mimetype) if handler: - path = self.attachment.save(tempfile.gettempdir()) - handler = re.sub('\'?%s\'?', '{}', handler) + nametemplate = entry.get('nametemplate', '%s') + prefix, suffix = parse_mailcap_nametemplate(nametemplate) + tmpfile = tempfile.NamedTemporaryFile(delete=False, + prefix=prefix, + suffix=suffix) + + self.attachment.write(tmpfile) + tmpfile.close() + + # read parameter, create handler command + part = self.attachment.get_mime_representation() + parms = tuple(map('='.join, part.get_params())) + + # create and call external command + handler = mailcap.subst(entry['view'], mimetype, + filename=tmpfile.name, plist=parms) # 'needsterminal' makes handler overtake the terminal - nt = settings.get_mime_handler(mimetype, key='needsterminal') + nt = entry.get('needsterminal', None) overtakes = (nt is None) def afterwards(): - os.remove(path) + os.remove(tmpfile.name) - ui.apply_command(ExternalCommand(handler, path=path, + ui.apply_command(ExternalCommand(handler, path=tmpfile.name, on_success=afterwards, thread=overtakes)) else: diff --git a/alot/db/attachment.py b/alot/db/attachment.py index 905e66b8..0fe8dc8b 100644 --- a/alot/db/attachment.py +++ b/alot/db/attachment.py @@ -62,10 +62,14 @@ class Attachment(object): FILE = tempfile.NamedTemporaryFile(delete=False, dir=path) else: FILE = open(path, "w") # this throws IOErrors for invalid path - FILE.write(self.get_data()) + self.write(FILE) FILE.close() return FILE.name + def write(self, fhandle): + """writes content to a given filehandle""" + fhandle.write(self.get_data()) + def get_data(self): """return data blob from wrapped file""" return self.part.get_payload(decode=True) diff --git a/alot/db/utils.py b/alot/db/utils.py index 0d28783f..4007606c 100644 --- a/alot/db/utils.py +++ b/alot/db/utils.py @@ -14,6 +14,7 @@ import alot.helper as helper from alot.settings import settings from alot.helper import string_sanitize from alot.helper import string_decode +from alot.helper import parse_mailcap_nametemplate def extract_headers(mail, headers=None): @@ -82,17 +83,10 @@ def extract_body(mail, types=None): if entry: # open tempfile, respect mailcaps nametemplate nametemplate = entry.get('nametemplate', '%s') - nt_list = nametemplate.split('%s') - template_prefix = '' - template_suffix = '' - if len(nt_list) == 2: - template_suffix = nt_list[1] - template_prefix = nt_list[0] - else: - template_suffix = nametemplate + prefix, suffix = parse_mailcap_nametemplate(nametemplate) tmpfile = tempfile.NamedTemporaryFile(delete=False, - prefix=template_prefix, - suffix=template_suffix) + prefix=prefix, + suffix=suffix) # write payload to tmpfile tmpfile.write(raw_payload) tmpfile.close() diff --git a/alot/helper.py b/alot/helper.py index 91aae324..85e527c3 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -17,6 +17,7 @@ from twisted.internet.protocol import ProcessProtocol from twisted.internet.defer import Deferred import StringIO import logging +import tempfile def safely_get(clb, E, on_error=''): @@ -396,3 +397,17 @@ def humanize_size(size): if size / factor < 1024: return format_string % (float(size) / factor) return format_string % (size / factor) + + +def parse_mailcap_nametemplate(tmplate='%s'): + """this returns a prefix and suffix to be used + in the tempfile module for a given mailcap nametemplate string""" + nt_list = tmplate.split('%s') + template_prefix = '' + template_suffix = '' + if len(nt_list) == 2: + template_suffix = nt_list[1] + template_prefix = nt_list[0] + else: + template_suffix = tmplate + return (template_prefix, template_suffix) |