#!/usr/bin/env python # -*- coding: iso-8859-1 -*- # Search my maildir directory an list the # count of new mails (mtime of each file/mail # gets read) # Python Imports import os import sys import time import getopt ignore=["trash"] def usage(): print """Usage: %s [--hoursback N] Print the number of new mail in $HOME/Mail. New mail is identified by the mtime. This needs a mailbox in maildir format. hoursback: Search emails that changed N hours ago. Default 12. """ % ( os.path.basename(sys.argv[0])) def find_newmail(dir, mtime, count=0): #assert(mtime>100) files=os.listdir(dir) for file in files: file=os.path.join(dir, file) if os.path.isdir(file): count+=find_newmail(file, mtime) else: if os.path.getmtime(file)>mtime: count+=1 return count def main(): try: opts, args = getopt.getopt(sys.argv[1:], "", ["hoursback="]) except getopt.GetoptError, e: print e usage() sys.exit(2) hoursback=12 for o, a in opts: if o=="--hoursback": hoursback=int(a) else: assert False, "Unhandled: %s %s" % (o, a) if args: usage() sys.exit() maildir=os.path.join(os.environ["HOME"], "Maildir") dirs=os.listdir(maildir) mtime=int(time.time())-(60*60*hoursback) counts=[] lens=[] lenscount=[] for dir in dirs: if dir in ignore: continue dir_abs=os.path.join(maildir, dir) if not os.path.isdir(dir_abs): continue count=find_newmail(dir_abs, mtime) if not count: continue counts.append((count, dir)) lens.append(len(dir)) lenscount.append(len(str(count))) if not counts: print "No new mail" return counts.sort() counts.reverse() maxlen=max(lens) maxlencount=max(lenscount) for count, dir in counts: space=(maxlen-len(dir))*" " spacecount=(maxlencount-len(str(count)))*" " print "%s%s: %s%s" % ( dir, space, spacecount, count) if __name__=="__main__": main()