From c8985785eb741ff646082879f1ca5e9cfe3873b0 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 20 Jan 2010 15:22:28 -0500 Subject: [PATCH] 'be-mbox-to-xml' -> 'be-mail-to-xml' + support for several formats. --- NEWS | 4 +++ misc/xml/{be-mbox-to-xml => be-mail-to-xml} | 28 +++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) rename misc/xml/{be-mbox-to-xml => be-mail-to-xml} (85%) diff --git a/NEWS b/NEWS index 99d5bca..17d5aee 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +January 20, 2010 + * Renamed 'be-mbox-to-xml' -> 'be-mail-to-xml' and added support for + several mailbox formats. + January 3, 2010 * Changed `be list --uuids` -> `be list --ids` Instead of UUIDs, it now outputs user ids: BUGDIR/BUG diff --git a/misc/xml/be-mbox-to-xml b/misc/xml/be-mail-to-xml similarity index 85% rename from misc/xml/be-mbox-to-xml rename to misc/xml/be-mail-to-xml index 1fc41e0..2add065 100755 --- a/misc/xml/be-mbox-to-xml +++ b/misc/xml/be-mail-to-xml @@ -23,16 +23,19 @@ followed by a blank line. """ import base64 +import codecs import email.utils -from libbe.encoding import get_encoding, set_IO_stream_encodings -from libbe.utility import time_to_str -from mailbox import mbox, Message # the mailbox people really want an on-disk copy +from libbe.util.encoding import get_output_encoding +from libbe.util.utility import time_to_str +import mailbox # the mailbox people really want an on-disk copy +import optparse +import sys from time import asctime, gmtime, mktime import types from xml.sax.saxutils import escape -DEFAULT_ENCODING = get_encoding() -set_IO_stream_encodings(DEFAULT_ENCODING) +DEFAULT_ENCODING = get_output_encoding() +sys.stdout = codecs.getwriter(DEFAULT_ENCODING)(sys.stdout) KNOWN_IDS = [] @@ -140,8 +143,17 @@ def comment_message_to_xml(message, fields=None): lines.append(u"") return u'\n'.join(lines) -def main(mbox_filename): - mb = mbox(mbox_filename) +def main(argv): + parser = optparse.OptionParser(usage='%prog [options] mailbox') + formats = ['mbox', 'Maildir', 'MH', 'Babyl', 'MMDF'] + parser.add_option('-f', '--format', type='choice', dest='format', + help="Select the mailbox format from %s. See the mailbox module's documention for descriptions of these formats." \ + % ', '.join(formats), + default='mbox', choices=formats) + options,args = parser.parse_args(argv) + mailbox_file = args[1] + reader = getattr(mailbox, options.format) + mb = reader(mailbox_file, factory=None) print u'' % DEFAULT_ENCODING print u"" for message in mb: @@ -151,4 +163,4 @@ def main(mbox_filename): if __name__ == "__main__": import sys - main(sys.argv[1]) + main(sys.argv) -- 2.26.2