From 5cf360add8cdbbdc4ca1c620d5c04a8af8956ace Mon Sep 17 00:00:00 2001 From: Folker Bernitt Date: Tue, 12 Jan 2016 15:25:42 +0100 Subject: Refactor AttachmentInfo: Remove knowledge about part map structure --- .../pixelated/adapter/mailstore/leap_mailstore.py | 23 +++++++++++++++------- .../test/unit/adapter/mailstore/test_leap_mail.py | 5 ++--- .../unit/adapter/mailstore/test_leap_mailstore.py | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index d550ec60..c2912bbe 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -29,12 +29,12 @@ from pixelated.support.functional import to_unicode class AttachmentInfo(object): - def __init__(self, part_map, headers): - self.ident = part_map['phash'] - self.name = _extract_filename(headers) - self.encoding = headers.get('Content-Transfer-Encoding', None) - self.ctype = part_map.get('ctype') or headers.get('Content-Type') - self.size = part_map.get('size', 0) + def __init__(self, ident, name, encoding=None, ctype='application/octet-stream', size=0): + self.ident = ident + self.name = name + self.encoding = encoding + self.ctype = ctype + self.size = size def __repr__(self): return 'AttachmentInfo[%s, %s, %s]' % (self.ident, self.name, self.encoding) @@ -402,6 +402,15 @@ class LeapMailStore(MailStore): return True + def _create_attachment_info_from(self, part_map, headers): + ident = part_map['phash'] + name = _extract_filename(headers) + encoding = headers.get('Content-Transfer-Encoding', None) + ctype = part_map.get('ctype') or headers.get('Content-Type') + size = part_map.get('size', 0) + + return AttachmentInfo(ident, name, encoding, ctype, size) + def _extract_part_map(self, part_maps): result = [] @@ -409,7 +418,7 @@ class LeapMailStore(MailStore): if 'headers' in part_map and 'phash' in part_map: headers = {header[0]: header[1] for header in part_map['headers']} if self._is_attachment(part_map, headers): - result.append(AttachmentInfo(part_map, headers)) + result.append(self._create_attachment_info_from(part_map, headers)) if 'part_map' in part_map: result += self._extract_part_map(part_map['part_map']) diff --git a/service/test/unit/adapter/mailstore/test_leap_mail.py b/service/test/unit/adapter/mailstore/test_leap_mail.py index 4b074082..91e960e1 100644 --- a/service/test/unit/adapter/mailstore/test_leap_mail.py +++ b/service/test/unit/adapter/mailstore/test_leap_mail.py @@ -85,9 +85,8 @@ class TestLeapMail(TestCase): self.assertEqual(body, mail.as_dict()['body']) def test_as_dict_with_attachments(self): - mail = LeapMail('doc id', 'INBOX', attachments=[AttachmentInfo({'phash': 'id', 'ctype': 'text/plain', 'size': 2}, - {'Content-Description': 'name', - 'Content-Transfer-Encoding': 'encoding'})]) + attachment_info = AttachmentInfo('id', 'name', 'encoding', ctype='text/plain', size=2) + mail = LeapMail('doc id', 'INBOX', attachments=[attachment_info]) self.assertEqual([{'ident': 'id', 'name': 'name', 'encoding': 'encoding', 'content-type': 'text/plain', 'size': 2}], mail.as_dict()['attachments']) diff --git a/service/test/unit/adapter/mailstore/test_leap_mailstore.py b/service/test/unit/adapter/mailstore/test_leap_mailstore.py index 1eae48df..0945e0c4 100644 --- a/service/test/unit/adapter/mailstore/test_leap_mailstore.py +++ b/service/test/unit/adapter/mailstore/test_leap_mailstore.py @@ -37,7 +37,7 @@ import pkg_resources from leap.mail.mail import Message from pixelated.adapter.mailstore import underscore_uuid -from pixelated.adapter.mailstore.leap_mailstore import LeapMailStore, LeapMail, AttachmentInfo +from pixelated.adapter.mailstore.leap_mailstore import LeapMailStore, LeapMail class TestLeapMailStore(TestCase): -- cgit v1.2.3