Skip to content

Instantly share code, notes, and snippets.

@novel
Created January 1, 2017 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save novel/5a739ba8156d1f60fd39aeaa0b48e789 to your computer and use it in GitHub Desktop.
Save novel/5a739ba8156d1f60fd39aeaa0b48e789 to your computer and use it in GitHub Desktop.
#!/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