diff options
Diffstat (limited to 'misc')
-rwxr-xr-x | misc/xml/be-mail-to-xml (renamed from misc/xml/be-mbox-to-xml) | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/misc/xml/be-mbox-to-xml b/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"</comment>") 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'<?xml version="1.0" encoding="%s" ?>' % DEFAULT_ENCODING print u"<be-xml>" for message in mb: @@ -151,4 +163,4 @@ def main(mbox_filename): if __name__ == "__main__": import sys - main(sys.argv[1]) + main(sys.argv) |