How can I safely, and hopefully with the standard python tools for Maildir, find the complete filename and path of a Maildir message. I want to write the mail-message-file to a tarfile before I delete it.
I'm importing mailbox and tarfile (++)
Context:
A list of Maildirs (created from a text file)
Looping over the Maildirs (simplified), creating a list over mails to delete, a function will take the list of mails, and one by one, append the emails to a tarfile, then delete the email.
import mailbox
# Creating the list
for maildir in maildir_list
inbox = mailbox.Maildir(maildir, factory=None, create=False)
# Looping over folders, if (folder = Trash)
for key,msg in inbox.iteritems():
my_list.append(maildir, inbox, key, foldername
Then looping over the list to append the mails to a tarfile and delete (discard) them.
import tarfile,mailbox
# _box is allready initialized in the previous function
for _maildir, _box, _key, _foldername in __my_list:
__msg = _box[_key]
__subdir = __msg.get_subdir()
__suffix = mailbox.Maildir.colon + __msg.get_info()
if __suffix == mailbox.Maildir.colon:
__suffix = ''
__file_name = "%s/.%s/%s/%s%s" % ( _maildir, _foldername, __subdir, _key, __suffix )
try:
tar.add(__file_name)
_box.discard(_key)
except Exception as inst:
error_type = type(inst)
log_text = "%s\: ERROR %s - %s" % (error_type, __file_name, inst)
log_this( logKeySub, log_text )
e.write(log_text + "\n")
I've looked into the python docs for mailbox and email, searched google, stackoverflow, etc. For now I'm resorting to building the path and file name with _maildir, _foldername, _key and get_info().
Edit: Based on a comment by t-8ch I've made this adjustment:
for _maildir, _box, _key, _foldername in __my_list:
try:
__file = _box._toc[_key]
__file_name = "%s/.%s/%s" % ( _maildir, _foldername, __file )
except Exception as inst:
error_type = type(inst) # Type Exception, inst.args, inst
log_text = "%s\: ERROR with %s/.%s - %s - %s" % (error_type, _maildir, _foldername, _key, inst)
log_this( logKeySub, log_text )
e.write(log_text + "\n")
continue
A dry run proved it to work very nicely.
You can use Maildir._toc
. This is a dictionary mapping the keys of mails to the path of their corresponding file. This dictionary is constructed in Maildir._refresh()
. This allows to defer the reading of a mails file only on demand and thus decreases the time to refresh the list of all mails (which happens quite often).
(I did find this in the source)