maildir->mbox conversion script review

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • matej

    maildir->mbox conversion script review

    Hi,

    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

    Matěj

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

    mdir2mbx [maildirName] [mboxName]

    TODO:
    * convert all (or as many as possible) status flags from KMail
    to Thunderbird.
    * testing, testing, testing
    """
    __version__ = "$Revision: 1.2 $"
    __author__ = "Matej Cepl <mcepl@redhat.c om>"
    __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,f ilename):

    mailbox.UnixMai lbox.__init__(s elf,filename,em ail.message_fro m_file)
    self.boxname=fi lename
    self.content = ""

    def add(self,msg):
    fp = cStringIO.Strin gIO()
    g = email.Generator .Generator(fp, mangle_from_=Tr ue,
    maxheaderlen=65 )
    g.flatten(msg,u nixfrom=True)
    self.content += "%s\n\n" % fp.getvalue()

    def write(self):
    outfile=file(se lf.boxname,"wb" )
    outfile.write(" %s\n" % self.content)
    outfile.close()

    class MyMaildir(mailb ox.Maildir):
    def __init__(self,d irname):
    mailbox.Maildir .__init__(self, dirname,email.m essage_from_fil e)
    self.dirname = dirname
    self.decfunc = email.Header.de code_header
    self.msg = ""

    def __translateHead er(self,headerN ame):
    header = email.Header.de code_header(sel f.msg[headerName])
    string = header[0][0]
    encoding = header[0][1]
    if not(encoding):
    encoding = "ascii"
    outstr = string.decode(e ncoding,'ignore ')
    return outstr

    def listHeaders(sel f):
    for self.msg in self:
    hdrfrom = self.__translat eHeader("From")
    #hdrto = self.__translat eHeader("To")
    hdrdate = self.__translat eHeader("Date")
    hdrsubject = self.__translat eHeader("Subjec t")
    print "%s;%s;%s" % (hdrfrom,hdrdat e,hdrsubject)
    #header =
    email.Header.de code_header(sel f.msg["Message-Id"])[0][0]
    #print "%s;%s" % (self.dirname,h eader)

    def writeMBox(self, filename):
    mbox = Mailbox(filenam e)
    for self.msg in self:
    mbox.add(self.m sg)
    mbox.write()

    if __name__=="__ma in__":
    obj = MyMaildir(sys.a rgv[1])
    obj.writeMBox(s ys.argv[2])

Working...