diff options
-rw-r--r-- | alot/helper.py | 7 | ||||
-rw-r--r-- | alot/settings.py | 23 | ||||
-rw-r--r-- | alot/widgets.py | 22 |
3 files changed, 43 insertions, 9 deletions
diff --git a/alot/helper.py b/alot/helper.py index 06fa986e..2fdf6d5f 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -19,6 +19,9 @@ Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com> from datetime import date from datetime import timedelta +import shlex +import subprocess + def shorten(string, maxlen): if len(string) > maxlen - 3: @@ -38,3 +41,7 @@ def pretty_datetime(d): else: string = d.strftime('%b %d') return string + +def cmd_output(command_line): + args = shlex.split(command_line) + return subprocess.check_output(args) diff --git a/alot/settings.py b/alot/settings.py index 75977a05..3cc196e3 100644 --- a/alot/settings.py +++ b/alot/settings.py @@ -18,7 +18,7 @@ Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com> """ import imp import os -import logging +import mailcap from ConfigParser import SafeConfigParser @@ -193,15 +193,16 @@ class CustomConfigParser(SafeConfigParser): config = CustomConfigParser(DEFAULTS) +mailcaps = mailcap.getcaps() def setup(configfilename): config.read(os.path.expanduser(configfilename)) - if config.has_option('general','hooksfile'): - hf = os.path.expanduser(config.get('general','hooksfile')) + if config.has_option('general', 'hooksfile'): + hf = os.path.expanduser(config.get('general', 'hooksfile')) if hf is not None: try: - config.hooks = imp.load_source('hooks',hf) + config.hooks = imp.load_source('hooks', hf) except: pass @@ -223,9 +224,19 @@ def get_palette(): def get_hook(hookname): h = None if config.hooks: - logging.info("hooks there") if config.hooks.__dict__: - logging.info("hooks is module") if hookname in config.hooks.__dict__: h = config.hooks.__dict__[hookname] return h + + +def get_mime_handler(mime_type, key, interactive=True): + if interactive: + mc_tuple = mailcap.findmatch(mailcaps, + mime_type, + key=key) + else: + mc_tuple = mailcap.findmatch(mailcaps, + mime_type, + key='copiousoutput') + return mc_tuple[1][key] diff --git a/alot/widgets.py b/alot/widgets.py index b45a0825..c5ca43fd 100644 --- a/alot/widgets.py +++ b/alot/widgets.py @@ -18,6 +18,8 @@ Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com> """ import email import urwid +import tempfile +import os from urwid import Text from urwid import Edit from urwid import Pile @@ -29,8 +31,10 @@ from urwid import SimpleListWalker from datetime import datetime from settings import config +from settings import get_mime_handler from helper import shorten from helper import pretty_datetime +from helper import cmd_output class ThreadlineWidget(AttrMap): @@ -354,9 +358,21 @@ class MessageBodyWidget(AttrMap): if ctype == 'text/plain': bodytxt += part.get_payload(None, True) elif ctype == 'text/html': - #TODO: call external render - bodytxt += part.get_payload(None, True) - + #get mime handler + handler = get_mime_handler(ctype, key='view', + interactive=False) + #open tempfile: + tmpfile = tempfile.NamedTemporaryFile(delete=False, + suffix='.html') + #write payload to tmpfile + tmpfile.write(part.get_payload(None, True)) + #create and call external command + cmd = handler % tmpfile.name + rendered = cmd_output(cmd) + #remove tempfile + tmpfile.close() + os.unlink(tmpfile.name) + bodytxt += rendered AttrMap.__init__(self, Text(bodytxt), 'message_body') def selectable(self): |