File: tempmaildir.py

package info (click to toggle)
rss2email 1%3A3.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,260 kB
  • sloc: python: 2,565; xml: 156; sh: 6; makefile: 2
file content (29 lines) | stat: -rw-r--r-- 848 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import mailbox
import tempfile
from pathlib import Path
from typing import List


class TemporaryMaildir:
    def __init__(self):
        self._tmpdir = tempfile.TemporaryDirectory()
        self.path = Path(self._tmpdir.name)
        mailbox.Maildir(str(self.path), create=True).close()
        self.inbox_name = "inbox"
        self.inbox = mailbox.Maildir(str(self.path.joinpath(self.inbox_name)), create=True)

    def cleanup(self):
        self.inbox.close()
        self._tmpdir.cleanup()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.cleanup()

    def inbox_messages(self) -> List[mailbox.MaildirMessage]:
        """
        :return: Messages from the inbox sorted by date ascending.
        """
        return sorted(self.inbox.itervalues(), key=(lambda x: x.get_date()))