From 2d3d56a2bfb0493bf8475742b6d2fc7cc762bbb5 Mon Sep 17 00:00:00 2001 From: Jonathan Dowland Date: Thu, 29 May 2014 20:13:08 +0100 Subject: [PATCH 1/4] add delete() methods to mailbox types Add delete() methods to mailbox classes, or common base classes. This removes any mail in the mailbox and the underlying files or directories on the filesystem. If the mailbox has sub-folders, they are not removed, and their parent directory is preserved. --- mailbox.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/mailbox.py b/mailbox.py index dde597c..bed7008 100644 --- a/mailbox.py +++ b/mailbox.py @@ -203,6 +203,10 @@ class Mailbox: raise ValueError("String input must be ASCII-only; " "use bytes or a Message instead") + def delete(self): + """Delete the mailbox.""" + raise NotImplementedError('Method must be implemented by subclass') + # Whether each message must end in a newline _append_newline = False @@ -429,6 +433,15 @@ class Maildir(Mailbox): """Flush and close the mailbox.""" return + def delete(self): # XXX: handle known cache/index files + """Delete the Maildir and any mail within, but not any sub-folders.""" + for k in self.keys(): + self.discard(k) + for d in ['tmp','new','cur']: + os.rmdir(os.path.join(self._path,d)) + if len(self.list_folders()) == 0: + os.rmdir(self._path) + def list_folders(self): """Return a list of folder names.""" result = [] @@ -612,6 +625,10 @@ class _singlefileMailbox(Mailbox): del self._toc[key] self._pending = True + def delete(self): + """Delete the mailbox and any mail within.""" + os.remove(self._path) + def __setitem__(self, key, message): """Replace the keyed message; raise KeyError if it doesn't exist.""" self._lookup(key) @@ -939,6 +956,15 @@ class MH(Mailbox): raise NoSuchMailboxError(self._path) self._locked = False + def delete(self): + """Delete MH mailbox (unless sub-folders exist) and any mail within.""" + self.lock() + for k in self.keys(): + self.remove(key) + self.unlock() + if len(self.list_folders()) == 0: + os.rmdir(self._path) + def add(self, message): """Add message and return assigned key.""" keys = self.keys() -- 1.8.5.2 (Apple Git-48)