dirty-helpers/maildir-zero

77 lines
1.8 KiB
Text
Raw Normal View History

2015-06-29 18:18:07 +02:00
#!/usr/bin/env python
"""
List maildirs in your Maildir++ with message counts.
"""
from __future__ import division, print_function
import os
import os.path
import re
import yaml
2015-06-29 18:18:07 +02:00
def maildirs(startdir):
for dirpath, dirnames, _ in os.walk(startdir):
if set(['cur', 'new', 'tmp']).issubset(set(dirnames)):
yield dirpath
def maildir_count(maildir):
def dir_count(dir_):
return len(os.listdir(dir_))
cur = dir_count(os.path.join(maildir, 'cur'))
new = dir_count(os.path.join(maildir, 'new'))
count = cur + new
2015-06-29 18:18:07 +02:00
return (new, count)
2015-06-29 18:18:07 +02:00
def mailbox_name(maildir, root):
name = re.sub(root + r'/?\.?', '', maildir)
if name == '':
return 'INBOX'
else:
return name
config_filename = '~/.config/maildir-zero.yml'
config = {}
try:
config = yaml.load(open(os.path.expanduser(config_filename)))
except:
pass
ignore_zero = config.get('ignore_zero', True)
ignore = config.get('ignore', [r'spam'])
sort_by_count = config.get('sort_by_count', True)
2015-06-29 18:18:07 +02:00
root = os.path.expanduser('~/Maildir')
counts = {}
for maildir in maildirs(root):
name = mailbox_name(maildir, root)
_, count = maildir_count(maildir)
2015-06-29 18:18:07 +02:00
if count == 0 and ignore_zero:
2015-06-29 18:18:07 +02:00
continue
if any(re.match(i, name) for i in ignore):
continue
counts[name] = count
2015-06-29 18:18:07 +02:00
2015-07-20 20:38:02 +02:00
if sort_by_count:
key = lambda i: i[1]
reverse = True
else:
key = lambda i: i[0]
reverse = False
length_name = max(len(name) for name in counts.keys())
length_count = max(len(str(count)) for count in counts.values())
2015-07-20 20:38:02 +02:00
for name, count in sorted(counts.items(), key=key, reverse=reverse):
print('{0:{1}}\t{2:{3}d}'.format(name, length_name, count, length_count))
2016-01-23 19:10:07 +01:00
total = sum(counts.values())
print('\n{0:{1}}\t{2:{3}d}'.format('Total', length_name, total, length_count))