X-Git-Url: https://git.wh0rd.org/?p=home.git;a=blobdiff_plain;f=.config%2Fmutt%2Flist-mailboxes.py;h=3737d7ad2c452cbd96002013e271dbabe9bbaf67;hp=f6e0a014cef594d95a507bb3b0776cdca63467ff;hb=b0e7e9096efa88ff34f2f33f71e99a1c2eeffa32;hpb=50dba5599fd34bc07d950840b2f9c7be574b751f diff --git a/.config/mutt/list-mailboxes.py b/.config/mutt/list-mailboxes.py index f6e0a01..3737d7a 100755 --- a/.config/mutt/list-mailboxes.py +++ b/.config/mutt/list-mailboxes.py @@ -1,70 +1,49 @@ -#!/usr/bin/python +#!/usr/bin/python3 """Produce a "sorted" list of maildir paths based on subdirs.""" from __future__ import print_function import os +from pathlib import Path import sys -IGNORED_DIRS = set(( +IGNORED_DIRS = { 'drafts', 'outbox', 'templates', '.inbox.directory', -)) +} +MAILDIR_DIRS = { + 'cur', 'new', 'tmp', +} def find_dirs(topdir, ignored): """Walk the subdirs following the KDE layout.""" - ret = [] - for d in sorted(os.listdir(topdir)): - if d in ('cur', 'new', 'tmp'): + for d in sorted(topdir.iterdir()): + if d.name in MAILDIR_DIRS: continue - fulld = os.path.join(topdir, d) - if d.startswith('.') or fulld in ignored: + if d in ignored: continue - if not os.path.islink(fulld): - if os.path.isdir(os.path.join(fulld, 'cur')): - ret.append(fulld) - subdir = os.path.join(topdir, '.%s.directory' % d) - if subdir not in ignored and os.path.isdir(subdir): - ret.extend(find_dirs(subdir, ignored)) - - return ret + if not d.is_symlink(): + if (d / 'cur').is_dir(): + yield d + subdir = topdir / f'.{d.name}.directory' + if subdir not in ignored and subdir.is_dir(): + yield from find_dirs(subdir, ignored) def main(argv): - maildir = os.path.expanduser('~/.mail/') - results = [] - ignored = set(os.path.join(maildir, x) for x in IGNORED_DIRS) + maildir = Path('~/.mail/').expanduser() + ignored = {maildir / x for x in IGNORED_DIRS} results = find_dirs(maildir, ignored) - print(' '.join('"%s"' % x for x in results)) + print(' '.join(f'"{x}"' for x in results)) return 0 -# Old logic that doesn't sort right. - for root, dirs, _ in os.walk(maildir, followlinks=False): - for d in ('cur', 'new', 'tmp'): - try: - dirs.remove(d) - except ValueError: - pass - - dirs.sort() - for d in dirs[:]: - fulld = os.path.join(root, d) - if fulld in ignored: - dirs.remove(d) - elif not os.path.islink(fulld): - if os.path.isdir(os.path.join(fulld, 'cur')): - results.append(fulld) - - print('\n'.join('"%s"' % x for x in results)) - - if __name__ == '__main__': sys.exit(main(sys.argv[1:]))