From 9d513123cdc50608208200c77052db58fbb8619a Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Sun, 11 Mar 2012 18:43:41 +0000 Subject: refactor: Attachment in its own file --- alot/db/envelope.py | 12 +++++----- alot/db/message.py | 68 +---------------------------------------------------- 2 files changed, 7 insertions(+), 73 deletions(-) (limited to 'alot/db') diff --git a/alot/db/envelope.py b/alot/db/envelope.py index 4af1a05b..6572b507 100644 --- a/alot/db/envelope.py +++ b/alot/db/envelope.py @@ -11,7 +11,7 @@ import logging import alot.helper as helper from alot.settings import settings -from message import Attachment +from attachment import Attachment from message import encode_header @@ -29,7 +29,7 @@ class Envelope(object): :param headers: unencoded header values :type headers: dict (str -> unicode) :param attachments: file attachments to include - :type attachments: list of :class:`Attachment` + :type attachments: list of :class:`~alot.db.attachment.Attachment` """ assert isinstance(bodytext, unicode) self.headers = {} @@ -107,11 +107,11 @@ class Envelope(object): """ attach a file - :param attachment: File to attach, given as :class:`Attachment` object - or path to a file. - :type attachment: :class:`Attachment` or str + :param attachment: File to attach, given as + :class:`~alot.db.attachment.Attachment` object or path to a file. + :type attachment: :class:`~alot.db.attachment.Attachment` or str :param filename: filename to use in content-disposition. - Will be ignored if `path` matches multiple files + Will be ignored if `path` matches multiple files :param ctype: force content-type to be used for this attachment :type ctype: str """ diff --git a/alot/db/message.py b/alot/db/message.py index 08162379..54b72241 100644 --- a/alot/db/message.py +++ b/alot/db/message.py @@ -19,6 +19,7 @@ from alot.settings import settings from alot.helper import string_sanitize from alot.helper import string_decode +from attachment import Attachment class Message(object): """ @@ -400,70 +401,3 @@ def encode_header(key, value): else: value = Header(value) return value - - -class Attachment(object): - """represents a mail attachment""" - - def __init__(self, emailpart): - """ - :param emailpart: a non-multipart email that is the attachment - :type emailpart: :class:`email.message.Message` - """ - self.part = emailpart - - def __str__(self): - desc = '%s:%s (%s)' % (self.get_content_type(), - self.get_filename(), - helper.humanize_size(self.get_size())) - return string_decode(desc) - - def get_filename(self): - """ - return name of attached file. - If the content-disposition header contains no file name, - this returns `None` - """ - extracted_name = decode_header(self.part.get_filename()) - if extracted_name: - return os.path.basename(extracted_name) - return None - - def get_content_type(self): - """mime type of the attachment part""" - ctype = self.part.get_content_type() - # replace underspecified mime description by a better guess - if ctype in ['octet/stream', 'application/octet-stream']: - ctype = helper.guess_mimetype(self.get_data()) - return ctype - - def get_size(self): - """returns attachments size in bytes""" - return len(self.part.get_payload()) - - def save(self, path): - """ - save the attachment to disk. Uses :meth:`get_filename` in case path - is a directory - """ - filename = self.get_filename() - path = os.path.expanduser(path) - if os.path.isdir(path): - if filename: - basename = os.path.basename(filename) - FILE = open(os.path.join(path, basename), "w") - else: - FILE = tempfile.NamedTemporaryFile(delete=False, dir=path) - else: - FILE = open(path, "w") # this throws IOErrors for invalid path - FILE.write(self.get_data()) - FILE.close() - return FILE.name - - def get_data(self): - """return data blob from wrapped file""" - return self.part.get_payload(decode=True) - - def get_mime_representation(self): - """returns mime part that constitutes this attachment""" - return self.part -- cgit v1.2.3