-
-
Save somini/84c5d624d0f14784e60da3817530ab48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# vim: ts=4:sw=4:et | |
import sys | |
import os | |
from collections import defaultdict | |
from glob import glob | |
import logging | |
from logging import info | |
from pprint import pprint | |
MAILROOT = os.path.expanduser('~/eMail') | |
def traverse_hierarchy(realm_location): | |
""" | |
Traverse a Realm/Turf/Account/Folder hierarchy. | |
Consider only the valid Maildir folders | |
""" | |
tree = defaultdict(lambda: defaultdict(list)) | |
for folder_path in glob("{}/*/*/*".format(realm_location)): | |
folder_path_split = folder_path | |
(folder_path_split, folder) = os.path.split(folder_path_split) | |
(folder_path_split, account) = os.path.split(folder_path_split) | |
(folder_path_split, turf) = os.path.split(folder_path_split) | |
# Make sure the folder is a Maildir | |
subfolders = frozenset(os.listdir(folder_path)) | |
if {'cur', 'new', 'tmp'}.issubset(subfolders): | |
info("Folder: {} | {},{},{}".format(folder_path, turf,account,folder)) | |
tree[turf][account].append(folder) | |
return tree | |
def create_folderlist(tree): | |
""" | |
Create a folderlist from an hierarchy. | |
Sort by "importantness". | |
""" | |
def sort_important(name): | |
first_letter = 'M' # Middle of the road | |
if name == 'INBOX': | |
first_letter = 'A' | |
if name == 'Sent': | |
first_letter = 'C' | |
if name == 'Drafts': | |
first_letter = 'F' | |
if name in ('Junk', 'Trash', 'Spam'): | |
first_letter = 'V' | |
if name == 'Archive': | |
first_letter = 'Z' | |
info("S: {} {}".format(first_letter, name.lower())) | |
return "{} {}".format(first_letter, name.lower()) | |
folders = list() | |
for turf in tree: | |
folders.append(turf) | |
for account in tree[turf]: | |
folders.append(os.path.join(turf,account)) | |
info(tree[turf][account]) | |
sorted_folders = sorted(tree[turf][account], key=sort_important) | |
info(sorted_folders) | |
for folder in sorted_folders: | |
folders.append(os.path.join(turf,account,folder)) | |
return folders | |
def create_accounttuples(tree): | |
""" | |
Create a list of tuples Turf/Account. | |
""" | |
return [{"turf":turf,"account":account} for turf in tree for account in tree[turf]] | |
if __name__ == '__main__': | |
# logging.basicConfig(level=logging.INFO) | |
if len(sys.argv) > 1: | |
realm = sys.argv[1] | |
info( "Realm: {}".format(realm) ) | |
realm_location = os.path.join(MAILROOT, realm) | |
# Tree: Turf -> Acc -> Folder | |
# Tree: dict -> dict -> list | |
tree = traverse_hierarchy(realm_location) | |
info(tree) | |
folderlist = create_folderlist(tree) | |
info(folderlist) | |
print('mailboxes ' + ' '.join(["\"+{}\"".format(name) for name in folderlist])) | |
accountlist = create_accounttuples(tree) | |
info(accountlist) | |
for account_tuple in accountlist: | |
print("folder-hook \"+{turf}/{account}/\" \"set my_Turf='{turf}' my_Account='{account}'\"".format(**account_tuple)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment