]> git.wh0rd.org - home.git/blame - .config/mutt/list-mailboxes.py
test.cc: new C++ test
[home.git] / .config / mutt / list-mailboxes.py
CommitLineData
6bf5458d
MF
1#!/usr/bin/python
2
3"""Produce a "sorted" list of maildir paths based on subdirs."""
4
5from __future__ import print_function
6
7import os
8import sys
9
10
11IGNORED_DIRS = set((
12 'drafts',
13 'outbox',
14 'templates',
15 '.inbox.directory',
16))
17
18
19def find_dirs(topdir, ignored):
20 """Walk the subdirs following the KDE layout."""
21 ret = []
22 for d in sorted(os.listdir(topdir)):
23 if d in ('cur', 'new', 'tmp'):
24 continue
25
26 fulld = os.path.join(topdir, d)
27 if d.startswith('.') or fulld in ignored:
28 continue
29 if not os.path.islink(fulld):
30 if os.path.isdir(os.path.join(fulld, 'cur')):
31 ret.append(fulld)
32 subdir = os.path.join(topdir, '.%s.directory' % d)
33 if subdir not in ignored and os.path.isdir(subdir):
34 ret.extend(find_dirs(subdir, ignored))
35
36 return ret
37
38
39def main(argv):
40 maildir = os.path.expanduser('~/.mail/')
41 results = []
42 ignored = set(os.path.join(maildir, x) for x in IGNORED_DIRS)
43
44 results = find_dirs(maildir, ignored)
45 print(' '.join('"%s"' % x for x in results))
46 return 0
47
48
49# Old logic that doesn't sort right.
50 for root, dirs, _ in os.walk(maildir, followlinks=False):
51 for d in ('cur', 'new', 'tmp'):
52 try:
53 dirs.remove(d)
54 except ValueError:
55 pass
56
57 dirs.sort()
58 for d in dirs[:]:
59 fulld = os.path.join(root, d)
60 if fulld in ignored:
61 dirs.remove(d)
62 elif not os.path.islink(fulld):
63 if os.path.isdir(os.path.join(fulld, 'cur')):
64 results.append(fulld)
65
66 print('\n'.join('"%s"' % x for x in results))
67
68
69if __name__ == '__main__':
70 sys.exit(main(sys.argv[1:]))