Created
January 1, 2017 09:42
-
-
Save novel/5a739ba8156d1f60fd39aeaa0b48e789 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import getopt | |
import mailbox | |
import sys | |
def parse_mailbox(filename, dry_run): | |
mbox = mailbox.mbox(filename) | |
sorted_messages = sorted(mbox, key=lambda x: x['subject']) | |
for message in sorted_messages: | |
print message['subject'] | |
if not dry_run: | |
mbox.clear() | |
for m in sorted_messages: | |
mbox.add(m) | |
mbox.close() | |
def main(): | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "df:") | |
except getopt.GetoptError as err: | |
print str(err) | |
sys.exit(2) | |
dry_run = False | |
filename = None | |
for o, a in opts: | |
if o == "-d": | |
dry_run = True | |
elif o == "-f": | |
filename = a | |
else: | |
assert False, "unhandled option" | |
if filename is None: | |
print "filename not specified" | |
sys.exit(2) | |
parse_mailbox(filename, dry_run) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment