@@ -0,0 +1,1785 @@
+#!/usr/bin/env python3%0A#%0A# Copyright (c) 2017 Aaron LI%0A# MIT license%0A#%0A# 2017-04-17%0A#%0A%0A%22%22%22%0ARename email filename according to the given pattern.%0A%22%22%22%0A%0Aimport os%0Aimport sys%0Aimport email%0Aimport argparse%0A%0A%0Aclass Email:%0A    def __init__(self, filename):%0A        self.filename = filename%0A        self.message = email.message_from_file(open(filename))%0A        self.date_ = email.utils.parsedate_to_datetime(self.message%5B%22Date%22%5D)%0A        self.from_ = email.utils.parseaddr(self.message%5B%22From%22%5D)%5B-1%5D%0A%0A    @property%0A    def headers(self):%0A        return %7B%0A            %22date%22: self.date_.isoformat(),%0A            %22from%22: self.from_%0A        %7D%0A%0A    def rename(self, pattern, dryrun=False):%0A        newname = pattern.format(**self.headers)%0A        newfp = os.path.join(os.path.dirname(self.filename), newname)%0A        print(%22Rename: '%25s' -%3E '%25s'%22 %25 (self.filename, newfp), file=sys.stderr)%0A        if not dryrun:%0A            os.renames(self.filename, newfp)%0A%0A%0Adef main():%0A    # format: %3Cfilename%3E:2,%3Cflags%3E%0A    default_pattern = %22%7Bdate%7D_%7Bfrom%7D:2,S%22%0A%0A    parser = argparse.ArgumentParser(%0A        description=%22Rename email filename according the given pattern%22)%0A    parser.add_argument(%22-p%22, %22--pattern%22, dest=%22pattern%22,%0A                        default=default_pattern,%0A                        help=%22New filename pattern (default: '%25s')%22 %25%0A                             default_pattern)%0A    parser.add_argument(%22-n%22, %22--dry-run%22, dest=%22dryrun%22,%0A                        action=%22store_true%22, help=%22Dry run%22)%0A    parser.add_argument(%22emails%22, nargs=%22+%22,%0A                        help=%22List of email files to be renamed%22)%0A    args = parser.parse_args()%0A%0A    for fn in args.emails:%0A        eml = Email(fn)%0A        eml.rename(pattern=args.pattern, dryrun=args.dryrun)%0A%0A%0Aif __name__ == %22__main__%22:%0A    main()%0A

Add bin/rename-email.py: Rename email according to the given pattern