aboutsummaryrefslogtreecommitdiffstats
path: root/bin/mutt_mailboxes
blob: 71547c9260710385cc56ce99f23c9800dd1e606e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python

from argparse import ArgumentParser
from pathlib import Path
from sys import exit

MAILBOX_SUBDIRS = {"cur", "new", "tmp"}


def get_mailbox_from_dir(path: Path, mailboxes: list[Path]) -> None:
    for current_path in path.iterdir():
        if current_path.is_dir() and current_path.name not in MAILBOX_SUBDIRS:
            subdirs = list(current_path.iterdir())
            if MAILBOX_SUBDIRS & set(subdir.name for subdir in subdirs) and current_path not in mailboxes:
                mailboxes.append(current_path)

            for subdir in subdirs:
                if subdir.name not in MAILBOX_SUBDIRS:
                    get_mailbox_from_dir(path=current_path, mailboxes=mailboxes)


def main(output: Path, paths: list[Path]) -> None:
    file_string = ""

    if len(paths) == 0:
        exit(1)

    for path in paths:
        mailboxes: list[Path] = []
        mailboxes_string = "mailboxes "

        if not path.exists():
            exit(1)

        get_mailbox_from_dir(path=path, mailboxes=mailboxes)

        for mailbox in mailboxes:
            mailboxes_string += f" '+{(str(mailbox).replace(str(path.parent) + '/', ''))}'"
        file_string += mailboxes_string + "\n"

    with open(output, "w") as f:
        print(file_string, file=f)


if __name__ == "__main__":
    parser = ArgumentParser(
        description="""
        Create 'mailboxes' entries for one or more directories.
        Each provided directory is
        """
    )
    parser.add_argument(
        "-o",
        "--output",
        default=Path("mailboxes.rc"),
        help="a file to write the mailboxes to (mailboxes.rc by default)",
        type=Path,
    )
    parser.add_argument(
        "paths",
        help="a directory to search in for mailbox directories",
        nargs="+",
        type=Path,
    )

    namespace = parser.parse_args()

    main(output=namespace.output, paths=namespace.paths)