def read_mailboxes(self, mailboxes): """Return list of mailboxes. Raise exception on invalid mailbox. """ mboxes = [] state = [] unread = [] if mailboxes: for mdir in mailboxes: try: mbox = Maildir(mdir, create=False) mbox.keys() mboxes.append(mbox) state.append('') unread.append(0) except NoSuchMailboxError: raise MailstatusException( "invalid path: {path}".format(path=mdir)) except FileNotFoundError: raise MailstatusException( "invalid maildir: {path}".format(path=mdir)) self.mboxes = mboxes self.mbox_state = state self.unread = unread
def add_file(go, path): if not go: print(f"Would add {path}") return # Open the mailbox md = Maildir(DEST.joinpath(DEST_FOLDER), create=False) # Determine the UID for the next message uid_re = re.compile(",U=([0-9]+),") uid = 1 + max( chain([0], map(lambda key: int(uid_re.search(key).groups()[0]), md.keys()))) print(f"Next UID: {uid}") # Determine the time when the message was originally delivered msg_bytes = path.read_bytes() msg = MaildirMessage(msg_bytes) msg_time = time.mktime(parsedate(msg["Date"])) # Create a Maildir filename in the format used by OfflineIMAP key = (f"{int(msg_time)}_0.{os.getpid()}.{socket.gethostname()},U={uid}," f"FMD5={md5(DEST_FOLDER.encode()).hexdigest()}:2,S") # Complete path dest = DEST.joinpath(DEST_FOLDER, "cur", key) assert not dest.exists() and dest.parent.exists() # Write the file print(f"Write {key}") dest.write_bytes(msg_bytes) # Update the utime os.utime(dest, (msg_time, msg_time))
class MaildirAdapter(Mapping): def __init__(self, maildir_path): self.mbox = Maildir(maildir_path, create=False) def __getitem__(self, key): actual_key = unquote(key) msg = self.mbox[actual_key] return MessageAdapter(msg) def __iter__(self): return iter(map(quote, self.mbox.keys())) def __len__(self): return len(self.mbox)