471,117 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,117 software developers and data experts.

maildir->mbox conversion script review


I am writing a script to convert couple of thousand emails (in couple
of hundred folders) and before I will get to the hard part -- maintaing
structure folders and subfolders, and maintaing record of the status of
the message, I would like to be sure that I have at least maildir->mbox
conversion right. Could anybody comment on the below shown code please?
Thanks a lot


#!/usr/bin/env python
"""mdir2mbx: yet another maildir -mbox converter

mdir2mbx [maildirName] [mboxName]

* convert all (or as many as possible) status flags from KMail
to Thunderbird.
* testing, testing, testing
__version__ = "$Revision: 1.2 $"
__author__ = "Matej Cepl <mc***@redhat.com>"
__copyright__ = "(C) 2007 Matej Cepl. MIT/X11."
__date__ = "$Date: 2007/01/08 23:56:29 $"
___contributors__ = []

import email, email.Errors, email.Header, email.Generator, mailbox
import codecs, sys, cStringIO

class Mailbox(mailbox.UnixMailbox):
def __init__(self,filename):

mailbox.UnixMailbox.__init__(self,filename,email.m essage_from_file)
self.content = ""

def add(self,msg):
fp = cStringIO.StringIO()
g = email.Generator.Generator(fp, mangle_from_=True,
self.content += "%s\n\n" % fp.getvalue()

def write(self):
outfile.write("%s\n" % self.content)

class MyMaildir(mailbox.Maildir):
def __init__(self,dirname):
mailbox.Maildir.__init__(self,dirname,email.messag e_from_file)
self.dirname = dirname
self.decfunc = email.Header.decode_header
self.msg = ""

def __translateHeader(self,headerName):
header = email.Header.decode_header(self.msg[headerName])
string = header[0][0]
encoding = header[0][1]
if not(encoding):
encoding = "ascii"
outstr = string.decode(encoding,'ignore')
return outstr

def listHeaders(self):
for self.msg in self:
hdrfrom = self.__translateHeader("From")
#hdrto = self.__translateHeader("To")
hdrdate = self.__translateHeader("Date")
hdrsubject = self.__translateHeader("Subject")
print "%s;%s;%s" % (hdrfrom,hdrdate,hdrsubject)
#header =
#print "%s;%s" % (self.dirname,header)

def writeMBox(self,filename):
mbox = Mailbox(filename)
for self.msg in self:

if __name__=="__main__":
obj = MyMaildir(sys.argv[1])

Jan 10 '07 #1
0 1496

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Julia Goolia | last post: by
6 posts views Thread by chuck amadi | last post: by
reply views Thread by Edvard Majakari | last post: by
reply views Thread by sarcastic | last post: by
34 posts views Thread by Reinhold Birkenfeld | last post: by
27 posts views Thread by Aurangzeb M. Agha | last post: by
10 posts views Thread by Eric | last post: by
6 posts views Thread by cassbiz | last post: by
3 posts views Thread by Mark Shroyer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.