An MH mailbox.
| 990 | |
| 991 | |
| 992 | class MH(Mailbox): |
| 993 | """An MH mailbox.""" |
| 994 | |
| 995 | def __init__(self, path, factory=None, create=True): |
| 996 | """Initialize an MH instance.""" |
| 997 | Mailbox.__init__(self, path, factory, create) |
| 998 | if not os.path.exists(self._path): |
| 999 | if create: |
| 1000 | os.mkdir(self._path, 0o700) |
| 1001 | os.close(os.open(os.path.join(self._path, '.mh_sequences'), |
| 1002 | os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)) |
| 1003 | else: |
| 1004 | raise NoSuchMailboxError(self._path) |
| 1005 | self._locked = False |
| 1006 | |
| 1007 | def add(self, message): |
| 1008 | """Add message and return assigned key.""" |
| 1009 | keys = self.keys() |
| 1010 | if len(keys) == 0: |
| 1011 | new_key = 1 |
| 1012 | else: |
| 1013 | new_key = max(keys) + 1 |
| 1014 | new_path = os.path.join(self._path, str(new_key)) |
| 1015 | f = _create_carefully(new_path) |
| 1016 | closed = False |
| 1017 | try: |
| 1018 | if self._locked: |
| 1019 | _lock_file(f) |
| 1020 | try: |
| 1021 | try: |
| 1022 | self._dump_message(message, f) |
| 1023 | except BaseException: |
| 1024 | # Unlock and close so it can be deleted on Windows |
| 1025 | if self._locked: |
| 1026 | _unlock_file(f) |
| 1027 | _sync_close(f) |
| 1028 | closed = True |
| 1029 | os.remove(new_path) |
| 1030 | raise |
| 1031 | if isinstance(message, MHMessage): |
| 1032 | self._dump_sequences(message, new_key) |
| 1033 | finally: |
| 1034 | if self._locked: |
| 1035 | _unlock_file(f) |
| 1036 | finally: |
| 1037 | if not closed: |
| 1038 | _sync_close(f) |
| 1039 | return new_key |
| 1040 | |
| 1041 | def remove(self, key): |
| 1042 | """Remove the keyed message; raise KeyError if it doesn't exist.""" |
| 1043 | path = os.path.join(self._path, str(key)) |
| 1044 | try: |
| 1045 | f = open(path, 'rb+') |
| 1046 | except OSError as e: |
| 1047 | if e.errno == errno.ENOENT: |
| 1048 | raise KeyError('No message with key: %s' % key) |
| 1049 | else: |
no outgoing calls
no test coverage detected