summaryrefslogtreecommitdiff
path: root/alot
diff options
context:
space:
mode:
authorpazz <patricktotzke@gmail.com>2011-07-23 18:35:03 +0100
committerpazz <patricktotzke@gmail.com>2011-07-23 18:35:03 +0100
commit475b65bde3ee4a331676a8ac8ebbd2f91d049e22 (patch)
treef70e451fa95cd76cfe0f0f7e96a3ace8b9d5fb9f /alot
parentf51175abbf0ef78209d6525c031a42d0ac706967 (diff)
mv msg interpretation to Message issue #30
Diffstat (limited to 'alot')
-rw-r--r--alot/db.py43
-rw-r--r--alot/widgets.py52
2 files changed, 46 insertions, 49 deletions
diff --git a/alot/db.py b/alot/db.py
index 1502b07d..a6aaac22 100644
--- a/alot/db.py
+++ b/alot/db.py
@@ -22,9 +22,12 @@ import email
from collections import deque
import os
import logging
+import tempfile
from settings import config
+from settings import get_mime_handler
import helper
+from helper import cmd_output
DB_ENC = 'utf8'
@@ -421,6 +424,46 @@ class Message:
self._attachments.append(Attachment(part))
return self._attachments
+ def accumulate_body(self):
+ bodytxt = ''
+ for part in self.get_email().walk():
+ ctype = part.get_content_type()
+ enc = part.get_content_charset()
+ raw_payload = part.get_payload(decode=True)
+ if part.get_content_maintype() == 'text':
+ if enc:
+ raw_payload = unicode(raw_payload, enc)
+ else:
+ raw_payload = unicode(raw_payload, errors='replace')
+ if ctype == 'text/plain':
+ bodytxt += raw_payload
+ else:
+ #get mime handler
+ handler = get_mime_handler(ctype, key='view',
+ interactive=False)
+ if handler:
+ #open tempfile. Not all handlers accept stuff from stdin
+ tmpfile = tempfile.NamedTemporaryFile(delete=False,
+ suffix='.html')
+ #write payload to tmpfile
+ if part.get_content_maintype() == 'text':
+ tmpfile.write(raw_payload.encode('utf8'))
+ else:
+ tmpfile.write(raw_payload)
+ #create and call external command
+ cmd = handler % tmpfile.name
+ rendered_payload = cmd_output(cmd)
+ #remove tempfile
+ tmpfile.close()
+ os.unlink(tmpfile.name)
+ if rendered_payload: # handler had output
+ bodytxt += unicode(rendered_payload.strip(),
+ encoding='utf8', errors='replace')
+ elif part.get_content_maintype() == 'text':
+ bodytxt += raw_payload
+ # else drop
+ return bodytxt
+
class Attachment:
"""represents a single mail attachment"""
diff --git a/alot/widgets.py b/alot/widgets.py
index b1d855f1..da35d6f3 100644
--- a/alot/widgets.py
+++ b/alot/widgets.py
@@ -18,14 +18,9 @@ Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com>
"""
import email
import urwid
-import tempfile
-import os
-import re
-from datetime import datetime
from urwid.command_map import command_map
from settings import config
-from settings import get_mime_handler
from helper import shorten
from helper import pretty_datetime
from helper import cmd_output
@@ -269,7 +264,7 @@ class MessageWidget(urwid.WidgetWrap):
def _get_body_widget(self):
"""creates/returns the widget that displays the mail body"""
if not self.bodyw:
- cols = [MessageBodyWidget(self.message.get_email())]
+ cols = [MessageBodyWidget(self.message)]
bc = list()
if self.depth:
cols.insert(0, self._get_spacer(self.bars_at[1:]))
@@ -448,49 +443,8 @@ class MessageHeaderWidget(urwid.AttrMap):
class MessageBodyWidget(urwid.AttrMap):
"""displays printable parts of an email"""
- def __init__(self, eml):
- """
- :param eml: the email
- :type eml: email.Message
- """
- self.eml = eml
- bodytxt = ''
- for part in self.eml.walk():
- ctype = part.get_content_type()
- enc = part.get_content_charset()
- raw_payload = part.get_payload(decode=True)
- if part.get_content_maintype() == 'text':
- if enc:
- raw_payload = unicode(raw_payload, enc)
- else:
- raw_payload = unicode(raw_payload, errors='replace')
- if ctype == 'text/plain':
- bodytxt += raw_payload
- else:
- #get mime handler
- handler = get_mime_handler(ctype, key='view',
- interactive=False)
- if handler:
- #open tempfile. Not all handlers accept stuff from stdin
- tmpfile = tempfile.NamedTemporaryFile(delete=False,
- suffix='.html')
- #write payload to tmpfile
- if part.get_content_maintype() == 'text':
- tmpfile.write(raw_payload.encode('utf8'))
- else:
- tmpfile.write(raw_payload)
- #create and call external command
- cmd = handler % tmpfile.name
- rendered_payload = cmd_output(cmd)
- #remove tempfile
- tmpfile.close()
- os.unlink(tmpfile.name)
- if rendered_payload: # handler had output
- bodytxt += unicode(rendered_payload.strip(),
- encoding='utf8', errors='replace')
- elif part.get_content_maintype() == 'text':
- bodytxt += raw_payload
- # else drop
+ def __init__(self, msg):
+ bodytxt = msg.accumulate_body()
urwid.AttrMap.__init__(self, urwid.Text(bodytxt), 'message_body')
def selectable(self):