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