diff options
author | Folker Bernitt <fbernitt@thoughtworks.com> | 2014-08-21 14:35:12 +0200 |
---|---|---|
committer | Folker Bernitt <fbernitt@thoughtworks.com> | 2014-08-21 14:35:12 +0200 |
commit | 4a6028ef895034f93b6f8aedd898f1e1542e98b1 (patch) | |
tree | 61e686a04b55028deb534f1c51e639af73aa572a /py-fake-service/app/adapter | |
parent | e7fd4d55992f1cb2f94b9b591f717158930889f5 (diff) |
fake service now supports mbox and RFC-2822 mails.
Diffstat (limited to 'py-fake-service/app/adapter')
-rw-r--r-- | py-fake-service/app/adapter/contacts.py | 2 | ||||
-rw-r--r-- | py-fake-service/app/adapter/mail.py | 2 | ||||
-rw-r--r-- | py-fake-service/app/adapter/mail_service.py | 20 |
3 files changed, 18 insertions, 6 deletions
diff --git a/py-fake-service/app/adapter/contacts.py b/py-fake-service/app/adapter/contacts.py index 5c55b8b1..f997b527 100644 --- a/py-fake-service/app/adapter/contacts.py +++ b/py-fake-service/app/adapter/contacts.py @@ -7,7 +7,7 @@ class Contacts: self.contacts = [] def add(self, mbox_mail): - contact = mbox_mail.get_from() + contact = mbox_mail.from_addr self.contacts.append(Contact(contact)) def search(self, query): diff --git a/py-fake-service/app/adapter/mail.py b/py-fake-service/app/adapter/mail.py index 30a53641..4f8eeb9a 100644 --- a/py-fake-service/app/adapter/mail.py +++ b/py-fake-service/app/adapter/mail.py @@ -56,7 +56,7 @@ class Mail: def _get_headers(self, mbox_mail): headers = {} - headers['from'] = mbox_mail.get_from() + headers['from'] = mbox_mail.from_addr headers['to'] = [mbox_mail.get('To')] headers['subject'] = mbox_mail.get('Subject') headers['date'] = datetime.fromtimestamp( diff --git a/py-fake-service/app/adapter/mail_service.py b/py-fake-service/app/adapter/mail_service.py index 3a4d45cc..66c0d74e 100644 --- a/py-fake-service/app/adapter/mail_service.py +++ b/py-fake-service/app/adapter/mail_service.py @@ -20,17 +20,29 @@ class MailService: self.tagsset = TagsSet() self.contacts = Contacts() + def _read_file(self, filename): + with open(filename, 'r') as fd: + return fd.read() + + def _create_message_from_file(self, filename): + data = self._read_file(filename) + if data.startswith('From '): + msg = mailbox.mbox(filename).popitem()[1] + msg.from_addr = msg.get_from() + else: + msg = mailbox.Message(data) + msg.from_addr = msg.get('From') + return msg + def load_mailset(self): mbox_filenames = [ filename for filename in os.listdir (self.MAILSET_PATH) if filename.startswith('mbox')] - boxes = (mailbox.mbox - (os.path.join(self.MAILSET_PATH, mbox)) + messages = (self._create_message_from_file(os.path.join(self.MAILSET_PATH, mbox)) for mbox in mbox_filenames) - for box in boxes: - message = box.popitem()[1] + for message in messages: self.mailset.add(message) self.tagsset.add(message) self.contacts.add(message) |